module scheme fn test_exact_boundaries_and_normalization() { assert rational(exact_min, -2) or { panic(err) }.as_integer() or { panic(err) } == 4611686018427387904 assert rational(exact_min, exact_min) or { panic(err) }.as_integer() or { panic(err) } == 1 assert rational(0, exact_min) or { panic(err) }.as_integer() or { panic(err) } == 0 assert number_text('(- -9223372036854775808 -9223372036854775808)') == '0' assert number_text('(/ -9223372036854775808 -9223372036854775808)') == '1' assert number_text('(* -9223372036854775808 0)') == '0' for source, expected in { '(- 9223372036854775807 -1)': '9223372036854775808' '(- -9223372036854775808 1)': '-9223372036854775809' '(* -9223372036854775808 -1)': '9223372036854775808' '(* -9223372036854775808 2)': '-18446744073709551616' '(* -9223372036854775808 -2)': '18446744073709551616' '(/ -9223372036854775808 -1)': '9223372036854775808' } { assert number_text(source) == expected } } fn test_small_rational_arithmetic_matches_independent_integer_identities() { for an in -9 .. 10 { for bn in -9 .. 10 { for ad in 1 .. 5 { for bd in 1 .. 5 { a := rational(i64(an), i64(ad)) or { panic(err) } b := rational(i64(bn), i64(bd)) or { panic(err) } expected_sum := rational(i64(an * bd + bn * ad), i64(ad * bd)) or { panic(err) } expected_difference := rational(i64(an * bd - bn * ad), i64(ad * bd)) or { panic(err) } expected_product := rational(i64(an * bn), i64(ad * bd)) or { panic(err) } assert eqv(numeric_add(a, b) or { panic(err) }, expected_sum) assert eqv(numeric_subtract(a, b) or { panic(err) }, expected_difference) assert eqv(numeric_multiply(a, b) or { panic(err) }, expected_product) left, right := an * bd, bn * ad expected_order := if left < right { -1 } else if left > right { 1 } else { 0 } assert numeric_compare(a, b) or { panic(err) } == expected_order if bn != 0 { expected_quotient := rational(i64(an * bd), i64(ad * bn)) or { panic(err) } assert eqv(numeric_divide(a, b) or { panic(err) }, expected_quotient) } } } } } } fn number_text(source string) string { mut vm := new() value := vm.eval(source) or { panic(err) } return write(value) or { panic(err) } }