math

Numeric utilities over CX int and float scalar kinds. Covers basic arithmetic and rounding, powers/logs/roots, trigonometry and hyperbolics, statistics, bit operations, numeric predicates, number theory, special constants, and explicit modular arithmetic. Every function is pure. Real-valued functions return NaN on domain errors per IEEE 754 rather than raising, while int64 arithmetic is checked by default with `wrapping-*` as the opt-in modular escape hatch.

math:abs

[$math:abs] -> any — Return the absolute value, preserving the operand's numeric kind.

            [?lib 'cx-stdlib/math']
[$math:abs -5]
          
            5
          

math:sign

[$math:sign] -> int — Return -1, 0, or 1 for the sign of a number.

            [?lib 'cx-stdlib/math']
[$math:sign -7]
          
            -1
          

math:floor

[$math:floor] -> int — Round a float down to the nearest integer (toward negative infinity).

            [?lib 'cx-stdlib/math']
[$math:floor 2.7]
          
            2
          

math:ceiling

[$math:ceiling] -> int — Round a float up to the nearest integer (toward positive infinity).

            [?lib 'cx-stdlib/math']
[$math:ceiling 2.1]
          
            3
          

math:round

[$math:round] -> int — Round a float to the nearest integer using banker's rounding (half-to-even).

            [?lib 'cx-stdlib/math']
[$math:round 0.5]
          
            0
          

math:round-half-up

[$math:round-half-up] -> int — Round a float to the nearest integer, ties going half-away-from-zero.

            [?lib 'cx-stdlib/math']
[$math:round-half-up 0.5]
          
            1
          

math:round-half-even

[$math:round-half-even] -> int — Round a float to the nearest integer using half-to-even; explicit alias for round.

            [?lib 'cx-stdlib/math']
[$math:round-half-even 2.5]
          
            2
          

math:round-to

[$math:round-to] -> float — Round a float to the given number of decimal places, returning a float.

            [?lib 'cx-stdlib/math']
[$math:round-to 3.14159 2]
          
            3.14
          

math:truncate

[$math:truncate] -> int — Truncate a float toward zero, discarding the fractional part.

            [?lib 'cx-stdlib/math']
[$math:truncate -2.9]
          
            -2
          

math:max

[$math:max] -> any — Return the largest value in a sequence.

            [?lib 'cx-stdlib/math']
[$math:max (3, 7, 2, 9, 1)]
          
            9
          

math:min

[$math:min] -> any — Return the smallest value in a sequence.

            [?lib 'cx-stdlib/math']
[$math:min (3, 7, 2, 9, 1)]
          
            1
          

math:clamp

[$math:clamp] -> any — Constrain a value to the inclusive range between lo and hi.

            [?lib 'cx-stdlib/math']
[$math:clamp 5 0 10]
          
            5
          

math:pow

[$math:pow] -> float — Raise base to the power of exp.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:pow 2.0 10.0] 0]
          
            1024.0
          

math:sqrt

[$math:sqrt] -> float — Return the square root; negative input yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:sqrt 16.0] 0]
          
            4.0
          

math:cbrt

[$math:cbrt] -> float — Return the cube root, defined for negative input.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:cbrt 27.0] 0]
          
            3.0
          

math:exp

[$math:exp] -> float — Return e raised to the power of x.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:exp 0.0] 0]
          
            1.0
          

math:log

[$math:log] -> float — Return the natural (base-e) logarithm; non-positive input yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:log [$math:e]] 8]
          
            1.0
          

math:log2

[$math:log2] -> float — Return the base-2 logarithm; non-positive input yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:log2 1024.0] 0]
          
            10.0
          

math:log10

[$math:log10] -> float — Return the base-10 logarithm; non-positive input yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:log10 100.0] 0]
          
            2.0
          

math:log-base

[$math:log-base] -> float — Return the logarithm of x in an arbitrary base.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:log-base 81.0 3.0] 0]
          
            4.0
          

math:sin

[$math:sin] -> float — Return the sine of an angle in radians.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:sin 0.0] 0]
          
            0.0
          

math:cos

[$math:cos] -> float — Return the cosine of an angle in radians.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:cos 0.0] 0]
          
            1.0
          

math:tan

[$math:tan] -> float — Return the tangent of an angle in radians.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:tan [$math:round-to [$math:pi] 15]] 8]
          
            0.0
          

math:asin

[$math:asin] -> float — Return the arcsine in radians; input outside -1..1 yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:asin 0.0] 0]
          
            0.0
          

math:acos

[$math:acos] -> float — Return the arccosine in radians; input outside -1..1 yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:acos 1.0] 0]
          
            0.0
          

math:atan

[$math:atan] -> float — Return the arctangent in radians.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:atan 0.0] 0]
          
            0.0
          

math:atan2

[$math:atan2] -> float — Return the angle in radians of the point (x, y), respecting quadrant.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:atan2 1.0 1.0] 8]
          
            0.78539816
          

math:sinh

[$math:sinh] -> float — Return the hyperbolic sine of x.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:sinh 0.0] 0]
          
            0.0
          

math:cosh

[$math:cosh] -> float — Return the hyperbolic cosine of x.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:cosh 0.0] 0]
          
            1.0
          

math:tanh

[$math:tanh] -> float — Return the hyperbolic tangent of x.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:tanh 0.0] 0]
          
            0.0
          

math:asinh

[$math:asinh] -> float — Return the inverse hyperbolic sine of x.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:asinh 0.0] 0]
          
            0.0
          

math:acosh

[$math:acosh] -> float — Return the inverse hyperbolic cosine of x; input below 1 yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:acosh 1.0] 0]
          
            0.0
          

math:atanh

[$math:atanh] -> float — Return the inverse hyperbolic tangent of x; input outside -1..1 yields NaN.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:atanh 0.0] 0]
          
            0.0
          

math:deg-to-rad

[$math:deg-to-rad] -> float — Convert an angle from degrees to radians.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:deg-to-rad 180.0] 8]
          
            3.14159265
          

math:rad-to-deg

[$math:rad-to-deg] -> float — Convert an angle from radians to degrees.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:rad-to-deg [$math:pi]] 0]
          
            180.0
          

math:sum

[$math:sum] -> any — Return the sum of a sequence of numbers.

            [?lib 'cx-stdlib/math']
[$math:sum (1, 2, 3, 4, 5)]
          
            15
          

math:product

[$math:product] -> any — Return the product of a sequence of numbers.

            [?lib 'cx-stdlib/math']
[$math:product (1, 2, 3, 4, 5)]
          
            120
          

math:mean

[$math:mean] -> float — Return the arithmetic mean of a sequence; empty input raises.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:mean (2, 4, 6)] 1]
          
            4.0
          

math:median

[$math:median] -> float — Return the median of a sequence; empty input raises.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:median (3, 1, 2)] 1]
          
            2.0
          

math:mode

[$math:mode] -> any — Return the most-frequent value; ties resolve to the smallest tied value.

            [?lib 'cx-stdlib/math']
[$math:mode (1, 2, 2, 3, 3, 3)]
          
            3
          

math:multimode

[$math:multimode] -> [sequence any] — Return all values tied at the maximum frequency, sorted ascending.

            [?lib 'cx-stdlib/math']
[$math:multimode (5, 5, 2, 2)]
          
            (2, 5)
          

math:stddev

[$math:stddev] -> float — Return the sample standard deviation (N-1) of a sequence.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:stddev (2, 4, 4, 4, 5, 5, 7, 9)] 4]
          
            2.1381
          

math:variance

[$math:variance] -> float — Return the sample variance (N-1) of a sequence.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:variance (2, 4, 4, 4, 5, 5, 7, 9)] 4]
          
            4.5714
          

math:stddev-pop

[$math:stddev-pop] -> float — Return the population standard deviation (N) of a sequence.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:stddev-pop (2, 4, 4, 4, 5, 5, 7, 9)] 1]
          
            2.0
          

math:variance-pop

[$math:variance-pop] -> float — Return the population variance (N) of a sequence.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:variance-pop (2, 4, 4, 4, 5, 5, 7, 9)] 1]
          
            4.0
          

math:percentile

[$math:percentile] -> float — Return the p-th percentile of a sequence, with p in 0..100.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:percentile (1, 2, 3, 4, 5) 50.0] 1]
          
            3.0
          

math:quantile

[$math:quantile] -> float — Return the q-th quantile of a sequence, with q in 0..1.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:quantile (1, 2, 3, 4, 5) 0.5] 1]
          
            3.0
          

math:correlation

[$math:correlation] -> float — Return the Pearson correlation coefficient between two sequences.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:correlation (1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0) (2.0, 4.0, 6.0, 8.0, 10.0, 12.0, 14.0, 16.0, 18.0, 20.0)] 1]
          
            1.0
          

math:covariance

[$math:covariance] -> float — Return the sample covariance (N-1) between two sequences.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:covariance (1.0, 2.0, 3.0, 4.0, 5.0) (2.0, 4.0, 6.0, 8.0, 10.0)] 1]
          
            5.0
          

math:bit-and

[$math:bit-and] -> int — Return the bitwise AND of two 64-bit integers.

            [?lib 'cx-stdlib/math']
[$math:bit-and 255 15]
          
            15
          

math:bit-or

[$math:bit-or] -> int — Return the bitwise OR of two 64-bit integers.

            [?lib 'cx-stdlib/math']
[$math:bit-or 240 15]
          
            255
          

math:bit-xor

[$math:bit-xor] -> int — Return the bitwise XOR of two 64-bit integers.

            [?lib 'cx-stdlib/math']
[$math:bit-xor 255 15]
          
            240
          

math:bit-not

[$math:bit-not] -> int — Return the bitwise complement of a 64-bit integer.

            [?lib 'cx-stdlib/math']
[$math:bit-not 0]
          
            -1
          

math:bit-shift-left

[$math:bit-shift-left] -> int — Shift bits left by n; count of 64 or more saturates to 0, negative count raises.

            [?lib 'cx-stdlib/math']
[$math:bit-shift-left 1 4]
          
            16
          

math:bit-shift-right

[$math:bit-shift-right] -> int — Arithmetic (sign-extending) right shift by n; count of 64 or more saturates, negative count raises.

            [?lib 'cx-stdlib/math']
[$math:bit-shift-right -8 1]
          
            -4
          

math:popcount

[$math:popcount] -> int — Count the set bits in a 64-bit integer.

            [?lib 'cx-stdlib/math']
[$math:popcount 255]
          
            8
          

math:leading-zeros

[$math:leading-zeros] -> int — Count the leading zero bits in a 64-bit integer.

            [?lib 'cx-stdlib/math']
[$math:leading-zeros 1]
          
            63
          

math:trailing-zeros

[$math:trailing-zeros] -> int — Count the trailing zero bits in a 64-bit integer.

            [?lib 'cx-stdlib/math']
[$math:trailing-zeros 8]
          
            3
          

math:is-nan

[$math:is-nan] -> bool — Report whether a value is IEEE 754 NaN.

            [?lib 'cx-stdlib/math']
[$math:is-nan [$math:log -1.0]]
          
            true
          

math:is-infinite

[$math:is-infinite] -> bool — Report whether a value is positive or negative infinity.

            [?lib 'cx-stdlib/math']
[$math:is-infinite [$math:infinity]]
          
            true
          

math:is-finite

[$math:is-finite] -> bool — Report whether a value is finite (not NaN or infinity).

            [?lib 'cx-stdlib/math']
[$math:is-finite 3.5]
          
            true
          

math:is-integer

[$math:is-integer] -> bool — Report whether a value has no fractional part.

            [?lib 'cx-stdlib/math']
[$math:is-integer 4.0]
          
            true
          

math:is-positive

[$math:is-positive] -> bool — Report whether a value is greater than zero.

            [?lib 'cx-stdlib/math']
[$math:is-positive 3]
          
            true
          

math:is-negative

[$math:is-negative] -> bool — Report whether a value is less than zero.

            [?lib 'cx-stdlib/math']
[$math:is-negative -3]
          
            true
          

math:is-zero

[$math:is-zero] -> bool — Report whether a value is exactly zero.

            [?lib 'cx-stdlib/math']
[$math:is-zero 0]
          
            true
          

math:gcd

[$math:gcd] -> int — Return the greatest common divisor of two integers.

            [?lib 'cx-stdlib/math']
[$math:gcd 12 18]
          
            6
          

math:lcm

[$math:lcm] -> int — Return the least common multiple of two integers.

            [?lib 'cx-stdlib/math']
[$math:lcm 4 6]
          
            12
          

math:is-prime

[$math:is-prime] -> bool — Report whether an integer is prime, exact across the full int64 range.

            [?lib 'cx-stdlib/math']
[$math:is-prime 97]
          
            true
          

math:factorial

[$math:factorial] -> int — Return n factorial; n greater than 20 overflows int64 and raises.

            [?lib 'cx-stdlib/math']
[$math:factorial 0]
          
            1
          

math:pi

[$math:pi] -> float — Return the constant pi.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:tan [$math:round-to [$math:pi] 15]] 8]
          
            0.0
          

math:e

[$math:e] -> float — Return Euler's number e.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:log [$math:e]] 8]
          
            1.0
          

math:tau

[$math:tau] -> float — Return the constant tau (2 pi).

            [?lib 'cx-stdlib/math']
[$math:round-to [/ [$math:tau] [$math:pi]] 8]
          
            2.0
          

math:golden

[$math:golden] -> float — Return the golden ratio (1 + sqrt 5) / 2.

            [?lib 'cx-stdlib/math']
[$math:round-to [$math:golden] 8]
          
            1.61803399
          

math:infinity

[$math:infinity] -> float — Return positive IEEE 754 infinity.

            [?lib 'cx-stdlib/math']
[$math:is-infinite [$math:infinity]]
          
            true
          

math:nan

[$math:nan] -> float — Return an IEEE 754 NaN value.

            [?lib 'cx-stdlib/math']
[$math:is-nan [$math:nan]]
          
            true
          

math:epsilon

[$math:epsilon] -> float — Return the float64 machine epsilon.

            [?lib 'cx-stdlib/math']
[$math:is-finite [$math:epsilon]]
          
            true
          

math:wrapping-add

[$math:wrapping-add] -> int — Add two integers with two's-complement wraparound; never raises.

            [?lib 'cx-stdlib/math']
[$math:wrapping-add 9223372036854775807 1]
          
            -9223372036854775808
          

math:wrapping-sub

[$math:wrapping-sub] -> int — Subtract two integers with two's-complement wraparound; never raises.

            [?lib 'cx-stdlib/math']
[$math:wrapping-sub -9223372036854775808 1]
          
            9223372036854775807
          

math:wrapping-mul

[$math:wrapping-mul] -> int — Multiply two integers with two's-complement wraparound; never raises.

            [?lib 'cx-stdlib/math']
[$math:wrapping-mul 6 7]
          
            42
          

math:wrapping-pow

[$math:wrapping-pow] -> int — Raise base to exp with two's-complement wraparound; never raises.

            [?lib 'cx-stdlib/math']
[$math:wrapping-pow 2 10]
          
            1024