module scheme import math import strconv fn test_mixed_comparisons_match_independent_exact_rational_oracle() { // Generated independently with Python Fraction(n, d), Fraction.from_float, // and a fixed seed. The fixture covers adjacent representable floats near // rational values, integer boundaries, subnormals, signed zeros, and wide // exponent differences; the test does not duplicate the comparison algorithm. fixture := $embed_file('testdata/mixed_numbers.tsv').to_string() mut checked := 0 for row in fixture.split_into_lines() { if row == '' || row.starts_with('#') { continue } fields := row.split('\t') n := strconv.parse_int(fields[0], 10, 64) or { panic(err) } d := strconv.parse_int(fields[1], 10, 64) or { panic(err) } bits := strconv.parse_uint(fields[2], 16, 64) or { panic(err) } expected := strconv.atoi(fields[3]) or { panic(err) } a := rational(n, d) or { panic(err) } b := real(math.f64_from_bits(bits)) nearest := strconv.parse_uint(fields[4], 16, 64) or { panic(err) } assert math.f64_bits(a.as_real() or { panic(err) }) == nearest, row assert numeric_compare(a, b) or { panic(err) } == expected, row assert numeric_compare(b, a) or { panic(err) } == -expected, row checked++ } assert checked > 1500 } fn test_mixed_scheme_comparisons_preserve_transitivity_and_exactness() { mut vm := new() value := vm.eval('(list (= 9007199254740992 9007199254740992.0) (= 9007199254740993 9007199254740992.0) (< 9007199254740992.0 9007199254740993) (> 9007199254740993 9007199254740992.0) (= 1/10 0.1) (< 1/10 0.1) (> -1/10 -0.1) (= 1/2 0.5) (eqv? 1/2 0.5))') or { panic(err) } assert write(value) or { panic(err) } == '(#t #f #t #t #f #t #t #t #f)' for n in [exact_min, i64(-1), i64(0), i64(1), exact_max] { assert numeric_compare(integer(n), real(math.inf(1))) or { panic(err) } == -1 assert numeric_compare(integer(n), real(math.inf(-1))) or { panic(err) } == 1 } for a in [integer(0), real(0.0), rational(1, 3) or { panic(err) }] { if _ := numeric_compare(a, real(math.nan())) { assert false } else { assert err is EvalError } if _ := numeric_compare(real(math.nan()), a) { assert false } else { assert err is EvalError } } assert numeric_compare(real(0.1), real(0.1)) or { panic(err) } == 0 assert numeric_compare(real(-0.1), real(0.1)) or { panic(err) } == -1 assert numeric_compare(real(0.1), real(-0.1)) or { panic(err) } == 1 assert numeric_add(integer(1), real(0.5)) or { panic(err) }.as_real() or { panic(err) } == 1.5 assert numeric_subtract(real(1.5), integer(1)) or { panic(err) }.as_real() or { panic(err) } == 0.5 }