module scheme import arena import math import math.complex import strconv fn compensation_float(text string) f64 { return math.f64_from_bits(strconv.parse_uint(text, 16, 64) or { panic(err) }) } fn compensation_near(actual f64, expected f64) bool { if actual == expected { return true } if math.is_nan(actual) || math.is_inf(actual, 0) { return false } return math.abs(actual - expected) <= math.abs(expected) * 4e-13 + math.f64_from_bits(8) } fn compensation_oracle_row(region &arena.Arena, row string) { region.push() defer { region.pop() } f := row.split('\t') assert f.len == 17, row a, b, c, d := compensation_float(f[2]), compensation_float(f[3]), compensation_float(f[4]), compensation_float(f[5]) shift := f[6].int() sum := compensated_product_sum(a, b, c, d) if f[7] == '1' { assert sum == 0, f[0] } assert compensation_near(sum, compensation_float(f[9])), '${f[0]} sum' product := finite_complex_product(complex.complex(math.scalbn(a, shift), math.scalbn(c, shift)), complex.complex(d, b)) or { panic('${f[0]}: ${err}') } assert compensation_near(product.re, compensation_float(f[10])), '${f[0]} multiply real' assert compensation_near(product.im, compensation_float(f[11])), '${f[0]} multiply imag' cs, dl := if math.abs(c) > math.abs(d) { d, c } else { c, d } rotated := f[0].int() % 2 == 1 x := if rotated { complex.complex(-math.scalbn(cs, shift), math.scalbn(a, shift)) } else { complex.complex(math.scalbn(a, shift), math.scalbn(cs, shift)) } y := complex.complex(b, dl) _, ae := math.frexp(math.max(math.abs(x.re), math.abs(x.im))) _, be := math.frexp(math.max(math.abs(y.re), math.abs(y.im))) ar, ai, br, bi := math.scalbn(x.re, -ae), math.scalbn(x.im, -ae), math.scalbn(y.re, -be), math.scalbn(y.im, -be) products := [ar * br, ai * bi, ai * br, ar * bi]! tiny := tiny_product(ar, br, products[0]) || tiny_product(ai, bi, products[1]) || tiny_product(ai, br, products[2]) || tiny_product(ar, bi, products[3]) assert tiny == (f[16] == '1'), '${f[0]} existing guard oracle' // These independent raw-sum counterexamples are physical division cases // only after normalizing both operands and checking the other diagonal. if f[14] == '1' || f[15] == '1' { assert tiny, '${f[0]} existing recovery guard' } if !tiny { for pair in [[products[0], products[1]]!, [products[2], -products[3]]!] { size := math.max(math.abs(pair[0]), math.abs(pair[1])) if math.abs(pair[0] + pair[1]) < 0.01 * size { // Conservative bound, allowing rounded-product endpoint error. assert size > math.scalbn(1, -513), '${f[0]} normalized cancellation floor' } } } quotient := scaled_complex_divide(x, y) or { panic('${f[0]}: ${err}') } expected_re := if rotated { -compensation_float(f[13]) } else { compensation_float(f[12]) } expected_im := if rotated { compensation_float(f[12]) } else { compensation_float(f[13]) } assert compensation_near(quotient.re, expected_re), '${f[0]} divide real ${quotient.re} != ${expected_re}' assert compensation_near(quotient.im, expected_im), '${f[0]} divide imag' } fn test_independent_fraction_product_and_normalized_division_cancellation_oracles() { fixture := $embed_file('testdata/complex_compensation.tsv').to_string() mut region := arena.new(chunk_size: 1024 * 1024) defer { region.free() } mut count := 0 for row in fixture.split_into_lines() { if row == '' || row[0] == `#` { continue } compensation_oracle_row(®ion, row) region.reset() assert region.used() == 0 count++ } assert count == 12385 } fn test_parent_epsilon_squared_components_survive_both_frontends_and_reentry() { mut vm := new() product := vm.eval('(* (make-rectangular 1.0000000000000002 1.0) (make-rectangular 1.0 -0.9999999999999998))') or { panic(err) } p := inexact_complex(product) or { panic(err) } assert p.re == 2 && p.im == math.scalbn(1, -104) quotient := vm.eval_alt('make-rectangular(1.0000000000000002,1.0) / make-rectangular(0.9999999999999998,-1.0)') or { panic(err) } q := inexact_complex(quotient) or { panic(err) } assert compensation_near(q.re, -2.4651903288156624e-32) && q.im == 1.0000000000000002 vm.register('host-compensated', fn (mut vm VM, args []Value) !Value { return vm.call(vm.lookup('*')!, args) }) or { panic(err) } retained := vm.eval('(define retained (host-compensated 1.0000000000000002+1.0i 1.0-0.9999999999999998i)) (lambda () retained)') or { panic(err) } gc_collect() actual := inexact_complex(vm.call(retained, []) or { panic(err) }) or { panic(err) } assert actual.re == p.re && actual.im == p.im } fn compensation_allocation_body() { for _ in 0 .. 1000 { value := compensated_product_sum(1, math.scalbn(1, -940), -1, math.scalbn(1, -940)) assert value == 0 z := complex.complex(1.0000000000000002, 1) product := finite_complex_product(z, complex.complex(1, -0.9999999999999998)) or { panic(err) } assert product.im == math.scalbn(1, -104) self := scaled_complex_divide(z, z) or { panic(err) } assert self.re == 1 && self.im == 0 conjugate := finite_complex_product(z, complex.complex(z.re, -z.im)) or { panic(err) } assert conjugate.re > 1 && conjugate.im == 0 } } fn test_compensation_genuine_zero_and_regular_complex_paths_allocate_nothing() { mut region := arena.new() defer { region.free() } region.push() before := region.used() compensation_allocation_body() assert region.used() == before region.pop() $if gcboehm ? { gc_collect() heap_before := gc_heap_usage().total_bytes compensation_allocation_body() assert gc_heap_usage().total_bytes == heap_before } }