| 1 | module scheme |
| 2 | |
| 3 | import math |
| 4 | import strconv |
| 5 | |
| 6 | fn test_mixed_comparisons_match_independent_exact_rational_oracle() { |
| 7 | // Generated independently with Python Fraction(n, d), Fraction.from_float, |
| 8 | // and a fixed seed. The fixture covers adjacent representable floats near |
| 9 | // rational values, integer boundaries, subnormals, signed zeros, and wide |
| 10 | // exponent differences; the test does not duplicate the comparison algorithm. |
| 11 | fixture := $embed_file('testdata/mixed_numbers.tsv').to_string() |
| 12 | mut checked := 0 |
| 13 | for row in fixture.split_into_lines() { |
| 14 | if row == '' || row.starts_with('#') { continue } |
| 15 | fields := row.split('\t') |
| 16 | n := strconv.parse_int(fields[0], 10, 64) or { panic(err) } |
| 17 | d := strconv.parse_int(fields[1], 10, 64) or { panic(err) } |
| 18 | bits := strconv.parse_uint(fields[2], 16, 64) or { panic(err) } |
| 19 | expected := strconv.atoi(fields[3]) or { panic(err) } |
| 20 | a := rational(n, d) or { panic(err) } |
| 21 | b := real(math.f64_from_bits(bits)) |
| 22 | nearest := strconv.parse_uint(fields[4], 16, 64) or { panic(err) } |
| 23 | assert math.f64_bits(a.as_real() or { panic(err) }) == nearest, row |
| 24 | assert numeric_compare(a, b) or { panic(err) } == expected, row |
| 25 | assert numeric_compare(b, a) or { panic(err) } == -expected, row |
| 26 | checked++ |
| 27 | } |
| 28 | assert checked > 1500 |
| 29 | } |
| 30 | |
| 31 | fn test_mixed_scheme_comparisons_preserve_transitivity_and_exactness() { |
| 32 | mut vm := new() |
| 33 | value := vm.eval('(list (= 9007199254740992 9007199254740992.0) |
| 34 | (= 9007199254740993 9007199254740992.0) |
| 35 | (< 9007199254740992.0 9007199254740993) |
| 36 | (> 9007199254740993 9007199254740992.0) |
| 37 | (= 1/10 0.1) (< 1/10 0.1) (> -1/10 -0.1) |
| 38 | (= 1/2 0.5) (eqv? 1/2 0.5))') or { panic(err) } |
| 39 | assert write(value) or { panic(err) } == '(#t #f #t #t #f #t #t #t #f)' |
| 40 | for n in [exact_min, i64(-1), i64(0), i64(1), exact_max] { |
| 41 | assert numeric_compare(integer(n), real(math.inf(1))) or { panic(err) } == -1 |
| 42 | assert numeric_compare(integer(n), real(math.inf(-1))) or { panic(err) } == 1 |
| 43 | } |
| 44 | for a in [integer(0), real(0.0), rational(1, 3) or { panic(err) }] { |
| 45 | if _ := numeric_compare(a, real(math.nan())) { |
| 46 | assert false |
| 47 | } else { |
| 48 | assert err is EvalError |
| 49 | } |
| 50 | if _ := numeric_compare(real(math.nan()), a) { |
| 51 | assert false |
| 52 | } else { |
| 53 | assert err is EvalError |
| 54 | } |
| 55 | } |
| 56 | assert numeric_compare(real(0.1), real(0.1)) or { panic(err) } == 0 |
| 57 | assert numeric_compare(real(-0.1), real(0.1)) or { panic(err) } == -1 |
| 58 | assert numeric_compare(real(0.1), real(-0.1)) or { panic(err) } == 1 |
| 59 | assert numeric_add(integer(1), real(0.5)) or { panic(err) }.as_real() or { panic(err) } == 1.5 |
| 60 | assert numeric_subtract(real(1.5), integer(1)) or { panic(err) }.as_real() or { panic(err) } == 0.5 |
| 61 | } |
| 62 | |