module scheme import math import math.big import math.bits as bitops const exact_min = i64(-9223372036854775807 - 1) const exact_max = i64(9223372036854775807) fn magnitude(value i64) u64 { return if value < 0 { u64(-(value + 1)) + 1 } else { u64(value) } } fn gcd(a u64, b u64) u64 { mut x := a mut y := b for y != 0 { x, y = y, x % y } return x } fn divide_magnitude(value i64, divisor u64) i64 { if divisor > u64(exact_max) { return if value == exact_min { i64(-1) } else { i64(0) } } return value / i64(divisor) } fn checked_add(a i64, b i64) !i64 { if (b > 0 && a > exact_max - b) || (b < 0 && a < exact_min - b) { return failure('exact arithmetic overflow') } return a + b } fn checked_negate(a i64) !i64 { if a == exact_min { return failure('exact arithmetic overflow') } return -a } fn checked_subtract(a i64, b i64) !i64 { if (b > 0 && a < exact_min + b) || (b < 0 && a > exact_max + b) { return failure('exact arithmetic overflow') } return a - b } // rational normalizes exact host components, promoting the result when sign // normalization needs more than i64. Denominator one becomes an integer. pub fn rational(numerator i64, denominator i64) !Value { if denominator == 0 { return failure('division by zero') } if numerator == 0 { return integer(0) } mut n := numerator mut d := denominator g := gcd(magnitude(n), magnitude(d)) n = divide_magnitude(n, g) d = divide_magnitude(d, g) if d < 0 { if n == exact_min || d == exact_min { return normalize_big_rational(big.integer_from_i64(n), big.integer_from_i64(d), false) } n = checked_negate(n)! d = checked_negate(d)! } if d == 1 { return integer(n) } return payload_value(.rational, &Rational{ numerator: n, denominator: d }) } fn is_number(v Value) bool { return v.tag in [.integer, .rational, .real, .big_integer, .big_rational, .exact_complex, .inexact_complex] } fn numeric_add(a Value, b Value) !Value { return numeric_add_subtract(a, b, false) } fn numeric_subtract(a Value, b Value) !Value { return numeric_add_subtract(a, b, true) } fn numeric_add_subtract(a Value, b Value, subtract bool) !Value { if a.tag == .integer && b.tag == .integer { return integer_add_value(a, b, subtract) } if is_nonreal(a) || is_nonreal(b) { return complex_binary(a, b, if subtract { .subtract } else { .add }) } if a.tag == .real || b.tag == .real { x, y := a.as_real()!, b.as_real()! return real(if subtract { x - y } else { x + y }) } if is_big_exact(a) || is_big_exact(b) { return big_numeric_binary(a, b, if subtract { .subtract } else { .add }) } an, ad := a.as_rational()! bn, bd := b.as_rational()! g := gcd(u64(ad), u64(bd)) x := small_multiply(an, bd / i64(g)) or { return big_numeric_binary(a, b, if subtract { .subtract } else { .add }) } y := small_multiply(bn, ad / i64(g)) or { return big_numeric_binary(a, b, if subtract { .subtract } else { .add }) } n := if subtract { small_subtract(x, y) } else { small_add(x, y) } or { return big_numeric_binary(a, b, if subtract { .subtract } else { .add }) } if n == 0 { return integer(0) } h := gcd(magnitude(n), g) d := small_multiply(ad / i64(g), bd / i64(h)) or { return big_numeric_binary(a, b, if subtract { .subtract } else { .add }) } return rational(divide_magnitude(n, h), d) } fn numeric_negate(a Value) !Value { if a.tag == .integer { if a.whole() == exact_min { return big_numeric_negate(a) } return integer(-a.whole()) } if is_nonreal(a) { x, y := complex_parts(a) return owned_complex(numeric_negate(x)!, numeric_negate(y)!) } if a.tag == .real { return real(-a.floating()) } if is_big_exact(a) { return big_numeric_negate(a) } n, d := a.as_rational()! if n == exact_min { return big_numeric_negate(a) } return rational(-n, d) } fn numeric_multiply(a Value, b Value) !Value { if a.tag == .integer && b.tag == .integer { return integer_multiply_value(a, b) } if is_nonreal(a) || is_nonreal(b) { return complex_binary(a, b, .multiply) } if a.tag == .real || b.tag == .real { return real(a.as_real()! * b.as_real()!) } if is_big_exact(a) || is_big_exact(b) { return big_numeric_binary(a, b, .multiply) } an, ad := a.as_rational()! bn, bd := b.as_rational()! g := gcd(magnitude(an), u64(bd)) h := gcd(magnitude(bn), u64(ad)) n := small_multiply(divide_magnitude(an, g), divide_magnitude(bn, h)) or { return big_numeric_binary(a, b, .multiply) } d := small_multiply(ad / i64(h), bd / i64(g)) or { return big_numeric_binary(a, b, .multiply) } return rational(n, d) } fn numeric_divide(a Value, b Value) !Value { if !is_number(a) || !is_number(b) { return failure('expected numbers') } if (b.tag == .real && b.floating() == 0) || (b.tag == .integer && b.whole() == 0) { return failure('division by zero') } if is_nonreal(a) || is_nonreal(b) { return complex_binary(a, b, .divide) } if a.tag == .real || b.tag == .real { return real(a.as_real()! / b.as_real()!) } if is_big_exact(a) || is_big_exact(b) { return big_numeric_binary(a, b, .divide) } an, ad := a.as_rational()! bn, bd := b.as_rational()! if an == 0 { return integer(0) } g := gcd(magnitude(an), magnitude(bn)) h := gcd(u64(ad), u64(bd)) n := small_multiply(divide_magnitude(an, g), bd / i64(h)) or { return big_numeric_binary(a, b, .divide) } d := small_multiply(ad / i64(h), divide_magnitude(bn, g)) or { return big_numeric_binary(a, b, .divide) } return rational(n, d) } // Continued-fraction comparison avoids overflowing cross-products and avoids // rounding large exact integers through f64. fn compare_fraction(a u64, b u64, c u64, d u64) int { mut an := a mut ad := b mut bn := c mut bd := d mut direction := 1 for { x, y := an / ad, bn / bd if x < y { return -direction } if x > y { return direction } r, s := an % ad, bn % bd if r == 0 || s == 0 { if r == s { return 0 } return if r == 0 { -direction } else { direction } } an, ad = ad, r bn, bd = bd, s direction = -direction } return 0 } fn numeric_compare(a Value, b Value) !int { if a.tag == .integer && b.tag == .integer { return if a.whole() < b.whole() { -1 } else if a.whole() > b.whole() { 1 } else { 0 } } if is_nonreal(a) || is_nonreal(b) { return failure('ordering requires real numbers') } if a.tag == .real && b.tag == .real { x, y := a.floating(), b.floating() if x != x || y != y { return failure('cannot order NaN') } return if x < y { -1 } else if x > y { 1 } else { 0 } } if a.tag == .real { return -compare_exact_real(b, a.floating())! } if b.tag == .real { return compare_exact_real(a, b.floating()) } if is_big_exact(a) || is_big_exact(b) { return big_exact_compare(a, b) } an, ad := a.as_rational()! bn, bd := b.as_rational()! if an < 0 && bn >= 0 { return -1 } if an >= 0 && bn < 0 { return 1 } result := compare_fraction(magnitude(an), u64(ad), magnitude(bn), u64(bd)) return if an < 0 { -result } else { result } } // Compare an exact bounded rational with the actual binary value of a float. // Rounding the exact operand to f64 would break transitivity near 2^53 and // would equate e.g. 1/10 with the slightly larger binary approximation of 0.1. // The significand times a bounded denominator needs at most 116 bits. Wide // exponent gaps are decided before shifting, so no bignum or heap object is // needed even for subnormal or very large floating-point values. fn compare_exact_real(exact Value, floating f64) !int { if is_big_exact(exact) { return big_compare_real(exact, floating) } numerator, denominator := exact.as_rational()! bits := math.f64_bits(floating) negative := bits >> 63 != 0 biased := int((bits >> 52) & 0x7ff) mut significand := bits & u64(0x000fffffffffffff) if biased == 0x7ff { if significand != 0 { return failure('cannot order NaN') } return if negative { 1 } else { -1 } } if floating == 0 { return if numerator < 0 { -1 } else if numerator > 0 { 1 } else { 0 } } if numerator == 0 { return if negative { 1 } else { -1 } } if (numerator < 0) != negative { return if numerator < 0 { -1 } else { 1 } } exponent := if biased == 0 { -1074 } else { biased - 1023 - 52 } if biased != 0 { significand |= u64(1) << 52 } product := u128(significand) * u128(denominator) n := u128(magnitude(numerator)) limit := ~u128(0) mut order := 0 if exponent >= 0 { if exponent >= 128 || product > (limit >> exponent) { order = -1 } else { right := product << exponent order = if n < right { -1 } else if n > right { 1 } else { 0 } } } else { shift := -exponent if shift >= 128 || n > (limit >> shift) { order = 1 } else { left := n << shift order = if left < product { -1 } else if left > product { 1 } else { 0 } } } return if negative { -order } else { order } } // Direct IEEE rounding avoids rounding the numerator and denominator before // their division. Small components and power-of-two denominators can still use // hardware division. For the remaining bounded ratios, the normalized quotient // needs 53 bits and its shifted numerator needs at most 117 bits, fitting u128. // Bounded components also ensure a normal, finite result; no subnormal/overflow // case is possible here. This path allocates no temporary rational or bignum. fn rational_real(numerator i64, denominator i64) f64 { n := magnitude(numerator) d := u64(denominator) if (n <= u64(1) << 53 && d <= u64(1) << 53) || d & (d - 1) == 0 { return f64(numerator) / f64(denominator) } mut exponent := bitops.len_64(n) - bitops.len_64(d) mut upper, mut lower := u128(n), u128(d) if exponent >= 0 { if upper < lower << exponent { exponent-- } } else if upper << -exponent < lower { exponent-- } shift := 52 - exponent if shift >= 0 { upper <<= shift } else { lower <<= -shift } quotient, remainder := upper / lower, upper % lower mut mantissa := u64(quotient) complement := lower - remainder if remainder > complement || (remainder == complement && mantissa & 1 != 0) { mantissa++ } if mantissa == u64(1) << 53 { mantissa >>= 1 exponent++ } mut raw := (u64(exponent + 1023) << 52) | (mantissa & u64(0x000fffffffffffff)) if numerator < 0 { raw |= u64(1) << 63 } return math.f64_from_bits(raw) } fn power_ten(n int) !i64 { if n < 0 || n > 18 { return failure('exact numeric literal exceeds supported range') } mut value := i64(1) for _ in 0 .. n { value *= 10 } return value } fn parse_number(spelling string) !Value { return parse_number_radix(spelling, 10) }