1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
(let $zero (bigrat (bigint 0) (bigint 1)))
(let $zero-alt (bigrat (bigint 0) (bigint -1)))
(check (= $zero $zero-alt))
(check (= $zero (* (bigrat (bigint -1) (bigint 1)) $zero-alt)))
(let $one (bigrat (bigint 1) (bigint 1)))
(let $two (bigrat (bigint 2) (bigint 1)))
(let $four (bigrat (bigint 4) (bigint 1)))
(let $fourth (bigrat (bigint 1) (bigint 4)))
(check (!= $four $fourth))
(let $neg-one (bigrat (bigint -1) (bigint 1)))
(let $neg-one-alt (bigrat (bigint 1) (bigint -1)))
(check (= $neg-one $neg-one-alt))
(let $neg-two (* $neg-one $two))
; 1 = 0^0 (zero-to-zero edge case)
(check (= $one (pow $zero $zero)))
(check (= $one (pow $zero $zero-alt)))
; 0 = 0^2 (a positive power)
(check (= $zero (pow $zero $two)))
; 1/0 error condition
(fail (pow $zero $neg-one))
(fail (pow $zero $neg-two))
; 4 = 2^2
(check (= $four (pow $two $two)))
; 1/4 == 4^-1
(check (= $fourth (pow $four $neg-one)))
; 1/4 = 2^-2
(check (= $fourth (pow $two $neg-two)))
; 1 = 1^-2
(check (= $one (pow $one $neg-two)))
; 1 = 2^0
(check (= $one (pow $two $zero)))
; 1 = (-2)^0
(check (= $one (pow $neg-two $zero)))
; rational powers are forbidden!
(fail (pow $zero $fourth))
(fail (pow $two $fourth))
; big numbers (kept intentionally small for portability)
; NOTE: We used to exercise exponents near 2^63, but an isize can be smaller on some platforms
(let $thirty-two (bigrat (bigint 32) (bigint 1)))
(check (= $one (pow $one $thirty-two)))