| 1 | module scheme |
| 2 | |
| 3 | import arena |
| 4 | import math |
| 5 | import math.complex |
| 6 | import strconv |
| 7 | |
| 8 | fn compensation_float(text string) f64 { |
| 9 | return math.f64_from_bits(strconv.parse_uint(text, 16, 64) or { panic(err) }) |
| 10 | } |
| 11 | |
| 12 | fn compensation_near(actual f64, expected f64) bool { |
| 13 | if actual == expected { return true } |
| 14 | if math.is_nan(actual) || math.is_inf(actual, 0) { return false } |
| 15 | return math.abs(actual - expected) <= math.abs(expected) * 4e-13 + math.f64_from_bits(8) |
| 16 | } |
| 17 | |
| 18 | fn compensation_oracle_row(region &arena.Arena, row string) { |
| 19 | region.push() |
| 20 | defer { region.pop() } |
| 21 | f := row.split('\t') |
| 22 | assert f.len == 17, row |
| 23 | a, b, c, d := compensation_float(f[2]), compensation_float(f[3]), compensation_float(f[4]), compensation_float(f[5]) |
| 24 | shift := f[6].int() |
| 25 | sum := compensated_product_sum(a, b, c, d) |
| 26 | if f[7] == '1' { |
| 27 | assert sum == 0, f[0] |
| 28 | } |
| 29 | assert compensation_near(sum, compensation_float(f[9])), '${f[0]} sum' |
| 30 | product := finite_complex_product(complex.complex(math.scalbn(a, shift), math.scalbn(c, shift)), |
| 31 | complex.complex(d, b)) or { panic('${f[0]}: ${err}') } |
| 32 | assert compensation_near(product.re, compensation_float(f[10])), '${f[0]} multiply real' |
| 33 | assert compensation_near(product.im, compensation_float(f[11])), '${f[0]} multiply imag' |
| 34 | cs, dl := if math.abs(c) > math.abs(d) { d, c } else { c, d } |
| 35 | rotated := f[0].int() % 2 == 1 |
| 36 | x := if rotated { |
| 37 | complex.complex(-math.scalbn(cs, shift), math.scalbn(a, shift)) |
| 38 | } else { |
| 39 | complex.complex(math.scalbn(a, shift), math.scalbn(cs, shift)) |
| 40 | } |
| 41 | y := complex.complex(b, dl) |
| 42 | _, ae := math.frexp(math.max(math.abs(x.re), math.abs(x.im))) |
| 43 | _, be := math.frexp(math.max(math.abs(y.re), math.abs(y.im))) |
| 44 | ar, ai, br, bi := math.scalbn(x.re, -ae), math.scalbn(x.im, -ae), math.scalbn(y.re, -be), math.scalbn(y.im, -be) |
| 45 | products := [ar * br, ai * bi, ai * br, ar * bi]! |
| 46 | tiny := tiny_product(ar, br, products[0]) || tiny_product(ai, bi, products[1]) |
| 47 | || tiny_product(ai, br, products[2]) || tiny_product(ar, bi, products[3]) |
| 48 | assert tiny == (f[16] == '1'), '${f[0]} existing guard oracle' |
| 49 | // These independent raw-sum counterexamples are physical division cases |
| 50 | // only after normalizing both operands and checking the other diagonal. |
| 51 | if f[14] == '1' || f[15] == '1' { |
| 52 | assert tiny, '${f[0]} existing recovery guard' |
| 53 | } |
| 54 | if !tiny { |
| 55 | for pair in [[products[0], products[1]]!, [products[2], -products[3]]!] { |
| 56 | size := math.max(math.abs(pair[0]), math.abs(pair[1])) |
| 57 | if math.abs(pair[0] + pair[1]) < 0.01 * size { |
| 58 | // Conservative bound, allowing rounded-product endpoint error. |
| 59 | assert size > math.scalbn(1, -513), '${f[0]} normalized cancellation floor' |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | quotient := scaled_complex_divide(x, y) or { panic('${f[0]}: ${err}') } |
| 64 | expected_re := if rotated { -compensation_float(f[13]) } else { compensation_float(f[12]) } |
| 65 | expected_im := if rotated { compensation_float(f[12]) } else { compensation_float(f[13]) } |
| 66 | assert compensation_near(quotient.re, expected_re), '${f[0]} divide real ${quotient.re} != ${expected_re}' |
| 67 | assert compensation_near(quotient.im, expected_im), '${f[0]} divide imag' |
| 68 | } |
| 69 | |
| 70 | fn test_independent_fraction_product_and_normalized_division_cancellation_oracles() { |
| 71 | fixture := $embed_file('testdata/complex_compensation.tsv').to_string() |
| 72 | mut region := arena.new(chunk_size: 1024 * 1024) |
| 73 | defer { region.free() } |
| 74 | mut count := 0 |
| 75 | for row in fixture.split_into_lines() { |
| 76 | if row == '' || row[0] == `#` { continue } |
| 77 | compensation_oracle_row(®ion, row) |
| 78 | region.reset() |
| 79 | assert region.used() == 0 |
| 80 | count++ |
| 81 | } |
| 82 | assert count == 12385 |
| 83 | } |
| 84 | |
| 85 | fn test_parent_epsilon_squared_components_survive_both_frontends_and_reentry() { |
| 86 | mut vm := new() |
| 87 | product := vm.eval('(* (make-rectangular 1.0000000000000002 1.0) (make-rectangular 1.0 -0.9999999999999998))') or { panic(err) } |
| 88 | p := inexact_complex(product) or { panic(err) } |
| 89 | assert p.re == 2 && p.im == math.scalbn(1, -104) |
| 90 | quotient := vm.eval_alt('make-rectangular(1.0000000000000002,1.0) / make-rectangular(0.9999999999999998,-1.0)') or { panic(err) } |
| 91 | q := inexact_complex(quotient) or { panic(err) } |
| 92 | assert compensation_near(q.re, -2.4651903288156624e-32) && q.im == 1.0000000000000002 |
| 93 | vm.register('host-compensated', fn (mut vm VM, args []Value) !Value { |
| 94 | return vm.call(vm.lookup('*')!, args) |
| 95 | }) or { panic(err) } |
| 96 | retained := vm.eval('(define retained (host-compensated 1.0000000000000002+1.0i 1.0-0.9999999999999998i)) (lambda () retained)') or { panic(err) } |
| 97 | gc_collect() |
| 98 | actual := inexact_complex(vm.call(retained, []) or { panic(err) }) or { panic(err) } |
| 99 | assert actual.re == p.re && actual.im == p.im |
| 100 | } |
| 101 | |
| 102 | fn compensation_allocation_body() { |
| 103 | for _ in 0 .. 1000 { |
| 104 | value := compensated_product_sum(1, math.scalbn(1, -940), -1, math.scalbn(1, -940)) |
| 105 | assert value == 0 |
| 106 | z := complex.complex(1.0000000000000002, 1) |
| 107 | product := finite_complex_product(z, complex.complex(1, -0.9999999999999998)) or { panic(err) } |
| 108 | assert product.im == math.scalbn(1, -104) |
| 109 | self := scaled_complex_divide(z, z) or { panic(err) } |
| 110 | assert self.re == 1 && self.im == 0 |
| 111 | conjugate := finite_complex_product(z, complex.complex(z.re, -z.im)) or { panic(err) } |
| 112 | assert conjugate.re > 1 && conjugate.im == 0 |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | fn test_compensation_genuine_zero_and_regular_complex_paths_allocate_nothing() { |
| 117 | mut region := arena.new() |
| 118 | defer { region.free() } |
| 119 | region.push() |
| 120 | before := region.used() |
| 121 | compensation_allocation_body() |
| 122 | assert region.used() == before |
| 123 | region.pop() |
| 124 | $if gcboehm ? { |
| 125 | gc_collect() |
| 126 | heap_before := gc_heap_usage().total_bytes |
| 127 | compensation_allocation_body() |
| 128 | assert gc_heap_usage().total_bytes == heap_before |
| 129 | } |
| 130 | } |
| 131 | |