| 1 | module scheme |
| 2 | |
| 3 | import math |
| 4 | import math.big |
| 5 | import math.bits as bitops |
| 6 | |
| 7 | const exact_min = i64(-9223372036854775807 - 1) |
| 8 | const exact_max = i64(9223372036854775807) |
| 9 | |
| 10 | fn magnitude(value i64) u64 { |
| 11 | return if value < 0 { u64(-(value + 1)) + 1 } else { u64(value) } |
| 12 | } |
| 13 | |
| 14 | fn gcd(a u64, b u64) u64 { |
| 15 | mut x := a |
| 16 | mut y := b |
| 17 | for y != 0 { x, y = y, x % y } |
| 18 | return x |
| 19 | } |
| 20 | |
| 21 | fn divide_magnitude(value i64, divisor u64) i64 { |
| 22 | if divisor > u64(exact_max) { return if value == exact_min { i64(-1) } else { i64(0) } } |
| 23 | return value / i64(divisor) |
| 24 | } |
| 25 | |
| 26 | fn checked_add(a i64, b i64) !i64 { |
| 27 | if (b > 0 && a > exact_max - b) || (b < 0 && a < exact_min - b) { |
| 28 | return failure('exact arithmetic overflow') |
| 29 | } |
| 30 | return a + b |
| 31 | } |
| 32 | |
| 33 | fn checked_negate(a i64) !i64 { |
| 34 | if a == exact_min { return failure('exact arithmetic overflow') } |
| 35 | return -a |
| 36 | } |
| 37 | |
| 38 | fn checked_subtract(a i64, b i64) !i64 { |
| 39 | if (b > 0 && a < exact_min + b) || (b < 0 && a > exact_max + b) { |
| 40 | return failure('exact arithmetic overflow') |
| 41 | } |
| 42 | return a - b |
| 43 | } |
| 44 | |
| 45 | // rational normalizes exact host components, promoting the result when sign |
| 46 | // normalization needs more than i64. Denominator one becomes an integer. |
| 47 | pub fn rational(numerator i64, denominator i64) !Value { |
| 48 | if denominator == 0 { return failure('division by zero') } |
| 49 | if numerator == 0 { return integer(0) } |
| 50 | mut n := numerator |
| 51 | mut d := denominator |
| 52 | g := gcd(magnitude(n), magnitude(d)) |
| 53 | n = divide_magnitude(n, g) |
| 54 | d = divide_magnitude(d, g) |
| 55 | if d < 0 { |
| 56 | if n == exact_min || d == exact_min { |
| 57 | return normalize_big_rational(big.integer_from_i64(n), big.integer_from_i64(d), false) |
| 58 | } |
| 59 | n = checked_negate(n)! |
| 60 | d = checked_negate(d)! |
| 61 | } |
| 62 | if d == 1 { return integer(n) } |
| 63 | return payload_value(.rational, &Rational{ numerator: n, denominator: d }) |
| 64 | } |
| 65 | |
| 66 | fn is_number(v Value) bool { |
| 67 | return v.tag in [.integer, .rational, .real, .big_integer, .big_rational, .exact_complex, |
| 68 | .inexact_complex] |
| 69 | } |
| 70 | |
| 71 | fn numeric_add(a Value, b Value) !Value { |
| 72 | return numeric_add_subtract(a, b, false) |
| 73 | } |
| 74 | |
| 75 | fn numeric_subtract(a Value, b Value) !Value { |
| 76 | return numeric_add_subtract(a, b, true) |
| 77 | } |
| 78 | |
| 79 | fn numeric_add_subtract(a Value, b Value, subtract bool) !Value { |
| 80 | if a.tag == .integer && b.tag == .integer { |
| 81 | return integer_add_value(a, b, subtract) |
| 82 | } |
| 83 | if is_nonreal(a) || is_nonreal(b) { |
| 84 | return complex_binary(a, b, if subtract { .subtract } else { .add }) |
| 85 | } |
| 86 | if a.tag == .real || b.tag == .real { |
| 87 | x, y := a.as_real()!, b.as_real()! |
| 88 | return real(if subtract { x - y } else { x + y }) |
| 89 | } |
| 90 | if is_big_exact(a) || is_big_exact(b) { |
| 91 | return big_numeric_binary(a, b, if subtract { .subtract } else { .add }) |
| 92 | } |
| 93 | an, ad := a.as_rational()! |
| 94 | bn, bd := b.as_rational()! |
| 95 | g := gcd(u64(ad), u64(bd)) |
| 96 | x := small_multiply(an, bd / i64(g)) or { |
| 97 | return big_numeric_binary(a, b, if subtract { |
| 98 | .subtract |
| 99 | } else { |
| 100 | .add |
| 101 | }) |
| 102 | } |
| 103 | y := small_multiply(bn, ad / i64(g)) or { |
| 104 | return big_numeric_binary(a, b, if subtract { |
| 105 | .subtract |
| 106 | } else { |
| 107 | .add |
| 108 | }) |
| 109 | } |
| 110 | n := if subtract { small_subtract(x, y) } else { small_add(x, y) } or { |
| 111 | return big_numeric_binary(a, b, if subtract { |
| 112 | .subtract |
| 113 | } else { |
| 114 | .add |
| 115 | }) |
| 116 | } |
| 117 | if n == 0 { return integer(0) } |
| 118 | h := gcd(magnitude(n), g) |
| 119 | d := small_multiply(ad / i64(g), bd / i64(h)) or { |
| 120 | return big_numeric_binary(a, b, if subtract { |
| 121 | .subtract |
| 122 | } else { |
| 123 | .add |
| 124 | }) |
| 125 | } |
| 126 | return rational(divide_magnitude(n, h), d) |
| 127 | } |
| 128 | |
| 129 | fn numeric_negate(a Value) !Value { |
| 130 | if a.tag == .integer { |
| 131 | if a.whole() == exact_min { return big_numeric_negate(a) } |
| 132 | return integer(-a.whole()) |
| 133 | } |
| 134 | if is_nonreal(a) { |
| 135 | x, y := complex_parts(a) |
| 136 | return owned_complex(numeric_negate(x)!, numeric_negate(y)!) |
| 137 | } |
| 138 | if a.tag == .real { return real(-a.floating()) } |
| 139 | if is_big_exact(a) { return big_numeric_negate(a) } |
| 140 | n, d := a.as_rational()! |
| 141 | if n == exact_min { return big_numeric_negate(a) } |
| 142 | return rational(-n, d) |
| 143 | } |
| 144 | |
| 145 | fn numeric_multiply(a Value, b Value) !Value { |
| 146 | if a.tag == .integer && b.tag == .integer { |
| 147 | return integer_multiply_value(a, b) |
| 148 | } |
| 149 | if is_nonreal(a) || is_nonreal(b) { return complex_binary(a, b, .multiply) } |
| 150 | if a.tag == .real || b.tag == .real { return real(a.as_real()! * b.as_real()!) } |
| 151 | if is_big_exact(a) || is_big_exact(b) { return big_numeric_binary(a, b, .multiply) } |
| 152 | an, ad := a.as_rational()! |
| 153 | bn, bd := b.as_rational()! |
| 154 | g := gcd(magnitude(an), u64(bd)) |
| 155 | h := gcd(magnitude(bn), u64(ad)) |
| 156 | n := small_multiply(divide_magnitude(an, g), divide_magnitude(bn, h)) or { return big_numeric_binary(a, b, .multiply) } |
| 157 | d := small_multiply(ad / i64(h), bd / i64(g)) or { return big_numeric_binary(a, b, .multiply) } |
| 158 | return rational(n, d) |
| 159 | } |
| 160 | |
| 161 | fn numeric_divide(a Value, b Value) !Value { |
| 162 | if !is_number(a) || !is_number(b) { return failure('expected numbers') } |
| 163 | if (b.tag == .real && b.floating() == 0) || (b.tag == .integer && b.whole() == 0) { |
| 164 | return failure('division by zero') |
| 165 | } |
| 166 | if is_nonreal(a) || is_nonreal(b) { return complex_binary(a, b, .divide) } |
| 167 | if a.tag == .real || b.tag == .real { return real(a.as_real()! / b.as_real()!) } |
| 168 | if is_big_exact(a) || is_big_exact(b) { return big_numeric_binary(a, b, .divide) } |
| 169 | an, ad := a.as_rational()! |
| 170 | bn, bd := b.as_rational()! |
| 171 | if an == 0 { return integer(0) } |
| 172 | g := gcd(magnitude(an), magnitude(bn)) |
| 173 | h := gcd(u64(ad), u64(bd)) |
| 174 | n := small_multiply(divide_magnitude(an, g), bd / i64(h)) or { return big_numeric_binary(a, b, .divide) } |
| 175 | d := small_multiply(ad / i64(h), divide_magnitude(bn, g)) or { return big_numeric_binary(a, b, .divide) } |
| 176 | return rational(n, d) |
| 177 | } |
| 178 | |
| 179 | // Continued-fraction comparison avoids overflowing cross-products and avoids |
| 180 | // rounding large exact integers through f64. |
| 181 | fn compare_fraction(a u64, b u64, c u64, d u64) int { |
| 182 | mut an := a |
| 183 | mut ad := b |
| 184 | mut bn := c |
| 185 | mut bd := d |
| 186 | mut direction := 1 |
| 187 | for { |
| 188 | x, y := an / ad, bn / bd |
| 189 | if x < y { return -direction } |
| 190 | if x > y { return direction } |
| 191 | r, s := an % ad, bn % bd |
| 192 | if r == 0 || s == 0 { |
| 193 | if r == s { return 0 } |
| 194 | return if r == 0 { -direction } else { direction } |
| 195 | } |
| 196 | an, ad = ad, r |
| 197 | bn, bd = bd, s |
| 198 | direction = -direction |
| 199 | } |
| 200 | return 0 |
| 201 | } |
| 202 | |
| 203 | fn numeric_compare(a Value, b Value) !int { |
| 204 | if a.tag == .integer && b.tag == .integer { |
| 205 | return if a.whole() < b.whole() { |
| 206 | -1 |
| 207 | } else if a.whole() > b.whole() { |
| 208 | 1 |
| 209 | } else { |
| 210 | 0 |
| 211 | } |
| 212 | } |
| 213 | if is_nonreal(a) || is_nonreal(b) { return failure('ordering requires real numbers') } |
| 214 | if a.tag == .real && b.tag == .real { |
| 215 | x, y := a.floating(), b.floating() |
| 216 | if x != x || y != y { return failure('cannot order NaN') } |
| 217 | return if x < y { |
| 218 | -1 |
| 219 | } else if x > y { |
| 220 | 1 |
| 221 | } else { |
| 222 | 0 |
| 223 | } |
| 224 | } |
| 225 | if a.tag == .real { return -compare_exact_real(b, a.floating())! } |
| 226 | if b.tag == .real { return compare_exact_real(a, b.floating()) } |
| 227 | if is_big_exact(a) || is_big_exact(b) { return big_exact_compare(a, b) } |
| 228 | an, ad := a.as_rational()! |
| 229 | bn, bd := b.as_rational()! |
| 230 | if an < 0 && bn >= 0 { return -1 } |
| 231 | if an >= 0 && bn < 0 { return 1 } |
| 232 | result := compare_fraction(magnitude(an), u64(ad), magnitude(bn), u64(bd)) |
| 233 | return if an < 0 { -result } else { result } |
| 234 | } |
| 235 | |
| 236 | // Compare an exact bounded rational with the actual binary value of a float. |
| 237 | // Rounding the exact operand to f64 would break transitivity near 2^53 and |
| 238 | // would equate e.g. 1/10 with the slightly larger binary approximation of 0.1. |
| 239 | // The significand times a bounded denominator needs at most 116 bits. Wide |
| 240 | // exponent gaps are decided before shifting, so no bignum or heap object is |
| 241 | // needed even for subnormal or very large floating-point values. |
| 242 | fn compare_exact_real(exact Value, floating f64) !int { |
| 243 | if is_big_exact(exact) { return big_compare_real(exact, floating) } |
| 244 | numerator, denominator := exact.as_rational()! |
| 245 | bits := math.f64_bits(floating) |
| 246 | negative := bits >> 63 != 0 |
| 247 | biased := int((bits >> 52) & 0x7ff) |
| 248 | mut significand := bits & u64(0x000fffffffffffff) |
| 249 | if biased == 0x7ff { |
| 250 | if significand != 0 { return failure('cannot order NaN') } |
| 251 | return if negative { 1 } else { -1 } |
| 252 | } |
| 253 | if floating == 0 { |
| 254 | return if numerator < 0 { |
| 255 | -1 |
| 256 | } else if numerator > 0 { |
| 257 | 1 |
| 258 | } else { |
| 259 | 0 |
| 260 | } |
| 261 | } |
| 262 | if numerator == 0 { return if negative { 1 } else { -1 } } |
| 263 | if (numerator < 0) != negative { return if numerator < 0 { -1 } else { 1 } } |
| 264 | exponent := if biased == 0 { -1074 } else { biased - 1023 - 52 } |
| 265 | if biased != 0 { significand |= u64(1) << 52 } |
| 266 | product := u128(significand) * u128(denominator) |
| 267 | n := u128(magnitude(numerator)) |
| 268 | limit := ~u128(0) |
| 269 | mut order := 0 |
| 270 | if exponent >= 0 { |
| 271 | if exponent >= 128 || product > (limit >> exponent) { |
| 272 | order = -1 |
| 273 | } else { |
| 274 | right := product << exponent |
| 275 | order = if n < right { |
| 276 | -1 |
| 277 | } else if n > right { |
| 278 | 1 |
| 279 | } else { |
| 280 | 0 |
| 281 | } |
| 282 | } |
| 283 | } else { |
| 284 | shift := -exponent |
| 285 | if shift >= 128 || n > (limit >> shift) { |
| 286 | order = 1 |
| 287 | } else { |
| 288 | left := n << shift |
| 289 | order = if left < product { |
| 290 | -1 |
| 291 | } else if left > product { |
| 292 | 1 |
| 293 | } else { |
| 294 | 0 |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | return if negative { -order } else { order } |
| 299 | } |
| 300 | |
| 301 | // Direct IEEE rounding avoids rounding the numerator and denominator before |
| 302 | // their division. Small components and power-of-two denominators can still use |
| 303 | // hardware division. For the remaining bounded ratios, the normalized quotient |
| 304 | // needs 53 bits and its shifted numerator needs at most 117 bits, fitting u128. |
| 305 | // Bounded components also ensure a normal, finite result; no subnormal/overflow |
| 306 | // case is possible here. This path allocates no temporary rational or bignum. |
| 307 | fn rational_real(numerator i64, denominator i64) f64 { |
| 308 | n := magnitude(numerator) |
| 309 | d := u64(denominator) |
| 310 | if (n <= u64(1) << 53 && d <= u64(1) << 53) || d & (d - 1) == 0 { |
| 311 | return f64(numerator) / f64(denominator) |
| 312 | } |
| 313 | mut exponent := bitops.len_64(n) - bitops.len_64(d) |
| 314 | mut upper, mut lower := u128(n), u128(d) |
| 315 | if exponent >= 0 { |
| 316 | if upper < lower << exponent { exponent-- } |
| 317 | } else if upper << -exponent < lower { |
| 318 | exponent-- |
| 319 | } |
| 320 | shift := 52 - exponent |
| 321 | if shift >= 0 { upper <<= shift } else { lower <<= -shift } |
| 322 | quotient, remainder := upper / lower, upper % lower |
| 323 | mut mantissa := u64(quotient) |
| 324 | complement := lower - remainder |
| 325 | if remainder > complement || (remainder == complement && mantissa & 1 != 0) { mantissa++ } |
| 326 | if mantissa == u64(1) << 53 { |
| 327 | mantissa >>= 1 |
| 328 | exponent++ |
| 329 | } |
| 330 | mut raw := (u64(exponent + 1023) << 52) | (mantissa & u64(0x000fffffffffffff)) |
| 331 | if numerator < 0 { raw |= u64(1) << 63 } |
| 332 | return math.f64_from_bits(raw) |
| 333 | } |
| 334 | |
| 335 | fn power_ten(n int) !i64 { |
| 336 | if n < 0 || n > 18 { return failure('exact numeric literal exceeds supported range') } |
| 337 | mut value := i64(1) |
| 338 | for _ in 0 .. n { value *= 10 } |
| 339 | return value |
| 340 | } |
| 341 | |
| 342 | fn parse_number(spelling string) !Value { |
| 343 | return parse_number_radix(spelling, 10) |
| 344 | } |
| 345 | |