soma.cell

Math builtins

54 builtins of Soma 3.2.4 in the category Math, with their exact signatures, as soma describe --builtins --json prints them.

to_fixed

to_fixed(x: Float, digits: Int) -> String

x with exactly digits decimals ("%.2f"), rounded half away from zero on the decimal text: to_fixed(1.005, 2) = "1.01".

deterministic · replay: pure

div_round

div_round(n: Int, d: Int) -> Int

Exact integer division rounded to the nearest, half away from zero (BigDecimal HALF_UP): div_round(10125 * 600, 120000) == 51, div_round(-7, 2) == -4. Money in cents stays exact.

deterministic · replay: pure

Used in verified programs:

floor_div

floor_div(a: Int, b: Int) -> Int

Division rounded toward -∞ (Ruby/Python //): floor_div(-150, 100) = -2. idiv truncates toward zero; / is exact.

deterministic · replay: pure

mod

mod(a: Int, b: Int) -> Int

Modulo with the DIVISOR's sign (Ruby/Python %): mod(-150, 100) = 50. The % operator keeps the dividend's sign (C/Rust): -150 % 100 = -50.

deterministic · replay: pure

divmod

divmod(a: Int, b: Int) -> [q, r]

[floor_div(a, b), mod(a, b)] — q * b + r == a, with r taking the DIVISOR's sign (|r| < |b|): divmod(7, -3) = [-3, -2].

deterministic · replay: pure

abs

abs(x: Int|Float) -> Int|Float

Absolute value, arbitrary precision (abs(-9223372036854775808) is 9223372036854775808).

deterministic · replay: pure

Used in verified programs:

round

round(x: Float) -> Int | round(x: Float, digits: Int) -> Float

Round half away from zero to the nearest integer, or keep digits decimals: round(2.345, 2) = 2.35.

deterministic · replay: pure

Used in verified programs:

  • math/fibonacci, line 35: return round((pow(phi, n) - pow(psi, n)) / sqrt(5.0))

floor

floor(x: Float) -> Int

Largest integer <= x.

deterministic · replay: pure

Used in verified programs:

ceil

ceil(x: Float) -> Int

Smallest integer >= x.

deterministic · replay: pure

sqrt

sqrt(x: Int|Float) -> Float

Square root.

deterministic · replay: pure

Used in verified programs:

sin

sin(x: Int|Float) -> Float

Sine (radians). Also cos, tan, atan, atan2(y, x).

deterministic · replay: pure

cos

cos(x: Int|Float) -> Float

Cosine (radians).

deterministic · replay: pure

tan

tan(x: Int|Float) -> Float

Tangent (radians).

deterministic · replay: pure

atan

atan(x: Int|Float) -> Float

Arc tangent.

deterministic · replay: pure

atan2

atan2(y: Float, x: Float) -> Float

Arc tangent of y/x, quadrant-aware.

deterministic · replay: pure

asin

asin(x: Float) -> Float

Arc sine (x in [-1, 1], else kind range).

deterministic · replay: pure

acos

acos(x: Float) -> Float

Arc cosine (x in [-1, 1], else kind range).

deterministic · replay: pure

pi

pi() -> Float

The constant π.

deterministic · replay: pure

log

log(x: Int|Float) -> Float

Natural logarithm.

deterministic · replay: pure

Used in verified programs:

ln

ln(x: Int|Float) -> Float

Alias of log (natural logarithm).

deterministic · replay: pure

exp

exp(x: Int|Float) -> Float

e raised to the power x.

deterministic · replay: pure

Used in verified programs:

log10

log10(x: Int|Float) -> Float

Base-10 logarithm.

deterministic · replay: pure

pow

pow(base: Int|Float, exp: Int|Float) -> Float

base raised to exp (always a Float; ipow for an exact Int power).

deterministic · replay: pure

Used in verified programs:

ipow

ipow(base: Int, exp: Int) -> Int

Exact Int power (exp ≥ 0; kind range past the Int size limit). to_int(pow(3, 40)) is off by 33, ipow(3, 40) is exact.

deterministic · replay: pure

min

min(a, b) -> Int|Float | min(list: List) -> Int|Float

Smaller of two numbers, or the minimum of a list (Float if any element is). An empty list gives () (no minimum exists).

deterministic · replay: pure

Used in verified programs:

max

max(a, b) -> Int|Float | max(list: List) -> Int|Float

Larger of two numbers, or the maximum of a list (Float if any element is). An empty list gives () (no maximum exists).

deterministic · replay: pure

Used in verified programs:

sum

sum(list: List) -> Int|Float

Sum of a list of numbers (Int-exact unless any element is a Float); 0 when empty. Floats are added left to right without compensation (NumPy's pairwise / Python's fsum can differ in the last bits).

deterministic · replay: pure

Used in verified programs:

product

product(list: List) -> Int|Float

Product of a list of numbers; 1 when empty.

deterministic · replay: pure

Used in verified programs:

avg

avg(list: List) -> Int|Float

Mean of a list of numbers: finite operands are averaged exactly before Float rounding, avoiding intermediate overflow and cancellation. An exact mean of Ints stays an Int; () when empty.

deterministic · replay: pure

Used in verified programs:

parse_int

parse_int(s: String, base: Int?) -> Int | ()

Strict integer parse: () unless the WHOLE string is an integer ("1.5", "12abc", "" → ()). parse_int("ff", 16) = 255 (base 2..36, no 0x prefix). to_int() is lenient and truncates.

deterministic · replay: pure

parse_float

parse_float(s: String) -> Float | ()

Strict float parse: () unless the whole string is a finite number.

deterministic · replay: pure

idiv

idiv(a: Int, b: Int) -> Int

Integer division truncating toward zero; errors on division by zero.

deterministic · replay: pure

Used in verified programs:

clamp

clamp(v, lo, hi) -> Int|Float

Constrain numeric v to [lo, hi] using exact Int/Float comparisons; return the selected operand with its type. Reject nonnumeric operands, reversed bounds and NaN bounds; a NaN value stays NaN.

deterministic · replay: pure

random

random() -> Float | random(max: Int) -> Int | random(min: Int, max: Int) -> Int

Time-seeded PRNG: float in [0,1), or int in [0,max) / [min,max). There is no seed: for reproducible runs write your own generator (an LCG over Ints), and for secrets use random_token().

not deterministic · replay: nondeterministic: replay reports it as a divergence source

Used in verified programs:

gcd

gcd(a: Int, b: Int) -> Int

Greatest common divisor (Euclid, absolute values).

deterministic · replay: pure

Used in verified programs:

sqrt_int

sqrt_int(n: Int) -> Int

Integer square root; errors on negative input.

deterministic · replay: pure

pow_mod

pow_mod(base: Int, exp: Int, m: Int) -> Int

Modular exponentiation base^exp mod m; errors if m is zero.

deterministic · replay: pure

Used in verified programs:

band

band(a: Int, b: Int) -> Int

Bitwise AND.

deterministic · replay: pure

Used in verified programs:

  • games/card_deck, line 39: state = band(state * 1103515245 + 12345, 2147483647)

bor

bor(a: Int, b: Int) -> Int

Bitwise OR.

deterministic · replay: pure

bxor

bxor(a: Int, b: Int) -> Int

Bitwise XOR.

deterministic · replay: pure

Used in verified programs:

bnot

bnot(a: Int) -> Int

Bitwise NOT.

deterministic · replay: pure

shl

shl(a: Int, n: Int) -> Int

Exact left shift (a * 2^n), arbitrary precision like every Int op. For a 64-bit wrapping shift (xorshift), mask: band(shl(x, 13), M) with M = shl(1, 64) - 1 bound once (a literal beyond 64 bits is not allowed in [native]); values past 2^63 run [native] code in BigInt mode — prefer 32-bit xorshift masks for speed.

deterministic · replay: pure

shr

shr(a: Int, n: Int) -> Int

Arithmetic shift right by a nonnegative Int count; arbitrarily large counts give 0 (nonnegative a) or -1 (negative a).

deterministic · replay: pure

bit_test

bit_test(a: Int, i: Int) -> Int

1 if bit i of a is set, else 0.

deterministic · replay: pure

bit_set

bit_set(a: Int, i: Int) -> Int

a with bit i set.

deterministic · replay: pure

bit_clr

bit_clr(a: Int, i: Int) -> Int

a with bit i cleared.

deterministic · replay: pure

bit_next

bit_next(a: Int, i: Int) -> Int

Index of the lowest set bit at or above i, or -1 if none.

deterministic · replay: pure

bit_len

bit_len(a: Int) -> Int

Exact number of significant bits in the magnitude, including BigInt.

deterministic · replay: pure

median

median(xs: List) -> Int | Float

Order Int/Float values exactly and preserve the middle value (mean of the two middles for even n). An Int middle stays exact; any NaN input makes the result NaN.

deterministic · replay: pure

pstdev

pstdev(xs: List) -> Float

Population standard deviation (divide by n). Preserve finite and subnormal deviations even when their variance is outside Float range.

deterministic · replay: pure

stddev

stddev(xs: List) -> Float

Same as pstdev (population).

deterministic · replay: pure

stdev

stdev(xs: List) -> Float

SAMPLE standard deviation (divide by n - 1); needs two values. Preserve finite and subnormal deviations even when their variance is outside Float range.

deterministic · replay: pure

variance

variance(xs: List) -> Float

SAMPLE variance (divide by n - 1) — statistics.variance; pvariance is the population form.

deterministic · replay: pure

pvariance

pvariance(xs: List) -> Float

Population variance (divide by n) — statistics.pvariance.

deterministic · replay: pure