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
- div_round
- floor_div
- mod
- divmod
- abs
- round
- floor
- ceil
- sqrt
- sin
- cos
- tan
- atan
- atan2
- asin
- acos
- pi
- log
- ln
- exp
- log10
- pow
- ipow
- min
- max
- sum
- product
- avg
- parse_int
- parse_float
- idiv
- clamp
- random
- gcd
- sqrt_int
- pow_mod
- band
- bor
- bxor
- bnot
- shl
- shr
- bit_test
- bit_set
- bit_clr
- bit_next
- bit_len
- median
- pstdev
- stddev
- stdev
- variance
- pvariance
to_fixed
to_fixed(x: Float, digits: Int) -> Stringx with exactly digits decimals ("%.2f"), rounded half away from zero on the decimal text: to_fixed(1.005, 2) = "1.01".
div_round
div_round(n: Int, d: Int) -> IntExact 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.
Used in verified programs:
- escrow_finance/loan_amortization, line 49: return div_round(p * r, 120000) // bps annual -> one month, rounded HALF_UP (BigDecimal style)
floor_div
floor_div(a: Int, b: Int) -> IntDivision rounded toward -∞ (Ruby/Python //): floor_div(-150, 100) = -2. idiv truncates toward zero; / is exact.
mod
mod(a: Int, b: Int) -> IntModulo with the DIVISOR's sign (Ruby/Python %): mod(-150, 100) = 50. The % operator keeps the dividend's sign (C/Rust): -150 % 100 = -50.
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].
abs
abs(x: Int|Float) -> Int|FloatAbsolute value, arbitrary precision (abs(-9223372036854775808) is 9223372036854775808).
Used in verified programs:
- algorithms/gcd_lcm, line 19: let x = abs(a)
- finance/bond_pricer, line 29: if abs(price - face_val) < 0.01 { return "par" }
- finance/compound_interest, line 37: assert abs(future_value(1000.0, 0.05, 2) - 1102.5) < 0.001
- finance/currency_converter, line 50: assert abs(to_usd("EUR", 100.0) - 108.695652) < 0.001
round
round(x: Float) -> Int | round(x: Float, digits: Int) -> FloatRound half away from zero to the nearest integer, or keep digits decimals: round(2.345, 2) = 2.35.
Used in verified programs:
- math/fibonacci, line 35: return round((pow(phi, n) - pow(psi, n)) / sqrt(5.0))
floor
floor(x: Float) -> IntLargest integer <= x.
Used in verified programs:
- agents/react_calculator, line 33: if floor(k / d) * d == k { is_prime = false break }
- algorithms/binary_search_list, line 18: let mid = floor((lo + hi) / 2)
- algorithms/gcd_lcm, line 23: y = x - floor(x / y) * y // x mod y
- algorithms/insertion_sort, line 32: let mid = floor((lo + hi) / 2)
ceil
ceil(x: Float) -> IntSmallest integer >= x.
sqrt
sqrt(x: Int|Float) -> FloatSquare root.
Used in verified programs:
- finance/returns_stats, line 24: return sqrt(acc / (len(rets) - 1))
- math/complex, line 33: return sqrt(x.re * x.re + x.im * x.im)
- math/fibonacci, line 33: let phi = (1.0 + sqrt(5.0)) / 2.0
- math/quadratic, line 26: let sq = sqrt(d)
sin
sin(x: Int|Float) -> FloatSine (radians). Also cos, tan, atan, atan2(y, x).
cos
cos(x: Int|Float) -> FloatCosine (radians).
tan
tan(x: Int|Float) -> FloatTangent (radians).
atan
atan(x: Int|Float) -> FloatArc tangent.
atan2
atan2(y: Float, x: Float) -> FloatArc tangent of y/x, quadrant-aware.
asin
asin(x: Float) -> FloatArc sine (x in [-1, 1], else kind range).
acos
acos(x: Float) -> FloatArc cosine (x in [-1, 1], else kind range).
pi
pi() -> FloatThe constant π.
log
log(x: Int|Float) -> FloatNatural logarithm.
Used in verified programs:
- finance/compound_interest, line 30: return log(2.0) / log(1.0 + rate)
ln
ln(x: Int|Float) -> FloatAlias of log (natural logarithm).
exp
exp(x: Int|Float) -> Floate raised to the power x.
Used in verified programs:
- finance/compound_interest, line 25: return principal * exp(rate * years)
log10
log10(x: Int|Float) -> FloatBase-10 logarithm.
pow
pow(base: Int|Float, exp: Int|Float) -> Floatbase raised to exp (always a Float; ipow for an exact Int power).
Used in verified programs:
- finance/bond_pricer, line 17: pv = pv + coupon / pow(1.0 + ytm, t)
- finance/compound_interest, line 15: return principal * pow(1.0 + rate, years)
- finance/loan_amortization, line 16: let g = pow(1.0 + r, months)
- finance/npv, line 15: return cash / pow(1.0 + rate, period)
ipow
ipow(base: Int, exp: Int) -> IntExact 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.
min
min(a, b) -> Int|Float | min(list: List) -> Int|FloatSmaller of two numbers, or the minimum of a list (Float if any element is). An empty list gives () (no minimum exists).
Used in verified programs:
- aerospace/unreliable_airspeed, line 61: let lo = min(a_kt, min(b_kt, c_kt))
- algorithms/edit_distance, line 32: dp[i][j] = min(list(
- finance/tax_brackets, line 29: let top = min(income, cap)
- games/war, line 33: let n = min(len(deck1), len(deck2))
max
max(a, b) -> Int|Float | max(list: List) -> Int|FloatLarger of two numbers, or the maximum of a list (Float if any element is). An empty list gives () (no maximum exists).
Used in verified programs:
- aerospace/unreliable_airspeed, line 60: let hi = max(a_kt, max(b_kt, c_kt))
- algorithms/counting_sort, line 17: let hi = max(xs)
- algorithms/knapsack, line 35: dp[i][c] = max(skip, take)
- algorithms/lis, line 26: return max(dp)
sum
sum(list: List) -> Int|FloatSum 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).
Used in verified programs:
- finance/moving_average, line 28: return sum(prices) / len(prices)
- finance/portfolio_pnl, line 35: return sum(pnls)
- finance/returns_stats, line 14: return sum(rets) / len(rets)
- games/conway_life, line 38: return sum(grid)
product
product(list: List) -> Int|FloatProduct of a list of numbers; 1 when empty.
Used in verified programs:
- math/combinatorics, line 23: return product(range(1, n + 1))
- math/primes, line 72: assert product(factorize(360)) == 360
- math/quadratic, line 49: assert abs(product(roots(1.0, 0 - 5.0, 6.0)) - 6.0) < 0.0000001
avg
avg(list: List) -> Int|FloatMean 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.
Used in verified programs:
- records/employees, line 34: return avg(rows |> pluck("salary"))
- records/students, line 30: return avg(s.scores |> pluck("pts"))
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.
parse_float
parse_float(s: String) -> Float | ()Strict float parse: () unless the whole string is a finite number.
idiv
idiv(a: Int, b: Int) -> IntInteger division truncating toward zero; errors on division by zero.
Used in verified programs:
- aerospace/unreliable_airspeed, line 70: let w = try { agreed_kt.set(id, idiv(a_kt + b_kt + c_kt, 3)) }
- civic/lottery, line 70: let idx = seed - idiv(seed, len(pool)) * len(pool)
- escrow_finance/vesting_schedule, line 58: return idiv(total * month, duration) // linear in between
- games_economy/wager_duel, line 77: let half = idiv(pot.get(duel) ?? 0, 2)
clamp
clamp(v, lo, hi) -> Int|FloatConstrain 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.
random
random() -> Float | random(max: Int) -> Int | random(min: Int, max: Int) -> IntTime-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().
Used in verified programs:
- agents/horde_rounds, line 50: let patience = random(30)
- math/monte_carlo_pi, line 22: let x = random()
gcd
gcd(a: Int, b: Int) -> IntGreatest common divisor (Euclid, absolute values).
Used in verified programs:
- algorithms/gcd_lcm, line 70: assert gcd_iter(48, 36) == gcd(48, 36)
- math/gcd_lcm, line 21: return idiv(a * b, gcd(a, b))
sqrt_int
sqrt_int(n: Int) -> IntInteger square root; errors on negative input.
pow_mod
pow_mod(base: Int, exp: Int, m: Int) -> IntModular exponentiation base^exp mod m; errors if m is zero.
Used in verified programs:
- math/modular, line 22: return pow_mod(a, p - 1, p) == 1
band
band(a: Int, b: Int) -> IntBitwise AND.
Used in verified programs:
- games/card_deck, line 39: state = band(state * 1103515245 + 12345, 2147483647)
bor
bor(a: Int, b: Int) -> IntBitwise OR.
bxor
bxor(a: Int, b: Int) -> IntBitwise XOR.
Used in verified programs:
- games/nim, line 23: x = bxor(x, h)
bnot
bnot(a: Int) -> IntBitwise NOT.
shl
shl(a: Int, n: Int) -> IntExact 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.
shr
shr(a: Int, n: Int) -> IntArithmetic shift right by a nonnegative Int count; arbitrarily large counts give 0 (nonnegative a) or -1 (negative a).
bit_test
bit_test(a: Int, i: Int) -> Int1 if bit i of a is set, else 0.
bit_set
bit_set(a: Int, i: Int) -> Inta with bit i set.
bit_clr
bit_clr(a: Int, i: Int) -> Inta with bit i cleared.
bit_next
bit_next(a: Int, i: Int) -> IntIndex of the lowest set bit at or above i, or -1 if none.
bit_len
bit_len(a: Int) -> IntExact number of significant bits in the magnitude, including BigInt.
median
median(xs: List) -> Int | FloatOrder 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.
pstdev
pstdev(xs: List) -> FloatPopulation standard deviation (divide by n). Preserve finite and subnormal deviations even when their variance is outside Float range.
stddev
stddev(xs: List) -> FloatSame as pstdev (population).
stdev
stdev(xs: List) -> FloatSAMPLE standard deviation (divide by n - 1); needs two values. Preserve finite and subnormal deviations even when their variance is outside Float range.
variance
variance(xs: List) -> FloatSAMPLE variance (divide by n - 1) — statistics.variance; pvariance is the population form.
pvariance
pvariance(xs: List) -> FloatPopulation variance (divide by n) — statistics.pvariance.