vile / scheme / number_library_test.v
235 lines · 224 sloc · 10.11 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1module scheme
2
3import math
4import arena
5
6fn number_text(source string) string {
7 mut vm := new()
8 value := vm.eval(source) or { panic('${source}: ${err}') }
9 return write(value) or { panic(err) }
10}
11
12fn test_gcd_lcm_preserve_integer_bits_until_the_final_inexact_conversion() {
13 mut vm := new()
14 answer := vm.eval("(and
15 (= (gcd 9007199254740993 2.0) 1.0)
16 (= (gcd 9223372036854775808.0 -9223372036854775807) 1.0)
17 (= (lcm 2.0 9007199254740993) (exact->inexact 18014398509481986))
18 (= (lcm 3 4611686018427387904) 13835058055282163712)
19 (inexact? (gcd 0.0 -5))
20 (= (gcd -9223372036854775808 0.0) 9223372036854775808.0)
21 (= (lcm 1e300 7 0.0) 0.0))") or { panic(err) }
22 if !answer.is_true() { panic('gcd/lcm rounded an operand or intermediate result') }
23 // Two individually representable operands can have an unrepresentable LCM.
24 if _ := vm.eval('(lcm 1e308 9e307)') {
25 panic('out-of-range inexact LCM was accepted')
26 } else {
27 if !err.msg().contains('inexact result exceeds supported range') { panic(err) }
28 }
29 if _ := vm.eval('(lcm 0 1e300 3/2)') { panic('zero shortcut skipped argument validation') }
30 if (vm.eval('(+ 40 2)') or { panic(err) }).whole() != 42 { panic('VM did not recover') }
31}
32
33fn test_small_mixed_gcd_and_lcm_reductions_allocate_no_arena_storage() {
34 args := [integer(35), real(21), integer(-14)]
35 mut region := arena.new()
36 region.push()
37 before := region.used()
38 for _ in 0 .. 256 {
39 g := integer_gcd_lcm(false, args, true) or { panic(err) }
40 l := integer_gcd_lcm(true, args, true) or { panic(err) }
41 if g.floating() != 7 || l.floating() != 210 { panic('mixed integer reduction failed') }
42 }
43 after := region.used()
44 region.pop()
45 region.free()
46 if after != before { panic('small mixed integer reductions allocated ${after - before} arena bytes') }
47}
48
49fn test_number_library_predicates_signs_and_contagious_inexactness() {
50 for source, expected in {
51 "(list (complex? 1/2) (complex? 'x) (inexact? 1.0) (inexact? 1/2))": '(#t #f #t #f)'
52 '(list (zero? -0.0) (zero? 0/3) (positive? 1/100) (negative? -1/100))': '(#t #t #t #t)'
53 '(list (odd? -3) (even? -4) (odd? -3.0) (even? 4.0) (even? 0))': '(#t #t #t #t #t)'
54 '(list (abs -7) (abs -7/2) (abs -7.0))': '(7 7/2 7.0)'
55 '(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)'
56 '(list (numerator 6/4) (denominator 6/4) (numerator 1.5) (denominator 1.5) (denominator 0))': '(3 2 3.0 2.0 1)'
57 '(list (gcd) (lcm) (gcd 32 -36) (lcm 32 -36) (lcm 32.0 -36))': '(0 1 4 288 288.0)'
58 '(list (gcd 0 0) (lcm 0 0) (gcd -9223372036854775808 2) (gcd -9223372036854775808 1))': '(0 0 2 1)'
59 '(lcm 9223372036854775807 9223372036854775806 0)': '0'
60 '(list (gcd -0.0 4) (lcm 4.0 0))': '(4.0 0.0)'
61 } {
62 assert number_text(source) == expected, source
63 }
64 mut vm := new()
65 large := vm.eval('(gcd 1.0e100 1.0e100)') or { panic(err) }
66 assert large.kind() == .real
67 assert large.as_real() or { panic(err) } == 1.0e100
68}
69
70fn test_integer_division_signs_boundaries_and_inexact_inputs() {
71 assert number_text('(list (quotient 13 4) (quotient -13 4) (quotient 13 -4) (quotient -13 -4)
72 (remainder 13 4) (remainder -13 4) (remainder 13 -4) (remainder -13 -4)
73 (modulo 13 4) (modulo -13 4) (modulo 13 -4) (modulo -13 -4))') == '(3 -3 -3 3 1 -1 1 -1 1 3 -3 -1)'
74 assert number_text('(list (remainder -13 -4.0) (modulo 13.0 -4) (quotient 13 4.0)
75 (remainder -9223372036854775808 -1) (modulo -9223372036854775808 -1))') == '(-1.0 -3.0 3.0 0 0)'
76 // Independently verify the quotient/remainder identity and mathematical sign
77 // and magnitude laws, rather than computing the same quotient in the test.
78 mut vm := new()
79 quotient := vm.lookup('quotient') or { panic(err) }
80 remainder := vm.lookup('remainder') or { panic(err) }
81 modulo := vm.lookup('modulo') or { panic(err) }
82 for x in -30 .. 31 {
83 for y in -10 .. 11 {
84 if y == 0 { continue }
85 args := [integer(x), integer(y)]
86 q := vm.call(quotient, args) or { panic(err) }.as_integer() or { panic(err) }
87 r := vm.call(remainder, args) or { panic(err) }.as_integer() or { panic(err) }
88 m := vm.call(modulo, args) or { panic(err) }.as_integer() or { panic(err) }
89 assert x == y * q + r
90 assert magnitude(r) < magnitude(y)
91 assert r == 0 || (r < 0) == (x < 0)
92 assert magnitude(m) < magnitude(y)
93 assert m == 0 || (m < 0) == (y < 0)
94 assert (x - m) % y == 0
95 }
96 }
97}
98
99fn test_exact_and_inexact_rounding_uses_nearest_even_ties() {
100 for source, expected in {
101 '(list (floor -4.3) (ceiling -4.3) (truncate -4.3) (round -4.3))': '(-5.0 -4.0 -4.0 -4.0)'
102 '(list (round 7/2) (round 5/2) (round -7/2) (round -5/2) (round 7))': '(4 2 -4 -2 7)'
103 '(list (round 3.5) (round 2.5) (round -3.5) (round -2.5))': '(4.0 2.0 -4.0 -2.0)'
104 '(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)'
105 '(list (floor 9223372036854775807) (ceiling -9223372036854775808))': '(9223372036854775807 -9223372036854775808)'
106 } {
107 assert number_text(source) == expected, source
108 }
109 for n in -100 .. 101 {
110 for d in 2 .. 21 {
111 value := rational(n, d) or { panic(err) }
112 floor := rounded_number(.floor, value) or { panic(err) }.as_integer() or { panic(err) }
113 ceil := rounded_number(.ceiling, value) or { panic(err) }.as_integer() or { panic(err) }
114 round := rounded_number(.round, value) or { panic(err) }.as_integer() or { panic(err) }
115 assert floor * d <= n && (floor + 1) * d > n
116 assert ceil * d >= n && (ceil - 1) * d < n
117 distance := magnitude(round * d - n)
118 assert distance * 2 <= u64(d)
119 assert distance * 2 != u64(d) || round % 2 == 0
120 }
121 }
122}
123
124fn test_exact_conversions_preserve_binary_fractions_and_reject_nonfinite_values() {
125 assert number_text('(list (inexact->exact 0.5) (inexact->exact 0.1) (inexact->exact -0.0)
126 (inexact->exact -9223372036854775808.0) (exact->inexact 3/2))') == '(1/2 3602879701896397/36028797018963968 0 -9223372036854775808 1.5)'
127 for power in 0 .. 53 {
128 for n in [i64(-53), i64(-1), i64(0), i64(1), i64(53)] {
129 exact := rational(n, i64(1) << power) or { panic(err) }
130 converted := to_exact(real(exact.as_real() or { panic(err) })) or { panic(err) }
131 assert eqv(converted, exact)
132 }
133 }
134 for bits in [u64(0x7ff0000000000000), u64(0x7ff8000000000001)] {
135 if _ := to_exact(real(math.f64_from_bits(bits))) {
136 assert false
137 } else {
138 assert err is EvalError
139 }
140 }
141}
142
143fn test_powers_square_roots_and_real_transcendentals() {
144 for source, expected in {
145 '(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)'
146 '(list (expt -2 63) (expt -1 -9223372036854775808) (sqrt 4) (sqrt 9/16) (sqrt 0))': '(-9223372036854775808 1 2 3/4 0)'
147 '(list (sqrt 4.0) (expt 2.0 3) (expt 2 3.0))': '(2.0 8.0 8.0)'
148 } {
149 assert number_text(source) == expected, source
150 }
151 mut vm := new()
152 for source, expected in {
153 '(exp 1)': math.e
154 '(log 1)': 0.0
155 '(sin 0)': 0.0
156 '(cos 0)': 1.0
157 '(tan 0)': 0.0
158 '(asin 1)': math.pi / 2
159 '(acos -1)': math.pi
160 '(atan 1)': math.pi / 4
161 '(atan 1 -1)': math.pi * 3 / 4
162 '(sqrt 2)': math.sqrt(2)
163 } {
164 actual := vm.eval(source) or { panic(err) }.as_real() or { panic(err) }
165 assert math.abs(actual - expected) < 1.0e-14, source
166 }
167 for root in u64(0) .. 10000 {
168 square := root * root
169 assert integer_sqrt(square) == root
170 if root > 1 {
171 assert integer_sqrt(square - 1) == root - 1
172 }
173 assert integer_sqrt(square + 1) == if root == 0 { u64(1) } else { root }
174 }
175 assert integer_sqrt(u64(0xffffffffffffffff)) == u64(0xffffffff)
176}
177
178fn test_number_library_type_arity_and_range_failures_recover() {
179 mut vm := new()
180 for source in ['(zero?)', '(max)', '(quotient 1)', '(round 1 2)', '(atan)', '(atan 1 2 3)',
181 '(odd? 1/2)', '(even? 1.5)', '(quotient 2 0)', '(remainder 2 0)', '(modulo 2 0.0)',
182 '(lcm 0 1/2)', '(gcd 1.0 1/2)', '(expt 0 -1)', '(log 0)', "(inexact? 'x)", '(floor "x")',
183 '(gcd 1 #f)'] {
184 if _ := vm.eval(source) {
185 assert false, source
186 } else {
187 assert err is EvalError, source
188 }
189 assert vm.eval('(+ 40 2)') or { panic(err) }.as_integer() or { panic(err) } == 42
190 }
191}
192
193fn test_rationalize_selects_the_simplest_fraction_and_preserves_exactness() {
194 for source, expected in {
195 '(rationalize 3/10 1/10)': '1/3'
196 '(rationalize -3/10 1/10)': '-1/3'
197 '(rationalize 3/10 -1/10)': '1/3'
198 '(rationalize 3/10 0)': '3/10'
199 '(rationalize 1/3 1/3)': '0'
200 '(rationalize 10 2)': '8'
201 '(rationalize -10 2)': '-8'
202 '(rationalize -9223372036854775808 1)': '-9223372036854775807'
203 '(rationalize 9223372036854775807 -9223372036854775808)': '0'
204 '(rationalize 0.0 1)': '0.0'
205 '(rationalize 1.0e100 0)': '1e+100'
206 } {
207 assert number_text(source) == expected, source
208 }
209 mut vm := new()
210 inexact := vm.eval('(rationalize 0.3 1/10)') or { panic(err) }
211 assert inexact.kind() == .real
212 assert inexact.as_real() or { panic(err) } == 1.0 / 3.0
213 // Enumerate small intervals. No rational with a smaller denominator may
214 // lie in the interval, and the result must be the numerator nearest zero
215 // among solutions with its denominator. All products below stay small.
216 for n in -20 .. 21 {
217 for d in 2 .. 11 {
218 value := rational(n, d) or { panic(err) }
219 radius := rational(1, 12) or { panic(err) }
220 result := rationalize_number(value, radius, false) or { panic(err) }
221 rn, rd := result.as_rational() or { panic(err) }
222 assert magnitude(rn * d - n * rd) * 12 <= u64(d * rd)
223 for q in i64(1) .. rd + 1 {
224 for p in i64(-40) .. 41 {
225 if magnitude(p * d - n * q) * 12 <= u64(d * q) {
226 assert q >= rd
227 if q == rd {
228 assert magnitude(p) >= magnitude(rn)
229 }
230 }
231 }
232 }
233 }
234 }
235}
236