stdlib.num¶
Numeric Traits & Operations
Generated from
v0.60.1. 11 source files, 499 documented symbols.
base.xi¶
fn to_base(n: Int, base: Int) -> Str¶
Integer n as a string in the given base (2-36), lowercase digits. n == 0 yields "0"; negatives get a "-" prefix. Invalid base yields "". NOTE: INT_MIN's magnitude (2^63) has no positive i64 representation, so to_base(INT_MIN, base) returns "-" (documented edge). Complexity: O(log_base |n|).
fn from_base(s: Str, base: Int) -> Result[Int, Str]¶
Parses a base-(2-36) string into an Int. An optional leading '-'/'+' is accepted; both digit cases are. Err on invalid base, empty string, an invalid digit, a digit out of range for the base, or overflow. Complexity: O(len(s)).
fn to_base_float(f: Float64, base: Int, prec: Int) -> Str¶
Float f as a base-(2-36) string with prec fraction digits (truncated, not rounded). Handles sign, "inf"/"-inf", and (for the future) "nan". Returns "" for an invalid base/prec or for |f| >= 2^63 (the integer part is then not representable). Complexity: O(prec + log_base |f|).
fn from_base_float(s: Str, base: Int) -> Result[Float64, Str]¶
Parses a base-(2-36) float string ("-1a.2f") into a Float64. An optional sign and a single '.' are accepted; scientific notation is not. Err on invalid base, empty string, bad digits, or multiple decimal points. Complexity: O(len(s)).
fn digits_of(n: Int, base: Int) -> Vec[Int]¶
Digits of |n| in the given base, least significant first (little-endian). n == 0 yields [0]; negatives use the magnitude. Invalid base yields an empty vector. Complexity: O(log_base |n|).
fn from_digits(digits: &Vec[Int], base: Int) -> Int¶
Integer reconstructed from a little-endian digit vector in the given base (inverse of digits_of). Returns 0 on an invalid base, an out-of-range digit, or overflow (documented -- the signature cannot signal errors). Complexity: O(len(digits)).
bigfloat.xi¶
enum RoundMode¶
Rounding mode for BigFloat operations.
NearestUpDownZero
type BigFloat¶
Arbitrary-precision floating-point value.
| Field | Type |
|---|---|
sign |
Bool |
exponent |
Int |
significand |
BigInt |
precision |
Int |
type IntFrac¶
Split result of _split_int_frac: magnitude = q + r / 10^d. PUB (not private): catalog fns returning module-local PRIVATE struct types are degraded to i64 by the checker (see docs/COMPILER_BUGS.md BUG 9) -- the type must be part of the module's public surface.
| Field | Type |
|---|---|
q |
BigInt |
r |
BigInt |
d |
Int |
fn bigfloat_zero() -> BigFloat¶
Constants (constructor functions -- see header note)
fn bigfloat_one() -> BigFloat¶
Constant 1.0 with default precision.
fn bigfloat_two() -> BigFloat¶
Constant 2.0 with default precision.
fn bigfloat_ten() -> BigFloat¶
Constant 10.0 with default precision.
fn bigfloat_half() -> BigFloat¶
Constant 0.5 with default precision.
fn bigfloat_pi() -> BigFloat¶
pi to 100 decimal digits.
fn bigfloat_e() -> BigFloat¶
e to 100 decimal digits.
fn bigfloat_set_round_mode(mode: RoundMode)¶
Rounding mode (thread-local default; whole-value global assignment)
fn bigfloat_get_round_mode() -> RoundMode¶
Current global rounding mode.
fn bigfloat_from_int(n: Int) -> BigFloat¶
Constructors
fn bigfloat_from_float(f: Float64) -> BigFloat¶
Exact to 15 significant digits (the f64 round-trip guarantee); power-of-10 inputs (0.1, 1e300, 3.14) come out exact.
- Precondition:
!xiom.math.is_nan(f) - Precondition:
!xiom.math.is_inf(f)
fn bigfloat_from_str(s: Str) -> Result[BigFloat, Str]¶
Parse decimal strings: "3.14159", "-1e-10", "2.5E+3", ".5", "3.". Exponent range is Int (i64). Result is normalized.
fn bigfloat_from_bigint(b: &BigInt) -> BigFloat¶
Convert a BigInt to BigFloat.
fn bigfloat_with_precision(n: Int, precision: Int) -> BigFloat¶
Value
nwith the given decimal working precision.
- Precondition:
precision >= 1
fn bigfloat_to_str(f: &BigFloat) -> Str¶
Exact decimal representation of the stored value ("shortest round-trip" by construction: the representation IS the decimal).
fn bigfloat_to_str_prec(f: &BigFloat, digits: Int) -> Str¶
Round to
digitssignificant digits (current round mode) then format. Trailing zeros produced by rounding stay significant ("1.41421356237310" is 15 digits); a rounding carry that overflows the digit budget collapses to its normalized form ("1" for 0.999...9 -> 20 digits).
- Precondition:
digits >= 1
fn bigfloat_to_bigint(f: &BigFloat) -> BigInt¶
Truncate toward zero.
fn bigfloat_to_float128(f: &BigFloat) -> Float128¶
Float128 conversion. Same accumulation strategy as bigfloat_to_float64 but in fp128: ~34 significant digits of the significand survive, and the exponent range extends to ~1.1e4932. Values whose exponent exceeds the fp128 range saturate to +/-inf (fp128 IEEE-754 semantics). TODO(compiler): BUG 33 -- Option[Float128] payload unwrap emits a load of the undefined
%struct.Float128(opaque) instead of nativefp128, so the Option-returning form cannot be consumed yet; returns the value directly until the unwrap path is fixed.
fn bigfloat_to_float64(f: &BigFloat) -> Option[Float64]¶
Float64 conversion. None on exponent overflow/underflow beyond f64 range (|value| > ~1.8e308); values underflowing to 0.0 return Some(0.0).
fn bigfloat_is_zero(f: &BigFloat) -> Bool¶
Predicates
fn bigfloat_is_negative(f: &BigFloat) -> Bool¶
True when the value is negative.
fn bigfloat_is_one(f: &BigFloat) -> Bool¶
True when the value equals 1.
fn bigfloat_sign(f: &BigFloat) -> Int¶
-1, 0 or 1 according to the sign.
fn bigfloat_precision(f: &BigFloat) -> Int¶
Working precision in decimal digits.
fn bigfloat_add(a: &BigFloat, b: &BigFloat) -> BigFloat¶
Arithmetic (result precision = max of operand precisions, rounded)
fn bigfloat_sub(a: &BigFloat, b: &BigFloat) -> BigFloat¶
Difference of two BigFloats.
fn bigfloat_mul(a: &BigFloat, b: &BigFloat) -> BigFloat¶
Product of two BigFloats.
fn bigfloat_div(a: &BigFloat, b: &BigFloat) -> BigFloat¶
Truncating long division with guard digits, then rounded to precision. Requires a non-zero divisor.
- Precondition:
!bigfloat_is_zero(b)
fn bigfloat_neg(f: &BigFloat) -> BigFloat¶
Negation.
fn bigfloat_abs(f: &BigFloat) -> BigFloat¶
Absolute value.
fn bigfloat_inv(f: &BigFloat) -> BigFloat¶
1/f with guard digits, rounded to precision.
- Precondition:
!bigfloat_is_zero(f)
fn bigfloat_sqrt(f: &BigFloat) -> BigFloat¶
Square root via Newton on the significand (bigint_sqrt is floor-exact), with 2 guard digits. Requires a non-negative operand.
- Precondition:
!bigfloat_is_negative(f)
fn bigfloat_pow(base: &BigFloat, exp: Int) -> BigFloat¶
Integer power (exp >= 0) by square-and-multiply.
- Precondition:
exp >= 0
fn bigfloat_trunc(f: &BigFloat) -> BigFloat¶
Round toward zero.
fn bigfloat_floor(f: &BigFloat) -> BigFloat¶
Round toward negative infinity.
fn bigfloat_ceil(f: &BigFloat) -> BigFloat¶
Round toward positive infinity.
fn bigfloat_round(f: &BigFloat) -> BigFloat¶
Round to the nearest integer, ties to even (magnitude rounding).
fn bigfloat_fract(f: &BigFloat) -> BigFloat¶
Fractional part with the sign of f: f - trunc(f).
fn bigfloat_with_rounding(f: &BigFloat, mode: RoundMode, digits: Int) -> BigFloat¶
Explicit rounding of
ftodigitssignificant digits withmode.
- Precondition:
digits >= 1
fn bigfloat_compare(a: &BigFloat, b: &BigFloat) -> Int¶
Comparisons
fn bigfloat_eq(a: &BigFloat, b: &BigFloat) -> Bool¶
Exact equality.
fn bigfloat_lt(a: &BigFloat, b: &BigFloat) -> Bool¶
Strictly less than.
fn bigfloat_le(a: &BigFloat, b: &BigFloat) -> Bool¶
Less than or equal.
fn bigfloat_gt(a: &BigFloat, b: &BigFloat) -> Bool¶
Strictly greater than.
fn bigfloat_ge(a: &BigFloat, b: &BigFloat) -> Bool¶
Greater than or equal.
fn bigfloat_pi_with_precision(precision: Int) -> BigFloat¶
pi / e at explicit precision.
- Precondition:
precision >= 1
fn bigfloat_e_with_precision(precision: Int) -> BigFloat¶
Euler's number e to the given decimal precision.
- Precondition:
precision >= 1
fn bigfloat_exp(f: &BigFloat) -> BigFloat¶
exp(x) = exp(r) * 10^k with r = x - k*ln(10) in [-ln(10)/2, ln(10)/2].
fn bigfloat_ln(f: &BigFloat) -> BigFloat¶
ln(x): x = m * 10^k with m in [1, 10); reduce m to [1, sqrt(10)) via one sqrt; ln(x) = 2atanh((m-1)/(m+1)) + kln(10).
- Precondition:
!bigfloat_is_negative(f) - Precondition:
!bigfloat_is_zero(f)
fn bigfloat_log10(f: &BigFloat) -> BigFloat¶
Base-10 logarithm.
- Precondition:
!bigfloat_is_negative(f) - Precondition:
!bigfloat_is_zero(f)
fn bigfloat_sin(f: &BigFloat) -> BigFloat¶
Sine of the value (radians).
fn bigfloat_cos(f: &BigFloat) -> BigFloat¶
Cosine of the value (radians).
fn bigfloat_tan(f: &BigFloat) -> BigFloat¶
Tangent of the value (radians).
fn bigfloat_atan(f: &BigFloat) -> BigFloat¶
atan(x) in [-pi/2, pi/2].
fn bigfloat_atan2(y: &BigFloat, x: &BigFloat) -> BigFloat¶
atan2(y, x) in [-pi, pi].
fn bigfloat_pow_bf(base: &BigFloat, exp: &BigFloat) -> BigFloat¶
base^exp for base >= 0 via exp(exp * ln(base)); negative exponents via inv.
- Precondition:
!bigfloat_is_negative(base)
fn bigfloat_log2(f: &BigFloat) -> BigFloat¶
log2(x) = ln(x) / ln(2).
- Precondition:
!bigfloat_is_negative(f) - Precondition:
!bigfloat_is_zero(f)
fn bigfloat_exp2(f: &BigFloat) -> BigFloat¶
exp2(x) = exp(x * ln(2)).
fn bigfloat_cbrt(f: &BigFloat) -> BigFloat¶
Cube root (Newton: x = (2x + n/x^2)/3). Works for negative operands via sign symmetry; the significand is scaled to a multiple-of-3 exponent so the final 10^(exp/3) shift is exact.
fn bigfloat_hypot(a: &BigFloat, b: &BigFloat) -> BigFloat¶
hypot(a, b) = sqrt(a^2 + b^2).
fn bigfloat_sinh(f: &BigFloat) -> BigFloat¶
Hyperbolic functions via exp.
fn bigfloat_cosh(f: &BigFloat) -> BigFloat¶
Hyperbolic cosine.
fn bigfloat_tanh(f: &BigFloat) -> BigFloat¶
Hyperbolic tangent.
fn bigfloat_asin(f: &BigFloat) -> BigFloat¶
asin(x) = atan(x / sqrt(1 - x^2)); asin(+-1) = +-pi/2.
fn bigfloat_acos(f: &BigFloat) -> BigFloat¶
acos(x) = pi/2 - asin(x). Exact endpoints: acos(1) = 0, acos(-1) = pi.
fn bigfloat_asinh(f: &BigFloat) -> BigFloat¶
asinh(x) = ln(x + sqrt(x^2 + 1)).
fn bigfloat_acosh(f: &BigFloat) -> BigFloat¶
acosh(x) = ln(x + sqrt(x^2 - 1)); requires x >= 1.
- Precondition:
!bigfloat_lt(f, &bigfloat_one())
fn bigfloat_atanh(f: &BigFloat) -> BigFloat¶
atanh(x) = ln((1 + x)/(1 - x)) / 2; requires |x| < 1.
- Precondition:
bigfloat_lt(&bigfloat_abs(f), &bigfloat_one())
fn bigfloat_to_str_sci(f: &BigFloat, digits: Int) -> Str¶
Scientific notation: d.ddd...e[+-]k with
digitssignificant digits. Zero renders as "0".
- Precondition:
digits >= 1
fn bigfloat_from_ratio(n: Int, d: Int) -> BigFloat¶
Exact rational n/d at the default precision.
- Precondition:
d != 0
fn bigfloat_pow10(f: &BigFloat, n: Int) -> BigFloat¶
Exact x * 10^n (pure exponent shift; no rounding).
fn bigfloat_floor_int(f: &BigFloat) -> Result[Int, Str]¶
Integer-valued helpers (range-checked to i64).
fn bigfloat_ceil_int(f: &BigFloat) -> Result[Int, Str]¶
Convert to Int after ceiling; Err when out of range.
fn bigfloat_round_int(f: &BigFloat) -> Result[Int, Str]¶
Convert to Int after rounding; Err when out of range.
fn bigfloat_trunc_int(f: &BigFloat) -> Result[Int, Str]¶
Convert to Int after truncation; Err when out of range.
bigfloat_agg.xi¶
bigint.xi¶
type BigInt¶
Type -- BigInt
| Field | Type |
|---|---|
digits |
Vec[Int] |
negative |
Bool |
fn bigint_from_int(n: Int) -> BigInt¶
Construction -- from Int, from Str
fn bigint_from_u64(n: UInt64) -> BigInt¶
Build from an unsigned 64-bit value. The compiler emits SIGNED LLVM instructions for UInt64
/and%, so the proven unsigned helpers from xiom.num (u64_div_floor / u64_mod_euclid) are used instead -- exact for the full 0 .. 2^64-1 range.
fn bigint_from_str(s: Str) -> Result[BigInt, Str]¶
Parse decimal string. Uses xiom.string.str_slice for char access. O(n2) due to repeated multiply-by-10-and-add during accumulation.
fn bigint_to_str(b: &BigInt) -> Str¶
Convert to decimal string via repeated division by BASE.
fn bigint_add(a: &BigInt, b: &BigInt) -> BigInt¶
Core arithmetic -- add, sub, mul, div_mod
fn bigint_sub(a: &BigInt, b: &BigInt) -> BigInt¶
Difference of two BigInts.
fn bigint_mul(a: &BigInt, b: &BigInt) -> BigInt¶
Product of two BigInts.
fn bigint_div_mod(a: &BigInt, b: &BigInt) -> (BigInt, BigInt)¶
Truncating division: returns (quotient, remainder).
fn bigint_compare(a: &BigInt, b: &BigInt) -> Int¶
Comparison, zero, sign, abs, neg
fn bigint_is_zero(b: &BigInt) -> Bool¶
True when the value is zero.
fn bigint_abs(b: &BigInt) -> BigInt¶
Absolute value.
fn bigint_neg(b: &BigInt) -> BigInt¶
Negation.
fn bigint_sign(b: &BigInt) -> Int¶
-1, 0 or 1 according to the sign.
fn bigint_mod(a: &BigInt, m: &BigInt) -> BigInt¶
Modular arithmetic and advanced operations
fn bigint_pow(base: &BigInt, exp: Int) -> BigInt¶
baseraised to a non-negative Int exponent.
fn bigint_gcd(a: &BigInt, b: &BigInt) -> BigInt¶
Greatest common divisor (non-negative).
fn bigint_shift_left(b: &BigInt, shift: Int) -> BigInt¶
Shift left by
shiftbits (a negative shift moves right).
fn bigint_zero() -> BigInt¶
-- Constants --------------------------------------------------------------- NOTE (2026-08-10): module-global initializers cannot call functions (the compiler silently leaves them zero -- see docs/COMPILER_BUGS.md), so the spec constants BIGINT_ZERO/ONE/TEN are exposed as pure constructors that return a fresh value. Zero-cost, immutable by construction.
fn bigint_one() -> BigInt¶
Constant 1 as BigInt.
fn bigint_ten() -> BigInt¶
Constant 10 as BigInt.
fn bigint_two() -> BigInt¶
Constant 2 as BigInt.
fn bigint_from_base(s: Str, base: Int) -> Result[BigInt, Str]¶
Parse a string in base 2..36 (optional leading - or +).
- Precondition:
base >= 2 && base <= 36
fn bigint_from_hex(s: Str) -> Result[BigInt, Str]¶
Parse hexadecimal ("ff", "-1a"). Case-insensitive; no "0x" prefix.
fn bigint_to_base(b: &BigInt, base: Int) -> Str¶
Convert to a string in base 2..36 (digits 0-9, A-Z; "-" prefix for negatives). Returns "" for an invalid base.
fn bigint_to_hex(b: &BigInt) -> Str¶
Lowercase hexadecimal (matches the parse examples "ff"/"-1a").
fn bigint_to_int(b: &BigInt) -> Result[Int, Str]¶
Range-checked conversion to Int (i64). Err on overflow.
fn bigint_to_u64(b: &BigInt) -> Result[UInt64, Str]¶
Convert to UInt64 (0 .. 2^64-1). Negative or >= 2^64 -> Err.
fn bigint_to_u128(b: &BigInt) -> Result[UInt128, Str]¶
Convert to UInt128 (0 .. 2^128-1). Negative or >= 2^128 -> Err.
fn bigint_to_i128(b: &BigInt) -> Result[Int128, Str]¶
Convert to Int128 (-2^127 .. 2^127-1). Out of range -> Err.
fn bigint_is_one(b: &BigInt) -> Bool¶
-- Predicates ---------------------------------------------------------------
fn bigint_is_even(b: &BigInt) -> Bool¶
True when the value is even.
fn bigint_is_odd(b: &BigInt) -> Bool¶
True when the value is odd.
fn bigint_is_negative(b: &BigInt) -> Bool¶
True when the value is negative.
fn bigint_div(a: &BigInt, b: &BigInt) -> BigInt¶
Truncating division (quotient of bigint_div_mod).
- Precondition:
!bigint_is_zero(b)
fn bigint_pow_mod(base: &BigInt, exp: &BigInt, m: &BigInt) -> BigInt¶
Modular exponentiation: (base^exp) mod m. exp >= 0, m != 0. Square-and-multiply.
- Precondition:
!bigint_is_zero(m) - Precondition:
!bigint_is_negative(exp)
fn bigint_sqrt(b: &BigInt) -> BigInt¶
Integer square root (floor): Newton's method with a decimal-digit-based initial guess 10^ceil(D/2) >= sqrt(n). Quadratic convergence.
- Precondition:
!bigint_is_negative(b)
fn bigint_sqrt_rem(b: &BigInt) -> (BigInt, BigInt)¶
(floor sqrt, n - sqrt^2).
- Precondition:
!bigint_is_negative(b)
fn bigint_lcm(a: &BigInt, b: &BigInt) -> BigInt¶
Least common multiple. lcm(0, x) == 0.
fn bigint_ext_gcd(a: &BigInt, b: &BigInt) -> (BigInt, BigInt, BigInt)¶
Extended Euclidean algorithm: returns (g, x, y) with ax + by == g, g = gcd(|a|, |b|) > 0.
fn bigint_is_prime(b: &BigInt) -> Bool¶
Miller-Rabin primality test. Deterministic for n < 3.3e24 (bases 2..37), probabilistic (error < 4^-rounds) above.
fn bigint_next_prime(b: &BigInt) -> BigInt¶
Smallest prime strictly greater than b. next_prime(1) == 2.
fn bigint_factorial(n: Int) -> BigInt¶
n! for n >= 0. O(n) BigInt multiplications.
- Precondition:
n >= 0
fn bigint_binomial(n: Int, k: Int) -> BigInt¶
C(n, k) for 0 <= k <= n. Multiplicative formula; every intermediate division is exact.
- Precondition:
n >= 0 - Precondition:
k >= 0 - Precondition:
k <= n
fn bigint_fibonacci(n: Int) -> BigInt¶
F(n): F(0)=0, F(1)=1. Iterative, O(n) BigInt additions.
- Precondition:
n >= 0
fn bigint_bit_and(a: &BigInt, b: &BigInt) -> BigInt¶
Bitwise AND on the two's-complement limbs.
fn bigint_bit_or(a: &BigInt, b: &BigInt) -> BigInt¶
Bitwise OR on the two's-complement limbs.
fn bigint_bit_xor(a: &BigInt, b: &BigInt) -> BigInt¶
Bitwise XOR on the two's-complement limbs.
fn bigint_shift_right(b: &BigInt, n: Int) -> BigInt¶
Arithmetic (floor) right shift by n bits: b >> n. For negative b this rounds toward -inf (true arithmetic shift).
- Precondition:
n >= 0
fn bigint_popcount(b: &BigInt) -> Int¶
Number of set bits in |b| (well-defined for all signs).
fn bigint_bit_len(b: &BigInt) -> Int¶
Bits needed to represent |b|; 0 for zero.
fn bigint_eq(a: &BigInt, b: &BigInt) -> Bool¶
-- Comparison wrappers ------------------------------------------------------
fn bigint_lt(a: &BigInt, b: &BigInt) -> Bool¶
Strictly less than.
fn bigint_le(a: &BigInt, b: &BigInt) -> Bool¶
Less than or equal.
fn bigint_gt(a: &BigInt, b: &BigInt) -> Bool¶
Strictly greater than.
fn bigint_ge(a: &BigInt, b: &BigInt) -> Bool¶
Greater than or equal.
convert.xi¶
fn to_base58(n: Int) -> Str¶
Base58 (Bitcoin alphabet) encoding of
n.
fn from_base58(s: Str) -> Option[Int]¶
Decode base58; None on invalid characters or overflow.
fn to_base62(n: Int) -> Str¶
Converts an integer to its Base62 representation ("0-9A-Za-z"). n == 0 yields "0". Negative numbers get a '-' prefix. Complexity: O(log_62 n).
fn from_base62(s: Str) -> Option[Int]¶
Parses a Base62 string back into an integer. Returns None on invalid characters, overflow, or an empty string. An optional leading '-'/'+' is accepted. Complexity: O(n).
fn to_ascii85(data: &Vec[UInt8]) -> Str¶
Adobe Ascii85 encoding of the bytes (see also xiom.convert.ascii85).
fn from_ascii85(s: Str) -> Option[Vec[UInt8]]¶
Decode Ascii85 into bytes; None on malformed input.
fn to_roman(n: Int) -> Option[Str]¶
Roman numeral for 1..3999, or None outside that range.
fn from_roman(s: Str) -> Option[Int]¶
Parse a Roman numeral; None on invalid input.
float.xi¶
fn float_bits(f: Float64) -> Int¶
Raw 64-bit IEEE-754 bit pattern of f. FALLBACK (TODO(compiler): needs bitcast intrinsic): returns 0 until one lands. Do not rely on the value.
fn bits_to_float(bits: Int) -> Float64¶
Float64 reconstructed from a raw 64-bit IEEE-754 bit pattern. FALLBACK (TODO(compiler): needs bitcast intrinsic): returns 0.0 until one lands. Do not rely on the value.
fn float_mantissa(f: Float64) -> Int¶
The significand of f as an integer (implicit leading bit included for normal values; no implicit bit for subnormals). Zero, NaN, and infinities map to 0 (documented). Exact: computed by scaling |f| by exact powers of two until it lies in [1, 2), then multiplying by 2^52 (or 2^(e+1074) for subnormals). Complexity: O(|exponent|) -- at most ~1074 iterations.
fn float_exponent(f: Float64) -> Int¶
Unbiased binary exponent of |f|: the unique e with 2^e <= |f| < 2^(e+1). Zero, NaN, and infinities map to 0 (documented; IEEE's stored exponent of zero would be -1023, but 0 is the conventional frexp-style result). Exact via repeated halving/doubling. Complexity: O(|e|) -- at most ~1074 iterations.
fn float_is_subnormal(f: Float64) -> Bool¶
True iff f is a subnormal value: 0 < |f| < 2^-1022 (the minimum normal). Zero, NaN, and infinities are not subnormal. Exact via comparison. Complexity: O(1).
fn float_is_nan(f: Float64) -> Bool¶
True iff f is NaN (f != f is the IEEE identity). Complexity: O(1).
fn float_is_infinite(f: Float64) -> Bool¶
True iff f is +inf or -inf (checked against 1.0/0.0 and -1.0/0.0). Complexity: O(1).
fn float_next_up(f: Float64) -> Float64¶
Smallest Float64 strictly greater than f. FALLBACK (TODO(compiler): needs bitcast intrinsic): returns f unchanged until intrinsic lands (exact next-up needs float_bits). Do not rely on the value.
fn float_next_down(f: Float64) -> Float64¶
Largest Float64 strictly less than f. FALLBACK (TODO(compiler): needs bitcast intrinsic): returns f unchanged until intrinsic lands. Do not rely on the value.
fn float_ulp(f: Float64) -> Float64¶
Unit in the last place of f: the distance to the next representable value. FALLBACK (TODO(compiler): needs bitcast intrinsic): returns 0.0 until one lands. Do not rely on the value.
fn float_classify(f: Float64) -> Str¶
Classification string: "nan", "inf", "-inf", "subnormal", "zero", or "normal" (checked in that order). The "nan" branch uses f != f (IEEE). Complexity: O(1).
fraction.xi¶
type Fraction¶
A rational number num/den with den > 0, always in lowest terms.
| Field | Type |
|---|---|
num |
Int |
den |
Int |
fn fraction_new(num: Int, den: Int) -> Fraction¶
Constructs a reduced fraction from num/den, normalizing the sign to the denominator. den == 0 returns the zero fraction 0/1 (documented fallback for the requires-clause violation). Result satisfies the module invariant. Complexity: O(log max(|num|,|den|)).
- Precondition:
den != 0 - Postcondition:
result.den > 0
fn fraction_from_float(f: Float64) -> Fraction¶
Best rational approximation of f via continued-fraction convergents. The first convergent that would overflow i64 (or the first exact one) is returned. Zero, NaN, and infinities map to 0/1 (documented). |f| must be < 2^63 for the floor cast; larger magnitudes return the current convergent. Complexity: O(log |f|) iterations.
fn fraction_add(a: Fraction, b: Fraction) -> Fraction¶
a + b. Pre-reduces by gcd(a.den, b.den) to limit overflow; the result is re-reduced. Cross-products may still overflow i64 for very large denominators (documented). Complexity: O(log max(a.den, b.den)).
fn fraction_sub(a: Fraction, b: Fraction) -> Fraction¶
a - b. Complexity: O(log max(a.den, b.den)).
fn fraction_mul(a: Fraction, b: Fraction) -> Fraction¶
a * b. Cross-cancels via gcd before multiplying, minimizing overflow. Complexity: O(log max(|num|, den)).
fn fraction_div(a: Fraction, b: Fraction) -> Option[Fraction]¶
a / b as a / (b^-1). None when b is zero (no silent division by zero). Complexity: O(log max(|num|, den)).
fn fraction_reduce(f: Fraction) -> Fraction¶
Reduces f to lowest terms with a positive denominator. Identity when f already satisfies the module invariant. Complexity: O(log max(|num|,|den|)).
fn fraction_to_float(f: Fraction) -> Float64¶
Converts to Float64 (numerator / denominator division). A zero denominator (invariant violation) returns 0.0 (documented). Complexity: O(1).
fn fraction_to_str(f: Fraction) -> Str¶
Renders as "num/den". Complexity: O(1) string building.
fn fraction_is_zero(f: Fraction) -> Bool¶
Returns true iff the numerator is zero. Complexity: O(1).
fn fraction_compare(a: Fraction, b: Fraction) -> Int¶
Three-way comparison via gcd-reduced cross-multiplication: -1, 0, or 1. Valid because denominators are positive. Cross-products can overflow i64 for large fractions (documented). Complexity: O(log max(den)).
num.xi¶
fn min_value[T]() -> T¶
Minimum finite value of a bounded numeric type.
fn max_value[T]() -> T¶
Maximum finite value of a bounded numeric type.
fn epsilon[T]() -> T¶
Difference between 1.0 and the next representable value.
fn gcd(a: Int, b: Int) -> Int¶
Integer-specific
- Postcondition:
result >= 0 - Postcondition:
a == 0 && b == 0 => result == 0
fn lcm(a: Int, b: Int) -> Int¶
Least common multiple of two Ints (0 when either is 0).
fn is_power_of_two(n: Int) -> Bool¶
True when
n > 0andnis a power of two.
fn next_power_of_two(n: Int) -> Int¶
Smallest power of two >= n (1 for n <= 1).
fn count_ones(n: Int) -> Int¶
Number of set bits (popcount).
- Precondition:
true
fn count_zeros(n: Int) -> Int¶
Number of cleared bits.
fn leading_zeros(n: Int) -> Int¶
Number of leading zero bits.
- Precondition:
true
fn trailing_zeros(n: Int) -> Int¶
Number of trailing zero bits.
- Precondition:
true
fn rotate_left(n: Int, k: Int) -> Int¶
Rotate bits left by
kpositions.
fn rotate_right(n: Int, k: Int) -> Int¶
Rotate bits right by
kpositions.
fn reverse_bits(n: Int) -> Int¶
Reverse the bit order of the value.
fn to_be(n: Int) -> Int¶
Convert from native to big-endian byte order.
fn to_le(n: Int) -> Int¶
Convert from native to little-endian byte order.
fn from_be(n: Int) -> Int¶
Convert from big-endian to native byte order.
fn from_le(n: Int) -> Int¶
Convert from little-endian to native byte order.
fn is_finite(x: Float64) -> Bool¶
Float-specific
fn is_normal(x: Float64) -> Bool¶
True when
xis finite and neither zero nor subnormal.
fn classify(x: Float64) -> Int¶
IEEE-754 classification code (see the FLOAT_* constants).
fn floor(x: Float64) -> Int¶
Largest integer not greater than
x.
- Postcondition:
to_float(result) <= x && x < to_float(result) + 1
fn ceil(x: Float64) -> Int¶
Smallest integer not less than
x.
- Postcondition:
to_float(result) - 1 < x && x <= to_float(result)
fn round(x: Float64) -> Int¶
Nearest integer, with halves rounded away from zero.
fn trunc(x: Float64) -> Int¶
Integer part of
x(truncation toward zero).
fn fract(x: Float64) -> Float64¶
Fractional part of
x(x - trunc(x)).
fn recip(x: Float64) -> Float64¶
Reciprocal
1 / x.
- Precondition:
x != 0
fn to_degrees(rad: Float64) -> Float64¶
Convert radians to degrees.
fn to_radians(deg: Float64) -> Float64¶
Convert degrees to radians.
fn hypot(x: Float64, y: Float64) -> Float64¶
Length of the hypotenuse
sqrt(x^2 + y^2)without intermediate overflow.
- Precondition:
true
fn saturating_add[T](a: T, b: T) -> T¶
Saturation arithmetic
fn saturating_sub[T](a: T, b: T) -> T¶
Subtraction clamped to the type minimum/maximum.
fn saturating_mul[T](a: T, b: T) -> T¶
Multiplication clamped to the type minimum/maximum.
fn checked_add[T](a: T, b: T) -> Option[T]¶
Checked arithmetic
fn checked_sub[T](a: T, b: T) -> Option[T]¶
Subtraction, or None on overflow/underflow.
fn checked_mul[T](a: T, b: T) -> Option[T]¶
Multiplication, or None on overflow.
fn checked_div[T](a: T, b: T) -> Option[T]¶
Division, or None on division by zero/overflow.
- Postcondition:
b == zero() => result is None
fn wrapping_add[T](a: T, b: T) -> T¶
Wrapping arithmetic
fn wrapping_sub[T](a: T, b: T) -> T¶
Subtraction wrapping around the type range.
fn wrapping_mul[T](a: T, b: T) -> T¶
Multiplication wrapping around the type range.
fn parse_int(s: Str) -> Result[Int, Str]¶
Parse
fn parse_float(s: Str) -> Result[Float64, Str]¶
Parse a decimal Float64; Err with a message on bad input.
fn parse_int_radix(s: Str, radix: Int) -> Result[Int, Str]¶
Parse an Int in the given radix (2..36); Err on bad input.
- Precondition:
s.len() > 0 - Precondition:
2 <= radix && radix <= 36
fn is_even(n: Int) -> Bool¶
Returns true if n is even. O(1).
fn is_odd(n: Int) -> Bool¶
Returns true if n is odd. O(1).
fn is_positive(n: Int) -> Bool¶
Returns true if n > 0. O(1).
fn is_negative(n: Int) -> Bool¶
Returns true if n < 0. O(1).
fn is_non_negative(n: Int) -> Bool¶
Returns true if n >= 0. O(1).
fn signum(n: Int) -> Int¶
Returns -1 for negative, 0 for zero, 1 for positive. O(1).
fn digit_count(n: Int) -> Int¶
Counts the number of decimal digits. O(log10 n).
fn digit_sum(n: Int) -> Int¶
Sum of decimal digits. O(log10 n).
fn digital_root(n: Int) -> Int¶
Digital root: repeated digit sum until a single digit is obtained. O(log10 n).
fn factorial(n: Int) -> Int¶
Factorial of n (n!). Returns 0 on overflow or negative input. O(N).
fn binomial(n: Int, k: Int) -> Int¶
Binomial coefficient C(n, k). Returns 0 on overflow or invalid input. O(k).
fn fibonacci(n: Int) -> Int¶
Fibonacci number F(n). 0-indexed: F(0)=0, F(1)=1. Returns 0 for n < 0. O(N).
fn gcd_many(nums: &Vec[Int]) -> Int¶
GCD of a slice of integers. Returns 0 if the slice is empty. O(N-log max).
fn lcm_many(nums: &Vec[Int]) -> Int¶
LCM of a slice of integers. Returns 0 if any element is 0. O(N-log max).
fn is_prime(n: Int) -> Bool¶
Trial-division primality test. O(sqrtn).
fn next_prime(n: Int) -> Int¶
Next prime greater than n. O(sqrtresult - gap).
fn nth_prime(n: Int) -> Int¶
Nth prime (1-indexed: nth_prime(1)=2). Returns 0 for n <= 0. O(n-sqrtpn).
fn prime_factors(n: Int) -> Vec[Int]¶
Prime factors of n (with multiplicity). O(sqrtn).
fn divisors(n: Int) -> Vec[Int]¶
All positive divisors of n (unsorted). O(sqrtn).
fn euler_totient(n: Int) -> Int¶
Euler's totient phi(n): count of k in [1, n] with gcd(k, n) = 1. O(sqrtn).
fn mod_pow(base: Int, exp: Int, m: Int) -> Int¶
Modular exponentiation: (base^exp) mod m. Uses square-and-multiply. O(log exp).
fn mod_inverse(a: Int, m: Int) -> Option[Int]¶
Modular inverse: x such that (a * x) == 1 (mod m). Uses extended Euclid. O(log min(a,m)). Returns None if gcd(a, m) != 1.
fn mod_add(a: Int, b: Int, m: Int) -> Int¶
Modular addition: (a + b) mod m. Result in [0, m). O(1).
fn mod_sub(a: Int, b: Int, m: Int) -> Int¶
Modular subtraction: (a - b) mod m. Result in [0, m). O(1).
fn mod_mul(a: Int, b: Int, m: Int) -> Int¶
Modular multiplication: (a * b) mod m. Result in [0, m). O(1).
fn is_perfect_square(n: Int) -> Bool¶
Returns true if n is a perfect square. O(log n).
fn is_palindrome_int(n: Int) -> Bool¶
Returns true if n reads the same forward and backward in decimal. O(log10 n).
fn reverse_int(n: Int) -> Int¶
Reverses the decimal digits of n. Sign is preserved. O(log10 n).
fn to_base(n: Int, base: Int) -> Str¶
Convert integer to string in given base (2-36). Uses digits 0-9, A-Z. Returns empty string for invalid base. O(log_base n).
fn from_base(s: Str, base: Int) -> Option[Int]¶
Parse integer from string in given base (2-36). Delegates to parse_int_radix. Returns None on invalid input or overflow. O(N).
type Fraction¶
-- Rational number (num / den, den > 0, always reduced) -------------------
| Field | Type |
|---|---|
num |
Int |
den |
Int |
type U64DivRem¶
Internal: unsigned 64-bit division result.
| Field | Type |
|---|---|
quot |
UInt64 |
rem |
UInt64 |
type I128DivRem¶
Internal: Int128 division by a small scalar.
| Field | Type |
|---|---|
quot |
Int128 |
rem |
Int |
fn f64_round(x: Float64) -> Float64¶
Rounds x to the nearest Float64, halves away from zero (round(2.5) == 3.0, round(-2.5) == -3.0). NaN propagates. Complexity: O(1).
- Precondition:
true
fn f64_floor(x: Float64) -> Float64¶
Largest integral Float64 <= x. NaN propagates. Complexity: O(1).
fn f64_ceil(x: Float64) -> Float64¶
Smallest integral Float64 >= x. NaN propagates. Complexity: O(1).
fn f64_trunc(x: Float64) -> Float64¶
Truncates x toward zero, returning the integral part as Float64. NaN propagates. Complexity: O(1).
fn f64_fract(x: Float64) -> Float64¶
Fractional part of x with the sign of x: x - trunc(x). fract(2.75) == 0.75, fract(-2.75) == -0.75. NaN propagates. Complexity: O(1).
fn f64_modf_int_part(x: Float64) -> Float64¶
Integral part of x (C
modfsplit, matches f64_fract's sign convention). Equivalent to f64_trunc. Complexity: O(1).
fn f64_modf_frac_part(x: Float64) -> Float64¶
Fractional part of x (C
modfsplit, sign of x). Equivalent to f64_fract. Complexity: O(1).
fn f32_round(x: Float32) -> Float32¶
Rounds x to the nearest Float32, halves away from zero. NaN propagates. Complexity: O(1).
- Precondition:
true
fn f32_floor(x: Float32) -> Float32¶
Largest integral Float32 <= x. NaN propagates. Complexity: O(1).
fn f32_ceil(x: Float32) -> Float32¶
Smallest integral Float32 >= x. NaN propagates. Complexity: O(1).
fn f32_trunc(x: Float32) -> Float32¶
Truncates x toward zero as Float32. NaN propagates. Complexity: O(1).
fn f32_fract(x: Float32) -> Float32¶
Fractional part of x with the sign of x. NaN propagates. Complexity: O(1).
fn f32_modf_int_part(x: Float32) -> Float32¶
Integral part of x (C
modfsplit). Equivalent to f32_trunc. Complexity: O(1).
fn f32_modf_frac_part(x: Float32) -> Float32¶
Fractional part of x (C
modfsplit, sign of x). Complexity: O(1).
fn f64_round_to_int(x: Float64) -> Int¶
Rounds x to the nearest Int, halves away from zero. NaN -> 0, out of i64 range -> saturated INT_MAX/INT_MIN. Complexity: O(1).
fn f64_floor_to_int(x: Float64) -> Int¶
Floor of x as Int. NaN -> 0, out of i64 range -> saturated INT_MAX/INT_MIN. Complexity: O(1).
fn f64_ceil_to_int(x: Float64) -> Int¶
Ceiling of x as Int. NaN -> 0, out of i64 range -> saturated INT_MAX/INT_MIN. Complexity: O(1).
fn f64_trunc_to_int(x: Float64) -> Int¶
Truncation of x toward zero as Int. NaN -> 0, out of i64 range -> saturated INT_MAX/INT_MIN. Complexity: O(1).
fn f32_round_to_int(x: Float32) -> Int¶
Float32 variant of f64_round_to_int. Complexity: O(1).
fn f32_floor_to_int(x: Float32) -> Int¶
Float32 variant of f64_floor_to_int. Complexity: O(1).
fn f32_ceil_to_int(x: Float32) -> Int¶
Float32 variant of f64_ceil_to_int. Complexity: O(1).
fn f32_trunc_to_int(x: Float32) -> Int¶
Float32 variant of f64_trunc_to_int. Complexity: O(1).
fn i64_div_floor(a: Int, b: Int) -> Int¶
Floor division: largest Int <= a/b (rounds toward -inf). floor(-7, 2) == -4. Complexity: O(1).
fn i64_div_ceil(a: Int, b: Int) -> Int¶
Ceiling division: smallest Int >= a/b (rounds toward +inf). ceil(-7, 2) == -3. Complexity: O(1).
fn i64_div_round(a: Int, b: Int) -> Int¶
Division rounding to nearest, halves away from zero, using unsigned magnitude arithmetic so the result is exact for the full i64 range (saturating to INT_MAX/INT_MIN only for the unrepresentable +2^63). Complexity: O(64) via u64_div_mod.
fn i64_mod_euclid(a: Int, b: Int) -> Int¶
Euclidean remainder: r >= 0 always, r == a (mod b). mod_euclid(-7, 2) == 1. Division by zero returns 0. Complexity: O(1).
fn i64_div_euclid(a: Int, b: Int) -> Int¶
Euclidean division: q = floor(a/b) (see i64_div_floor). Complexity: O(1).
fn u64_div_floor(a: UInt64, b: UInt64) -> UInt64¶
Unsigned floor division (identical to plain unsigned division). Division by zero returns 0. Complexity: O(64).
fn u64_div_ceil(a: UInt64, b: UInt64) -> UInt64¶
Unsigned ceiling division. Division by zero returns 0. Complexity: O(64).
fn u64_mod_euclid(a: UInt64, b: UInt64) -> UInt64¶
Unsigned Euclidean remainder (= plain unsigned remainder). Division by zero returns 0. Complexity: O(64).
fn u64_div_euclid(a: UInt64, b: UInt64) -> UInt64¶
Unsigned Euclidean division (identical to u64_div_floor). Complexity: O(64).
fn i64_add_checked(a: Int, b: Int) -> Option[Int]¶
a + b with overflow detection. Some(sum) on success, None on overflow. Complexity: O(1).
fn i64_sub_checked(a: Int, b: Int) -> Option[Int]¶
a - b with overflow detection. Some(diff) on success, None on overflow. Complexity: O(1).
fn i64_mul_checked(a: Int, b: Int) -> Option[Int]¶
a * b with overflow detection. Some(product) on success, None on overflow. Handles the +/-1 edge cases explicitly. Complexity: O(1).
fn i64_div_checked(a: Int, b: Int) -> Option[Int]¶
a / b with div-by-zero and INT_MIN / -1 overflow detection. Complexity: O(1).
fn i64_neg_checked(a: Int) -> Option[Int]¶
-a with overflow detection (INT_MIN has no positive inverse). Complexity: O(1).
fn i64_pow_checked(base: Int, exp: Int) -> Option[Int]¶
base^exp with overflow detection via square-and-multiply. Negative exponent returns None. Complexity: O(log exp).
fn i32_add_checked(a: Int32, b: Int32) -> Option[Int32]¶
i32 a + b. Some on success, None on overflow. Complexity: O(1).
fn i32_sub_checked(a: Int32, b: Int32) -> Option[Int32]¶
i32 a - b. Some on success, None on overflow. Complexity: O(1).
fn i32_mul_checked(a: Int32, b: Int32) -> Option[Int32]¶
i32 a * b (i64 product is exact for all i32 inputs). None on overflow. Complexity: O(1).
fn i32_div_checked(a: Int32, b: Int32) -> Option[Int32]¶
i32 a / b. None on div-by-zero or INT32_MIN / -1. Complexity: O(1).
fn i16_add_checked(a: Int16, b: Int16) -> Option[Int16]¶
i16 a + b. None on overflow. Complexity: O(1).
fn i16_sub_checked(a: Int16, b: Int16) -> Option[Int16]¶
i16 a - b. None on overflow. Complexity: O(1).
fn i16_mul_checked(a: Int16, b: Int16) -> Option[Int16]¶
i16 a * b. None on overflow. Complexity: O(1).
fn i16_div_checked(a: Int16, b: Int16) -> Option[Int16]¶
i16 a / b. None on div-by-zero or INT16_MIN / -1. Complexity: O(1).
fn i8_add_checked(a: Int8, b: Int8) -> Option[Int8]¶
i8 a + b. None on overflow. Complexity: O(1).
fn i8_sub_checked(a: Int8, b: Int8) -> Option[Int8]¶
i8 a - b. None on overflow. Complexity: O(1).
fn i8_mul_checked(a: Int8, b: Int8) -> Option[Int8]¶
i8 a * b. None on overflow. Complexity: O(1).
fn i8_div_checked(a: Int8, b: Int8) -> Option[Int8]¶
i8 a / b. None on div-by-zero or INT8_MIN / -1. Complexity: O(1).
fn u64_add_checked(a: UInt64, b: UInt64) -> Option[UInt64]¶
u64 a + b using unsigned wrap detection. None on overflow. Complexity: O(1).
fn u64_sub_checked(a: UInt64, b: UInt64) -> Option[UInt64]¶
u64 a - b. None on underflow (a < b). Complexity: O(1).
fn u64_mul_checked(a: UInt64, b: UInt64) -> Option[UInt64]¶
u64 a * b via the exact 128-bit product; None when the high limb is non-zero. Complexity: O(1).
fn u64_div_checked(a: UInt64, b: UInt64) -> Option[UInt64]¶
u64 a / b. None on div-by-zero. Complexity: O(64).
fn u32_add_checked(a: UInt32, b: UInt32) -> Option[UInt32]¶
u32 a + b. None on overflow. Complexity: O(1).
fn u32_sub_checked(a: UInt32, b: UInt32) -> Option[UInt32]¶
u32 a - b. None on underflow. Complexity: O(1).
fn u32_mul_checked(a: UInt32, b: UInt32) -> Option[UInt32]¶
u32 a * b (i64 product is exact for all u32 inputs). None on overflow. Complexity: O(1).
fn u32_div_checked(a: UInt32, b: UInt32) -> Option[UInt32]¶
u32 a / b. None on div-by-zero. Complexity: O(1).
fn u16_add_checked(a: UInt16, b: UInt16) -> Option[UInt16]¶
u16 a + b. None on overflow. Complexity: O(1).
fn u16_sub_checked(a: UInt16, b: UInt16) -> Option[UInt16]¶
u16 a - b. None on underflow. Complexity: O(1).
fn u16_mul_checked(a: UInt16, b: UInt16) -> Option[UInt16]¶
u16 a * b. None on overflow. Complexity: O(1).
fn u16_div_checked(a: UInt16, b: UInt16) -> Option[UInt16]¶
u16 a / b. None on div-by-zero. Complexity: O(1).
fn u8_add_checked(a: UInt8, b: UInt8) -> Option[UInt8]¶
u8 a + b. None on overflow. Complexity: O(1).
fn u8_sub_checked(a: UInt8, b: UInt8) -> Option[UInt8]¶
u8 a - b. None on underflow. Complexity: O(1).
fn u8_mul_checked(a: UInt8, b: UInt8) -> Option[UInt8]¶
u8 a * b. None on overflow. Complexity: O(1).
fn u8_div_checked(a: UInt8, b: UInt8) -> Option[UInt8]¶
u8 a / b. None on div-by-zero. Complexity: O(1).
fn i64_add_sat(a: Int, b: Int) -> Int¶
Saturating i64 addition: clamps to INT_MAX/INT_MIN. Complexity: O(1).
fn i64_sub_sat(a: Int, b: Int) -> Int¶
Saturating i64 subtraction. Complexity: O(1).
fn i64_mul_sat(a: Int, b: Int) -> Int¶
Saturating i64 multiplication. Complexity: O(1).
fn u64_add_sat(a: UInt64, b: UInt64) -> UInt64¶
Saturating u64 addition: clamps at 0xFFFFFFFFFFFFFFFF. Complexity: O(1).
fn u64_sub_sat(a: UInt64, b: UInt64) -> UInt64¶
Saturating u64 subtraction: clamps at 0 on underflow. Complexity: O(1).
fn u64_mul_sat(a: UInt64, b: UInt64) -> UInt64¶
Saturating u64 multiplication: clamps at 0xFFFFFFFFFFFFFFFF. Complexity: O(1).
fn i32_add_sat(a: Int32, b: Int32) -> Int32¶
Saturating i32 addition. Complexity: O(1).
fn i32_sub_sat(a: Int32, b: Int32) -> Int32¶
Saturating i32 subtraction. Complexity: O(1).
fn i32_mul_sat(a: Int32, b: Int32) -> Int32¶
Saturating i32 multiplication. Complexity: O(1).
fn u32_add_sat(a: UInt32, b: UInt32) -> UInt32¶
Saturating u32 addition: clamps at 4294967295. Complexity: O(1).
fn u32_sub_sat(a: UInt32, b: UInt32) -> UInt32¶
Saturating u32 subtraction: clamps at 0. Complexity: O(1).
fn u32_mul_sat(a: UInt32, b: UInt32) -> UInt32¶
Saturating u32 multiplication. Complexity: O(1).
fn i128_from_i64(v: Int) -> Int128¶
Constructs an Int128 from a signed 64-bit value (sign-extended). Complexity: O(1).
fn i128_from_parts(hi: Int, lo: UInt64) -> Int128¶
Constructs an Int128 directly from a high signed limb and a low unsigned limb (value = hi * 2^64 + lo). Complexity: O(1).
fn i128_add(a: Int128, b: Int128) -> Int128¶
128-bit addition (wraps on overflow). Complexity: O(1).
fn i128_sub(a: Int128, b: Int128) -> Int128¶
128-bit subtraction (wraps on underflow). Complexity: O(1).
fn i128_neg(a: Int128) -> Int128¶
Two's-complement negation. Handles INT128_MIN correctly (wraps back to itself, as required by two's-complement arithmetic). Complexity: O(1).
fn i128_abs(a: Int128) -> Int128¶
Absolute value (returns the negated value for INT128_MIN, documenting the two's-complement wrap). Complexity: O(1).
fn i64_mul_wide(a: Int, b: Int) -> Int128¶
Full 128-bit product of two 64-bit signed values. i64_mul_wide(2^32, 2^32) == { hi: 1, lo: 0 } == 2^64. Exact. Complexity: O(1).
fn i128_mul(a: Int128, b: Int128) -> Int128¶
128 x 128 multiplication (result is modulo 2^128; low 128 bits are exact regardless of signedness). Complexity: O(1) -- native i128 mul.
fn i128_compare(a: Int128, b: Int128) -> Int¶
Three-way comparison (-1/0/1). Complexity: O(1).
fn i128_is_zero(a: Int128) -> Bool¶
Returns true iff the value is exactly zero. Complexity: O(1).
fn i128_is_negative(a: Int128) -> Bool¶
Returns true iff the value is negative (top bit set). Complexity: O(1).
fn i128_to_i64(a: Int128) -> Option[Int]¶
Converts to i64; None if the value does not fit in a signed 64-bit range. Complexity: O(1).
fn i128_to_str(a: Int128) -> Str¶
Decimal string representation, handling the sign. Exact for the full Int128 range, including INT128_MIN. Complexity: O(128 * digits) ~ O(1) bounded by 39 digits.
NOTE (2026-08-08): uses 64-bit limb arithmetic, NOT native i128 div/rem -- the sdiv/srem i128 libcalls (__divti3/__modti3) inside a multi-iteration loop with memory ops miscompile at clang -O2 (verified repeatedly). The limb algorithm is exact (e2e-proven pre-D1) and works at every opt level.
fn i128_from_str(s: Str) -> Result[Int128, Str]¶
Parses a decimal string (optional +/- prefix) into an Int128. Returns Err on invalid characters, empty input, or overflow beyond the 128-bit range. Complexity: O(digits * 128) ~ O(1).
fn i128_shl(a: Int128, n: Int) -> Int128¶
Arithmetic shift left by n bits (wraps at 128 bits). n >= 128 yields zero. Complexity: O(1).
fn i128_shr(a: Int128, n: Int) -> Int128¶
Arithmetic shift right by n bits (sign-extending). For n >= 128 the result is the sign (all ones for negatives, zero otherwise). Complexity: O(1).
fn i64_mul_div(a: Int, b: Int, c: Int) -> Int¶
a*b/c evaluated with a 128-bit intermediate, then clamped (saturated) to the i64 range. c == 0 returns 0 (documented). Exact for all i64 inputs. Complexity: O(1) -- native i128 mul + div.
fn u8_from_i64(v: Int) -> Option[Int]¶
u8 from i64: Some(v) iff 0 <= v <= 255. Complexity: O(1).
fn u8_from_i32(v: Int32) -> Option[Int]¶
u8 from i32 (promoted through i64 arithmetic). Complexity: O(1).
fn u16_from_i64(v: Int) -> Option[Int]¶
u16 from i64: Some(v) iff 0 <= v <= 65535. Complexity: O(1).
fn u32_from_i64(v: Int) -> Option[Int]¶
u32 from i64: Some(v) iff 0 <= v <= 4294967295. Complexity: O(1).
fn u64_from_i64(v: Int) -> Option[UInt64]¶
u64 from i64: Some(v) iff v >= 0. Complexity: O(1).
fn i8_from_i64(v: Int) -> Option[Int]¶
i8 from i64: Some(v) iff -128 <= v <= 127. Complexity: O(1).
fn i16_from_i64(v: Int) -> Option[Int]¶
i16 from i64: Some(v) iff -32768 <= v <= 32767. Complexity: O(1).
fn i32_from_i64(v: Int) -> Option[Int]¶
i32 from i64: Some(v) iff -2147483648 <= v <= 2147483647. Complexity: O(1).
fn i64_from_u64(v: UInt64) -> Option[Int]¶
i64 from u64: Some(v) iff v has the top bit clear. Complexity: O(1).
fn i64_from_i32(v: Int32) -> Int¶
i64 from i32: always fits. Complexity: O(1).
fn i64_from_i16(v: Int16) -> Int¶
i64 from i16: always fits. Complexity: O(1).
fn i64_from_i8(v: Int8) -> Int¶
i64 from i8: always fits. Complexity: O(1).
fn i64_from_u8(v: UInt8) -> Int¶
i64 from u8: always fits. Complexity: O(1).
fn i64_from_u16(v: UInt16) -> Int¶
i64 from u16: always fits. Complexity: O(1).
fn i64_from_u32(v: UInt32) -> Int¶
i64 from u32: always fits. Complexity: O(1).
fn f64_from_int(v: Int) -> Float64¶
Float64 from i64 (lossless only for |v| < 2^53; rounds beyond). Complexity: O(1).
fn f64_from_i32(v: Int32) -> Float64¶
Float64 from i32: always exact. Complexity: O(1).
fn f64_from_u64(v: UInt64) -> Float64¶
Float64 from u64: exact split into two 32-bit halves avoids relying on unsigned-to-float conversions for values above i64::MAX. Complexity: O(1).
fn f32_from_int(v: Int) -> Float32¶
Float32 from i64 (rounds for |v| > 2^24). Complexity: O(1).
fn f32_from_f64(v: Float64) -> Float32¶
Float32 from Float64 (round-to-nearest; inf/NaN propagate). Complexity: O(1).
fn f64_from_f32(v: Float32) -> Float64¶
Float64 from Float32: always exact. Complexity: O(1).
fn int_from_f64_trunc(v: Float64) -> Option[Int]¶
Truncating conversion Float64 -> Int. None on NaN or out-of-i64-range. Complexity: O(1).
fn int_from_f64_round(v: Float64) -> Option[Int]¶
Rounding conversion Float64 -> Int (round-half-away-from-zero). None on NaN or out-of-range. Complexity: O(1).
fn i32_from_f64_trunc(v: Float64) -> Option[Int32]¶
Truncating conversion Float64 -> Int32. None on NaN, out-of-i64-range, or outside the Int32 range. Complexity: O(1).
fn i32_from_f64_round(v: Float64) -> Option[Int32]¶
Rounding conversion Float64 -> Int32 (round-half-away-from-zero). Complexity: O(1).
fn u64_from_f64_trunc(v: Float64) -> Option[UInt64]¶
Truncating conversion Float64 -> UInt64. None on NaN, negatives, or values >= 2^64. Handles the high half without unsigned-to-int casts. Complexity: O(1).
fn int_from_f32_trunc(v: Float32) -> Option[Int]¶
Truncating conversion Float32 -> Int. Complexity: O(1).
fn int_from_f32_round(v: Float32) -> Option[Int]¶
Rounding conversion Float32 -> Int (round-half-away-from-zero). Complexity: O(1).
fn int_from_str_radix_checked(s: Str, radix: Int) -> Option[Int]¶
Bounds-checked integer parse in the given radix (2..36). Reuses parse_int_radix; None on invalid input or overflow. Complexity: O(len(s)).
fn i64_abs(v: Int) -> Int¶
Absolute value of an i64. NOTE: |INT_MIN| wraps to INT_MIN (two's-complement); use i64_neg_checked for overflow-safe negation. Complexity: O(1).
fn i32_abs(v: Int32) -> Int32¶
Absolute value of an i32. |INT32_MIN| wraps to INT32_MIN. Complexity: O(1).
fn i16_abs(v: Int16) -> Int16¶
Absolute value of an i16. |INT16_MIN| wraps to INT16_MIN. Complexity: O(1).
fn i8_abs(v: Int8) -> Int8¶
Absolute value of an i8. |INT8_MIN| wraps to INT8_MIN. Complexity: O(1).
fn f64_abs(x: Float64) -> Float64¶
Absolute value of a Float64 (fabs; -0.0 becomes +0.0, NaN propagates). Complexity: O(1).
fn f32_abs(x: Float32) -> Float32¶
Absolute value of a Float32. Complexity: O(1).
fn i64_clamp(v: Int, lo: Int, hi: Int) -> Int¶
Clamps v to the inclusive range [lo, hi]. Complexity: O(1).
fn i32_clamp(v: Int32, lo: Int32, hi: Int32) -> Int32¶
Clamps an i32 (promoted to i64 arithmetic). Complexity: O(1).
fn u64_clamp(v: UInt64, lo: UInt64, hi: UInt64) -> UInt64¶
Clamps a u64 using unsigned comparisons (avoids the signed
>bug). Complexity: O(1).
fn f64_clamp(x: Float64, lo: Float64, hi: Float64) -> Float64¶
Clamps a Float64. Complexity: O(1).
fn f32_clamp(x: Float32, lo: Float32, hi: Float32) -> Float32¶
Clamps a Float32. Complexity: O(1).
fn i64_signum(v: Int) -> Int¶
-1/0/1 for negative/zero/positive i64. Complexity: O(1).
fn i32_signum(v: Int32) -> Int¶
-1/0/1 for i32. Complexity: O(1).
fn f64_signum(v: Float64) -> Float64¶
-1.0/0.0/1.0 for a Float64. NaN returns NaN (IEEE signum semantics). Complexity: O(1).
fn f32_signum(v: Float32) -> Float32¶
-1.0/0.0/1.0 for a Float32. NaN returns NaN. Complexity: O(1).
fn i64_pow(base: Int, exp: Int) -> Int¶
base^exp for i64 via square-and-multiply. Overflows WRAP (documented); use i64_pow_checked for overflow detection. Negative exponent returns 0. Complexity: O(log exp).
fn u64_pow(base: UInt64, exp: Int) -> UInt64¶
base^exp for u64. Overflows WRAP (documented). Negative exponent returns 0. Complexity: O(log exp).
fn i32_pow(base: Int32, exp: Int) -> Int32¶
base^exp for i32 (wraps at 32 bits; documented). Negative exponent returns 0. Complexity: O(log exp).
fn f64_pow(base: Float64, exp: Float64) -> Float64¶
Float64 power, delegating to xiom.math.pow. Negative bases require an integer exponent; otherwise returns NaN. Exponent outside the i64 range returns NaN (documented edge). Complexity: O(log exp) via libm.
- Precondition:
true
fn f32_pow(base: Float32, exp: Float32) -> Float32¶
Float32 power (promoted through f64_pow). Complexity: O(log exp).
fn i64_min_of3(a: Int, b: Int, c: Int) -> Int¶
Minimum of three i64 values. Concrete (generic min3 needs Ord dispatch). Complexity: O(1).
fn i64_max_of3(a: Int, b: Int, c: Int) -> Int¶
Maximum of three i64 values. Complexity: O(1).
fn fraction_new(num: Int, den: Int) -> Fraction¶
Builds a reduced fraction from num/den, normalizing the sign to the denominator. den == 0 returns the zero fraction (documented). Complexity: O(log max(|num|, |den|)) for gcd.
fn fraction_from_int(v: Int) -> Fraction¶
Builds the fraction v/1. Complexity: O(1).
fn fraction_zero() -> Fraction¶
The zero fraction 0/1. Complexity: O(1).
fn fraction_one() -> Fraction¶
The one fraction 1/1. Complexity: O(1).
fn fraction_add(a: &Fraction, b: &Fraction) -> Fraction¶
a + b. Pre-reduces by gcd(a.den, b.den) to limit overflow; the final result is re-reduced. Cross-products may still overflow i64 for very large denominators (documented). Complexity: O(log max(den)).
fn fraction_sub(a: &Fraction, b: &Fraction) -> Fraction¶
a - b. Complexity: O(log max(den)).
fn fraction_mul(a: &Fraction, b: &Fraction) -> Fraction¶
a * b. Cross-cancels via gcd before multiplying, minimizing overflow. Complexity: O(log max(|num|, den)).
fn fraction_div(a: &Fraction, b: &Fraction) -> Fraction¶
a / b (flip b and multiply). Dividing by the zero fraction yields zero (documented). Complexity: O(log max(|num|, den)).
fn fraction_neg(a: &Fraction) -> Fraction¶
-a. Complexity: O(1).
fn fraction_compare(a: Fraction, b: Fraction) -> Int¶
Three-way comparison via cross-multiplication. Cross-products can overflow i64 for large fractions (documented). Complexity: O(1).
fn fraction_is_zero(a: Fraction) -> Bool¶
Returns true iff the fraction is zero. Complexity: O(1).
fn fraction_num(a: Fraction) -> Int¶
The numerator. Complexity: O(1).
fn fraction_den(a: Fraction) -> Int¶
The denominator (always > 0). Complexity: O(1).
fn fraction_to_float(a: Fraction) -> Float64¶
Converts to Float64 (numerator/denominator division). Complexity: O(1).
fn fraction_to_float32(a: Fraction) -> Float32¶
Converts to Float32. Complexity: O(1).
fn fraction_to_str(a: Fraction) -> Str¶
Renders as "num/den". Complexity: O(1) string building.
fn fraction_from_str(s: Str) -> Option[Fraction]¶
Parses "num/den" (optional signs on each part). None on malformed input, empty parts, or a zero denominator. Complexity: O(len(s)).
fn fraction_reciprocal(a: &Fraction) -> Fraction¶
1/a (den/num). The zero fraction has no reciprocal; returns zero. Complexity: O(log max(|num|, den)).
fn fraction_pow_int(a: Fraction, n: Int) -> Fraction¶
a^n for integer exponents. Negative n raises the reciprocal; n == 0 returns one. n == INT_MIN returns zero (documented overflow edge). Complexity: O(log |n|).
fn fraction_is_proper(a: Fraction) -> Bool¶
Returns true iff |num| < den (a proper fraction). NOTE: |INT_MIN| wraps (documented); proper tests near the i64 extreme are degenerate. Complexity: O(1).
fn i64_lerp(a: Int, b: Int, t: Int) -> Int¶
Integer lerp: a + (b - a) * t with t clamped to [0, 1]. For i64, t is effectively 0 or 1. NOTE: (b - a) * t can overflow i64 for extreme ranges (documented). Complexity: O(1).
fn f64_lerp(a: Float64, b: Float64, t: Float64) -> Float64¶
Float64 lerp: a + (b - a) * t. Complexity: O(1).
fn f32_lerp(a: Float32, b: Float32, t: Float32) -> Float32¶
Float32 lerp: a + (b - a) * t. Complexity: O(1).
fn f64_inverse_lerp(a: Float64, b: Float64, v: Float64) -> Float64¶
Inverse lerp: (v - a) / (b - a), clamped to [0, 1]. If b == a, returns 0 (documented). Complexity: O(1).
fn f64_remap(v: Float64, in_lo: Float64, in_hi: Float64, out_lo: Float64, out_hi: Float64) -> Float64¶
Remaps v from the range [in_lo, in_hi] to [out_lo, out_hi] (linear). If in_hi == in_lo, returns out_lo (documented). Complexity: O(1).
precision_float.xi¶
fn bigfloat_from_float(x: Float64) -> BigFloat¶
Constructs a BigFloat from a native float. NaN/inf are not representable (the underlying implementation requires finite inputs; do not pass them). Complexity: O(1).
fn bigfloat_from_str(s: Str) -> Option[BigFloat]¶
Parses a decimal string ("3.14", "-1e-10", ".5"). None on invalid input. Complexity: O(n).
fn bigfloat_to_str(b: BigFloat) -> Str¶
Decimal string representation at the current precision. Complexity: O(n).
fn bigfloat_with_precision(b: BigFloat, p: Int) -> BigFloat¶
Re-rounds b to p significant digits (round-half-to-even), returning a fresh value with precision p. p < 1 is clamped to 1 (documented). Complexity: O(n).
fn bigfloat_add(a: BigFloat, b: BigFloat) -> BigFloat¶
Sum a + b, rounded to max(a.precision, b.precision). Complexity: O(n).
fn bigfloat_sub(a: BigFloat, b: BigFloat) -> BigFloat¶
Difference a - b. Complexity: O(n).
fn bigfloat_mul(a: BigFloat, b: BigFloat) -> BigFloat¶
Product a * b. Complexity: O(n^2).
fn bigfloat_div(a: BigFloat, b: BigFloat) -> Option[BigFloat]¶
Quotient a / b. None when b is zero. Complexity: O(n^2).
fn bigfloat_neg(a: BigFloat) -> BigFloat¶
Negation. Complexity: O(1).
fn bigfloat_abs(a: BigFloat) -> BigFloat¶
Absolute value. Complexity: O(1).
fn bigfloat_sqrt(a: BigFloat) -> Option[BigFloat]¶
Square root. None when a is negative (Newton on the significand). Complexity: O(prec^2).
fn bigfloat_cbrt(a: BigFloat) -> BigFloat¶
Cube root. Complexity: O(prec^2).
fn bigfloat_exp(a: BigFloat) -> BigFloat¶
Exponential function. Complexity: O(prec^2) series.
fn bigfloat_ln(a: BigFloat) -> Option[BigFloat]¶
Natural logarithm. None when a is not positive (zero or negative). Complexity: O(prec^2).
fn bigfloat_log10(a: BigFloat) -> Option[BigFloat]¶
Base-10 logarithm. None when a is not positive. Complexity: O(prec^2).
fn bigfloat_log2(a: BigFloat) -> Option[BigFloat]¶
Base-2 logarithm. None when a is not positive. Complexity: O(prec^2).
fn bigfloat_pow(base: BigFloat, exp: BigFloat) -> BigFloat¶
Exponentiation base^exp for arbitrary BigFloat exponent (log + exp). Complexity: O(prec^2).
fn bigfloat_sin(a: BigFloat) -> BigFloat¶
Sine. Complexity: O(prec^2) series.
fn bigfloat_cos(a: BigFloat) -> BigFloat¶
Cosine. Complexity: O(prec^2) series.
fn bigfloat_tan(a: BigFloat) -> BigFloat¶
Tangent. Complexity: O(prec^2).
fn bigfloat_asin(a: BigFloat) -> Option[BigFloat]¶
Arcsine. None when a is outside [-1, 1]. Complexity: O(prec^2).
fn bigfloat_acos(a: BigFloat) -> Option[BigFloat]¶
Arccosine. None when a is outside [-1, 1]. Complexity: O(prec^2).
fn bigfloat_atan(a: BigFloat) -> BigFloat¶
Arctangent. Complexity: O(prec^2).
fn bigfloat_atan2(y: BigFloat, x: BigFloat) -> BigFloat¶
Four-quadrant arctangent of y/x. Complexity: O(prec^2).
fn bigfloat_sinh(a: BigFloat) -> BigFloat¶
Hyperbolic sine. Complexity: O(prec^2).
fn bigfloat_cosh(a: BigFloat) -> BigFloat¶
Hyperbolic cosine. Complexity: O(prec^2).
fn bigfloat_tanh(a: BigFloat) -> BigFloat¶
Hyperbolic tangent. Complexity: O(prec^2).
fn bigfloat_pi(p: Int) -> BigFloat¶
Pi to p significant digits (p < 1 clamped to 1). Complexity: O(prec^2).
fn bigfloat_e(p: Int) -> BigFloat¶
Euler's number e to p significant digits (p < 1 clamped to 1). Complexity: O(prec^2).
fn bigfloat_compare(a: BigFloat, b: BigFloat) -> Int¶
Three-way comparison: -1, 0, or 1 ordering a vs b. Complexity: O(n).
precision_integer.xi¶
fn bigint_from_int(v: Int) -> BigInt¶
Constructs a BigInt from a native integer. Complexity: O(1).
fn bigint_from_str(s: Str) -> Option[BigInt]¶
Parses a decimal string (optional leading '-'/'+'). None on invalid input (empty string or non-digit character). Complexity: O(n^2) accumulation.
fn bigint_to_str(b: BigInt) -> Str¶
Decimal string representation. Complexity: O(n).
fn bigint_to_hex(b: BigInt) -> Str¶
Lowercase hexadecimal string representation ("ff", "-1a"). Complexity: O(n).
fn bigint_to_bin(b: BigInt) -> Str¶
Binary string representation ("0"/"1" digits). Complexity: O(n).
fn bigint_to_oct(b: BigInt) -> Str¶
Octal string representation. Complexity: O(n).
fn bigint_add(a: BigInt, b: BigInt) -> BigInt¶
Sum a + b. Complexity: O(n).
fn bigint_sub(a: BigInt, b: BigInt) -> BigInt¶
Difference a - b. Complexity: O(n).
fn bigint_mul(a: BigInt, b: BigInt) -> BigInt¶
Product a * b. Complexity: O(n^2) schoolbook, Karatsuba above 36k digits.
fn bigint_div(a: BigInt, b: BigInt) -> Option[BigInt]¶
Quotient a / b (truncating). None when b is zero. Complexity: O(n^2) Knuth Algorithm D.
fn bigint_mod(a: BigInt, b: BigInt) -> Option[BigInt]¶
Remainder a mod b (sign of the dividend). None when b is zero. Complexity: O(n^2).
fn bigint_pow(base: BigInt, exp: Int) -> BigInt¶
base raised to a non-negative integer power exp (square-and-multiply). exp < 0 returns zero (documented). Complexity: O(log exp) multiplications.
- Precondition:
exp >= 0
fn bigint_neg(a: BigInt) -> BigInt¶
Negation. Complexity: O(n).
fn bigint_abs(a: BigInt) -> BigInt¶
Absolute value. Complexity: O(n).
fn bigint_compare(a: BigInt, b: BigInt) -> Int¶
Three-way comparison: -1, 0, or 1 ordering a vs b. Complexity: O(n).
fn bigint_eq(a: BigInt, b: BigInt) -> Bool¶
Whether a equals b. Complexity: O(n).
fn bigint_lt(a: BigInt, b: BigInt) -> Bool¶
Whether a is strictly less than b. Complexity: O(n).
fn bigint_gt(a: BigInt, b: BigInt) -> Bool¶
Whether a is strictly greater than b. Complexity: O(n).
fn bigint_bit_and(a: BigInt, b: BigInt) -> BigInt¶
Bitwise AND (two's-complement semantics). Complexity: O(n).
fn bigint_bit_or(a: BigInt, b: BigInt) -> BigInt¶
Bitwise OR (two's-complement semantics). Complexity: O(n).
fn bigint_bit_xor(a: BigInt, b: BigInt) -> BigInt¶
Bitwise XOR (two's-complement semantics). Complexity: O(n).
fn bigint_shift_left(a: BigInt, n: Int) -> BigInt¶
Left shift by n bits (multiply by 2^n; n < 0 treated as 0). This is a true BIT shift (distinct from xiom.bigint's decimal shift_left). Complexity: O(n^2) via the multiplications.
- Precondition:
n >= 0
fn bigint_shift_right(a: BigInt, n: Int) -> BigInt¶
Arithmetic (floor) right shift by n bits: b >> n rounds toward -inf for negative b. n < 0 treated as 0. Complexity: O(n^2) via the divisions.
- Precondition:
n >= 0
fn bigint_is_prime(b: BigInt) -> Bool¶
Probabilistic primality test (Miller-Rabin, deterministic for n < 3.3e24). Complexity: O(k log^3 n) bigint operations.
fn bigint_gcd(a: BigInt, b: BigInt) -> BigInt¶
Greatest common divisor (non-negative). Complexity: O(log n) divisions.
fn bigint_lcm(a: BigInt, b: BigInt) -> BigInt¶
Least common multiple; lcm(0, x) == 0. Complexity: O(gcd + mul).
fn bigint_mod_inverse(a: BigInt, m: BigInt) -> Option[BigInt]¶
Modular inverse of a mod m via extended Euclid. None when gcd(a, m) != 1 or when m is zero. The result lies in [0, |m|). Complexity: O(log m) divisions.
fn bigint_mod_pow(base: BigInt, exp: BigInt, m: BigInt) -> BigInt¶
(base^exp) mod m via square-and-multiply. Requires m != 0 and exp >= 0; a zero modulus returns zero (documented). Complexity: O(log exp) muls.
- Precondition:
!xiom.bigint.bigint_is_zero(&m) - Precondition:
!xiom.bigint.bigint_is_negative(&exp)
fn bigint_factorial(n: Int) -> BigInt¶
n! as an arbitrary-precision integer. n < 0 returns zero (documented). Complexity: O(n) bigint multiplications.
- Precondition:
n >= 0
fn bigint_binomial(n: Int, k: Int) -> BigInt¶
Binomial coefficient C(n, k). Invalid k (outside [0, n]) returns zero. Complexity: O(k) bigint multiplications/divisions.
- Precondition:
n >= 0 - Precondition:
k >= 0 - Precondition:
k <= n
precision_rational.xi¶
type BigRat¶
Arbitrary-precision rational number: num/den, den > 0, always reduced.
| Field | Type |
|---|---|
num |
BigInt |
den |
BigInt |
fn bigrat_new(num: BigInt, den: BigInt) -> Option[BigRat]¶
Constructs a reduced BigRat from num/den, normalizing the sign to the denominator. None when den is zero. Zero maps to 0/1. Complexity: O(gcd) bigint operations.
fn bigrat_from_int(v: Int) -> BigRat¶
Constructs the BigRat v/1. Complexity: O(1).
fn bigrat_from_str(s: Str) -> Option[BigRat]¶
Parses "num/den" (optional signs) or a decimal string ("3.14", "-0.5"). None on invalid input, empty parts, or a zero denominator. Complexity: O(n^2) via the bigint accumulation.
fn bigrat_to_str(r: BigRat) -> Str¶
String representation: "num" when the denominator is 1, else "num/den". Complexity: O(n).
fn bigrat_numerator(r: BigRat) -> BigInt¶
The numerator (a copy). Complexity: O(n).
fn bigrat_denominator(r: BigRat) -> BigInt¶
The denominator (always positive; a copy). Complexity: O(n).
fn bigrat_add(a: BigRat, b: BigRat) -> BigRat¶
Sum a + b: (a.numb.den + b.numa.den) / (a.den*b.den), reduced. Complexity: O(n^2) bigint operations.
fn bigrat_sub(a: BigRat, b: BigRat) -> BigRat¶
Difference a - b. Complexity: O(n^2).
fn bigrat_mul(a: BigRat, b: BigRat) -> BigRat¶
Product a * b. Complexity: O(n^2).
fn bigrat_div(a: BigRat, b: BigRat) -> Option[BigRat]¶
Quotient a / b. None when b is zero. The denominator sign is normalized by the reduction. Complexity: O(n^2).
fn bigrat_neg(a: BigRat) -> BigRat¶
Negation. Complexity: O(n).
fn bigrat_abs(a: BigRat) -> BigRat¶
Absolute value. Complexity: O(n).
fn bigrat_recip(a: BigRat) -> Option[BigRat]¶
Reciprocal 1/a. None when a is zero. Complexity: O(n^2) reduction.
fn bigrat_reduce(r: BigRat) -> BigRat¶
Reduces r to lowest terms with a positive denominator. Identity when r already satisfies the invariant. Complexity: O(gcd).
fn bigrat_is_reduced(r: BigRat) -> Bool¶
Whether r is in lowest terms with a positive denominator. Complexity: O(gcd).
fn bigrat_is_integer(r: BigRat) -> Bool¶
Whether the denominator divides the numerator (i.e. r is an integer; for a reduced fraction this is den == 1). Complexity: O(1).
fn bigrat_is_zero(r: BigRat) -> Bool¶
Whether r equals zero. Complexity: O(1).
fn bigrat_compare(a: BigRat, b: BigRat) -> Int¶
Three-way comparison via cross-multiplication: -1, 0, or 1. Valid because denominators are positive. Complexity: O(n^2).
fn bigrat_eq(a: BigRat, b: BigRat) -> Bool¶
Whether a equals b. Complexity: O(n^2).
fn bigrat_to_float(r: BigRat) -> Float64¶
Converts to Float64 (bigint magnitude accumulation, then division). The result may be inf for values beyond the Float64 range (documented). Complexity: O(n).
fn bigrat_to_integer(r: BigRat) -> Option[BigInt]¶
The integer value of r; None when r is not integral. Complexity: O(1) (reduced form: integral iff den == 1).