module scheme import math import arena fn number_text(source string) string { mut vm := new() value := vm.eval(source) or { panic('${source}: ${err}') } return write(value) or { panic(err) } } fn test_gcd_lcm_preserve_integer_bits_until_the_final_inexact_conversion() { mut vm := new() answer := vm.eval("(and (= (gcd 9007199254740993 2.0) 1.0) (= (gcd 9223372036854775808.0 -9223372036854775807) 1.0) (= (lcm 2.0 9007199254740993) (exact->inexact 18014398509481986)) (= (lcm 3 4611686018427387904) 13835058055282163712) (inexact? (gcd 0.0 -5)) (= (gcd -9223372036854775808 0.0) 9223372036854775808.0) (= (lcm 1e300 7 0.0) 0.0))") or { panic(err) } if !answer.is_true() { panic('gcd/lcm rounded an operand or intermediate result') } // Two individually representable operands can have an unrepresentable LCM. if _ := vm.eval('(lcm 1e308 9e307)') { panic('out-of-range inexact LCM was accepted') } else { if !err.msg().contains('inexact result exceeds supported range') { panic(err) } } if _ := vm.eval('(lcm 0 1e300 3/2)') { panic('zero shortcut skipped argument validation') } if (vm.eval('(+ 40 2)') or { panic(err) }).whole() != 42 { panic('VM did not recover') } } fn test_small_mixed_gcd_and_lcm_reductions_allocate_no_arena_storage() { args := [integer(35), real(21), integer(-14)] mut region := arena.new() region.push() before := region.used() for _ in 0 .. 256 { g := integer_gcd_lcm(false, args, true) or { panic(err) } l := integer_gcd_lcm(true, args, true) or { panic(err) } if g.floating() != 7 || l.floating() != 210 { panic('mixed integer reduction failed') } } after := region.used() region.pop() region.free() if after != before { panic('small mixed integer reductions allocated ${after - before} arena bytes') } } fn test_number_library_predicates_signs_and_contagious_inexactness() { for source, expected in { "(list (complex? 1/2) (complex? 'x) (inexact? 1.0) (inexact? 1/2))": '(#t #f #t #f)' '(list (zero? -0.0) (zero? 0/3) (positive? 1/100) (negative? -1/100))': '(#t #t #t #t)' '(list (odd? -3) (even? -4) (odd? -3.0) (even? 4.0) (even? 0))': '(#t #t #t #t #t)' '(list (abs -7) (abs -7/2) (abs -7.0))': '(7 7/2 7.0)' '(list (max 3 4) (min 3 4) (max 3.9 4) (min 3 4.0) (max 3/2 4/3))': '(4 3 4.0 3.0 3/2)' '(list (numerator 6/4) (denominator 6/4) (numerator 1.5) (denominator 1.5) (denominator 0))': '(3 2 3.0 2.0 1)' '(list (gcd) (lcm) (gcd 32 -36) (lcm 32 -36) (lcm 32.0 -36))': '(0 1 4 288 288.0)' '(list (gcd 0 0) (lcm 0 0) (gcd -9223372036854775808 2) (gcd -9223372036854775808 1))': '(0 0 2 1)' '(lcm 9223372036854775807 9223372036854775806 0)': '0' '(list (gcd -0.0 4) (lcm 4.0 0))': '(4.0 0.0)' } { assert number_text(source) == expected, source } mut vm := new() large := vm.eval('(gcd 1.0e100 1.0e100)') or { panic(err) } assert large.kind() == .real assert large.as_real() or { panic(err) } == 1.0e100 } fn test_integer_division_signs_boundaries_and_inexact_inputs() { assert number_text('(list (quotient 13 4) (quotient -13 4) (quotient 13 -4) (quotient -13 -4) (remainder 13 4) (remainder -13 4) (remainder 13 -4) (remainder -13 -4) (modulo 13 4) (modulo -13 4) (modulo 13 -4) (modulo -13 -4))') == '(3 -3 -3 3 1 -1 1 -1 1 3 -3 -1)' assert number_text('(list (remainder -13 -4.0) (modulo 13.0 -4) (quotient 13 4.0) (remainder -9223372036854775808 -1) (modulo -9223372036854775808 -1))') == '(-1.0 -3.0 3.0 0 0)' // Independently verify the quotient/remainder identity and mathematical sign // and magnitude laws, rather than computing the same quotient in the test. mut vm := new() quotient := vm.lookup('quotient') or { panic(err) } remainder := vm.lookup('remainder') or { panic(err) } modulo := vm.lookup('modulo') or { panic(err) } for x in -30 .. 31 { for y in -10 .. 11 { if y == 0 { continue } args := [integer(x), integer(y)] q := vm.call(quotient, args) or { panic(err) }.as_integer() or { panic(err) } r := vm.call(remainder, args) or { panic(err) }.as_integer() or { panic(err) } m := vm.call(modulo, args) or { panic(err) }.as_integer() or { panic(err) } assert x == y * q + r assert magnitude(r) < magnitude(y) assert r == 0 || (r < 0) == (x < 0) assert magnitude(m) < magnitude(y) assert m == 0 || (m < 0) == (y < 0) assert (x - m) % y == 0 } } } fn test_exact_and_inexact_rounding_uses_nearest_even_ties() { for source, expected in { '(list (floor -4.3) (ceiling -4.3) (truncate -4.3) (round -4.3))': '(-5.0 -4.0 -4.0 -4.0)' '(list (round 7/2) (round 5/2) (round -7/2) (round -5/2) (round 7))': '(4 2 -4 -2 7)' '(list (round 3.5) (round 2.5) (round -3.5) (round -2.5))': '(4.0 2.0 -4.0 -2.0)' '(list (floor -5/2) (ceiling -5/2) (truncate -5/2) (floor 5/2) (ceiling 5/2) (truncate 5/2))': '(-3 -2 -2 2 3 2)' '(list (floor 9223372036854775807) (ceiling -9223372036854775808))': '(9223372036854775807 -9223372036854775808)' } { assert number_text(source) == expected, source } for n in -100 .. 101 { for d in 2 .. 21 { value := rational(n, d) or { panic(err) } floor := rounded_number(.floor, value) or { panic(err) }.as_integer() or { panic(err) } ceil := rounded_number(.ceiling, value) or { panic(err) }.as_integer() or { panic(err) } round := rounded_number(.round, value) or { panic(err) }.as_integer() or { panic(err) } assert floor * d <= n && (floor + 1) * d > n assert ceil * d >= n && (ceil - 1) * d < n distance := magnitude(round * d - n) assert distance * 2 <= u64(d) assert distance * 2 != u64(d) || round % 2 == 0 } } } fn test_exact_conversions_preserve_binary_fractions_and_reject_nonfinite_values() { assert number_text('(list (inexact->exact 0.5) (inexact->exact 0.1) (inexact->exact -0.0) (inexact->exact -9223372036854775808.0) (exact->inexact 3/2))') == '(1/2 3602879701896397/36028797018963968 0 -9223372036854775808 1.5)' for power in 0 .. 53 { for n in [i64(-53), i64(-1), i64(0), i64(1), i64(53)] { exact := rational(n, i64(1) << power) or { panic(err) } converted := to_exact(real(exact.as_real() or { panic(err) })) or { panic(err) } assert eqv(converted, exact) } } for bits in [u64(0x7ff0000000000000), u64(0x7ff8000000000001)] { if _ := to_exact(real(math.f64_from_bits(bits))) { assert false } else { assert err is EvalError } } } fn test_powers_square_roots_and_real_transcendentals() { for source, expected in { '(list (expt 2 10) (expt 2 -3) (expt 2/3 3) (expt 2/3 -2) (expt 0 0))': '(1024 1/8 8/27 9/4 1)' '(list (expt -2 63) (expt -1 -9223372036854775808) (sqrt 4) (sqrt 9/16) (sqrt 0))': '(-9223372036854775808 1 2 3/4 0)' '(list (sqrt 4.0) (expt 2.0 3) (expt 2 3.0))': '(2.0 8.0 8.0)' } { assert number_text(source) == expected, source } mut vm := new() for source, expected in { '(exp 1)': math.e '(log 1)': 0.0 '(sin 0)': 0.0 '(cos 0)': 1.0 '(tan 0)': 0.0 '(asin 1)': math.pi / 2 '(acos -1)': math.pi '(atan 1)': math.pi / 4 '(atan 1 -1)': math.pi * 3 / 4 '(sqrt 2)': math.sqrt(2) } { actual := vm.eval(source) or { panic(err) }.as_real() or { panic(err) } assert math.abs(actual - expected) < 1.0e-14, source } for root in u64(0) .. 10000 { square := root * root assert integer_sqrt(square) == root if root > 1 { assert integer_sqrt(square - 1) == root - 1 } assert integer_sqrt(square + 1) == if root == 0 { u64(1) } else { root } } assert integer_sqrt(u64(0xffffffffffffffff)) == u64(0xffffffff) } fn test_number_library_type_arity_and_range_failures_recover() { mut vm := new() for source in ['(zero?)', '(max)', '(quotient 1)', '(round 1 2)', '(atan)', '(atan 1 2 3)', '(odd? 1/2)', '(even? 1.5)', '(quotient 2 0)', '(remainder 2 0)', '(modulo 2 0.0)', '(lcm 0 1/2)', '(gcd 1.0 1/2)', '(expt 0 -1)', '(log 0)', "(inexact? 'x)", '(floor "x")', '(gcd 1 #f)'] { if _ := vm.eval(source) { assert false, source } else { assert err is EvalError, source } assert vm.eval('(+ 40 2)') or { panic(err) }.as_integer() or { panic(err) } == 42 } } fn test_rationalize_selects_the_simplest_fraction_and_preserves_exactness() { for source, expected in { '(rationalize 3/10 1/10)': '1/3' '(rationalize -3/10 1/10)': '-1/3' '(rationalize 3/10 -1/10)': '1/3' '(rationalize 3/10 0)': '3/10' '(rationalize 1/3 1/3)': '0' '(rationalize 10 2)': '8' '(rationalize -10 2)': '-8' '(rationalize -9223372036854775808 1)': '-9223372036854775807' '(rationalize 9223372036854775807 -9223372036854775808)': '0' '(rationalize 0.0 1)': '0.0' '(rationalize 1.0e100 0)': '1e+100' } { assert number_text(source) == expected, source } mut vm := new() inexact := vm.eval('(rationalize 0.3 1/10)') or { panic(err) } assert inexact.kind() == .real assert inexact.as_real() or { panic(err) } == 1.0 / 3.0 // Enumerate small intervals. No rational with a smaller denominator may // lie in the interval, and the result must be the numerator nearest zero // among solutions with its denominator. All products below stay small. for n in -20 .. 21 { for d in 2 .. 11 { value := rational(n, d) or { panic(err) } radius := rational(1, 12) or { panic(err) } result := rationalize_number(value, radius, false) or { panic(err) } rn, rd := result.as_rational() or { panic(err) } assert magnitude(rn * d - n * rd) * 12 <= u64(d * rd) for q in i64(1) .. rd + 1 { for p in i64(-40) .. 41 { if magnitude(p * d - n * q) * 12 <= u64(d * q) { assert q >= rd if q == rd { assert magnitude(p) >= magnitude(rn) } } } } } } }