Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 247 lines (192 sloc) 8.217 kB
025316a @colomon Move the infix operators into Math::BigInt, and add lots of tests.
authored
1 use Math::BigInt;
2 use Test;
3
4 plan *;
5
6cc43b1 @colomon Multiply up to spec.
authored
6 # adding BigInts and BigInts
025316a @colomon Move the infix operators into Math::BigInt, and add lots of tests.
authored
7 {
8 my $a = Math::BigInt.new("100000000000000000");
9 my $b = Math::BigInt.new("1");
10
11 my $c = $a + $b;
12 isa_ok $c, Math::BigInt, "100000000000000000 + 1 is a BigInt";
13 is $c, "100000000000000001", "and it's 100000000000000001";
14 }
15
1d897d3 @colomon Simple tests for adding negative numbers.
authored
16 {
6e8a50c @colomon Fix addition to properly support negative numbers.
authored
17 my @numbers = (-20, -11, -1, 0, 1, 3, 13, 27);
18
19 for @numbers X @numbers -> $a-int, $b-int {
20 my $a = Math::BigInt.new($a-int);
21 my $b = Math::BigInt.new($b-int);
22
23 my $c = $a + $b;
24 isa_ok $c, Math::BigInt, "long sum of $a-int and $b-int is a BigInt";
25 is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
26
27 $c = $b + $a;
28 isa_ok $c, Math::BigInt, "long sum of $b-int and $a-int is a BigInt";
29 is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
30
31 $c = $a + $b-int;
32 isa_ok $c, Math::BigInt, "long sum of $a-int and $b-int is a BigInt";
33 is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
34
35 $c = $a-int + $b;
36 isa_ok $c, Math::BigInt, "long sum of $a-int and $b-int is a BigInt";
37 is $c, $a-int + $b-int, "and it's { $a-int + $b-int }";
38 }
eac2ccc @colomon Subtraction, too.
authored
39
40 for @numbers X @numbers -> $a-int, $b-int {
41 my $a = Math::BigInt.new($a-int);
42 my $b = Math::BigInt.new($b-int);
43
44 my $c = $a - $b;
45 isa_ok $c, Math::BigInt, "long difference of $a-int and $b-int is a BigInt";
46 is $c, $a-int - $b-int, "and it's { $a-int - $b-int }";
47
48 $c = $b - $a;
49 isa_ok $c, Math::BigInt, "long difference of $b-int and $a-int is a BigInt";
50 is $c, $b-int - $a-int, "and it's { $b-int - $a-int }";
51
52 $c = $a - $b-int;
53 isa_ok $c, Math::BigInt, "long difference of $a-int and $b-int is a BigInt";
54 is $c, $a-int - $b-int, "and it's { $a-int - $b-int }";
55
56 $c = $a-int - $b;
57 isa_ok $c, Math::BigInt, "long difference of $a-int and $b-int is a BigInt";
58 is $c, $a-int - $b-int, "and it's { $a-int - $b-int }";
59 }
6d8cb98 @colomon Make multiplication honor negatives too.
authored
60
61 for @numbers X @numbers -> $a-int, $b-int {
62 my $a = Math::BigInt.new($a-int);
63 my $b = Math::BigInt.new($b-int);
64
65 my $c = $a * $b;
66 isa_ok $c, Math::BigInt, "long product of $a-int and $b-int is a BigInt";
67 is $c, $a-int * $b-int, "and it's { $a-int * $b-int }";
68
69 $c = $b * $a;
70 isa_ok $c, Math::BigInt, "long product of $b-int and $a-int is a BigInt";
71 is $c, $b-int * $a-int, "and it's { $b-int * $a-int }";
72
73 $c = $a * $b-int;
74 isa_ok $c, Math::BigInt, "long product of $a-int and $b-int is a BigInt";
75 is $c, $a-int * $b-int, "and it's { $a-int * $b-int }";
76
77 $c = $a-int * $b;
78 isa_ok $c, Math::BigInt, "long product of $a-int and $b-int is a BigInt";
79 is $c, $a-int * $b-int, "and it's { $a-int * $b-int }";
80 }
dc96ebc @colomon Get div sort of working. (Has incorrect rounding on negative results.)
authored
81
82 # in the long run, this should work for negative numbers as well
83 for @numbers.grep(*>0) X @numbers.grep(*>0) -> $a-int, $b-int {
84 my $a = Math::BigInt.new($a-int);
85 my $b = Math::BigInt.new($b-int);
86
87 if $a-int != 0
88 {
89 my $c = $b div $a;
90 isa_ok $c, Math::BigInt, "long dividend of $b-int and $a-int is a BigInt";
91 is $c, $b-int div $a-int, "and it's { $b-int div $a-int }";
92 }
93
94 if $b-int != 0
95 {
96 my $c = $a div $b;
97 isa_ok $c, Math::BigInt, "long dividend of $a-int and $b-int is a BigInt";
98 is $c, $a-int div $b-int, "and it's { $a-int div $b-int }";
99
100 $c = $a div $b-int;
101 isa_ok $c, Math::BigInt, "long dividend of $a-int and $b-int is a BigInt";
102 is $c, $a-int div $b-int, "and it's { $a-int div $b-int }";
103 }
104 }
105
106 todo "Negative div not rounding properly", 1;
107 is (-4)L div 3L, -4 div 3, "-4L div 3L should be the same as -4 div 3";
1d897d3 @colomon Simple tests for adding negative numbers.
authored
108 }
109
025316a @colomon Move the infix operators into Math::BigInt, and add lots of tests.
authored
110 # adding normal ints and BigInts
111 {
112 my $a = Math::BigInt.new("100000000000000000");
113
114 my $b = $a + 1;
115 isa_ok $b, Math::BigInt, "100000000000000000 + 1 is a BigInt";
116 is $b, "100000000000000001", "and it's 100000000000000001";
117
118 $a = 1 + $b;
119 isa_ok $a, Math::BigInt, "1 + 100000000000000001 is a BigInt";
120 is $a, "100000000000000002", "and it's 100000000000000002";
121
122 todo "+= ignoring new operators", 2;
123 $a += 3;
124 isa_ok $a, Math::BigInt, "1 + 100000000000000001 is a BigInt";
125 is $a, "100000000000000003", "and it's 100000000000000003";
126 }
127
6cc43b1 @colomon Multiply up to spec.
authored
128 # subtracting BigInts and BigInts
129 {
130 my $a = Math::BigInt.new("100000000000000000");
131 my $b = Math::BigInt.new("1");
132
133 my $c = $a - $b;
134 isa_ok $c, Math::BigInt, "100000000000000000 - 1 is a BigInt";
135 is $c, "99999999999999999", "and it's 99999999999999999";
136
137 $c = $b - $a;
138 isa_ok $c, Math::BigInt, "1 - 100000000000000000 is a BigInt";
139 is $c, "-99999999999999999", "and it's -99999999999999999";
140 }
141
142 # subtracting normal ints and BigInts
143 {
144 my $a = Math::BigInt.new("100000000000000000");
145
146 my $b = $a - 1;
147 isa_ok $b, Math::BigInt, "100000000000000000 - 1 is a BigInt";
148 is $b, "99999999999999999", "and it's 99999999999999999";
149
150 $a = 1 - $b;
151 isa_ok $a, Math::BigInt, "1 - 99999999999999999 is a BigInt";
152 is $a, "-99999999999999998", "and it's -99999999999999998";
153
154 todo "-= ignoring new operators", 2;
155 $a -= 3;
156 isa_ok $a, Math::BigInt, "-99999999999999998 - 3 is a BigInt";
157 is $a, "-100000000000000001", "and it's -100000000000000001";
158 }
159
160 # multiplying BigInts and BigInts
161 {
162 my $a = Math::BigInt.new("100000000000000000");
163 my $b = Math::BigInt.new("2");
164
165 my $c = $a * $b;
166 isa_ok $c, Math::BigInt, "100000000000000000 * 2 is a BigInt";
167 is $c, "200000000000000000", "and it's 200000000000000000";
168 }
169
170 # multiplying normal ints and BigInts
171 {
172 my $a = Math::BigInt.new("100000000000000000");
173
174 my $b = $a * 2;
175 isa_ok $b, Math::BigInt, "100000000000000000 * 2 is a BigInt";
176 is $b, "200000000000000000", "and it's 200000000000000000";
177
178 $a = 2 * $b;
179 isa_ok $a, Math::BigInt, "2 * 200000000000000000 is a BigInt";
180 is $a, "400000000000000000", "and it's 400000000000000000";
181
182 todo "*= ignoring new operators", 2;
183 $a *= 2;
184 isa_ok $a, Math::BigInt, "400000000000000000 * 2 is a BigInt";
185 is $a, "800000000000000000", "and it's 800000000000000000";
186 }
187
de7a5ce @colomon Add infix:<div>.
authored
188 # div BigInts and BigInts
189 {
190 my $a = Math::BigInt.new("43452454234524532423");
191 my $b = Math::BigInt.new("2");
192
193 my $c = ($a * $b) div $b;
194 isa_ok $c, Math::BigInt, "43452454234524532423 * 2 div 2 is a BigInt";
195 is $c, "43452454234524532423", "and it's 43452454234524532423";
196
197 $c = ($a * $b) div 2;
198 isa_ok $c, Math::BigInt, "43452454234524532423 * 2 div 2 is a BigInt";
199 is $c, "43452454234524532423", "and it's 43452454234524532423";
200 }
201
17ff128 @colomon Add infix:<%>.
authored
202 # modulo BigInts and BigInts
203 {
204 my $a = Math::BigInt.new("43452454234524532423");
205 my $b = Math::BigInt.new("2");
206
207 my $c = $a % $b;
208 isa_ok $c, Math::BigInt, "43452454234524532423 % 2 is a BigInt";
209 is $c, "1", "and it's 1";
210
211 $c = $a % 2;
212 isa_ok $c, Int, "43452454234524532423 % 2 is an Int";
213 is $c, 1, "and it's 1";
214 }
215
5627557 @colomon Add Math::BigInt.Bool and infix:<**>.
authored
216 # power!
217 {
218 my @powers-of-three := Math::BigInt.new(1), * * 3 ... *;
219 for ^20 -> $i {
e4158c1 @colomon Switch postfix:<¹> to postfix:<L> at sorear++'s suggestion.
authored
220 is @powers-of-three[$i], 3L ** $i, "3 ** $i agrees (integer power)";
221 is @powers-of-three[$i], 3L ** Math::BigInt.new($i), "3 ** $i agrees (Math::BigInt power)";
5627557 @colomon Add Math::BigInt.Bool and infix:<**>.
authored
222 }
18a8910 @colomon Add a test which would fail if infix:<**> was just falling back to Num.
authored
223
e4158c1 @colomon Switch postfix:<¹> to postfix:<L> at sorear++'s suggestion.
authored
224 is (2L)**80, "1208925819614629174706176", "2**80 is 1208925819614629174706176";
5627557 @colomon Add Math::BigInt.Bool and infix:<**>.
authored
225 }
226
5affaa7 @colomon Add gcd routine which calls into BigDigits for efficiency.
authored
227 # gcd!
228 {
b8b20cb @colomon Switch gcd to an operator.
authored
229 isa_ok 25L gcd 24L, Math::BigInt, "25L gcd 24L is a Math::BigInt";
230 is 25L gcd 24L, 1, "gcd of 25 and 24 is 1";
231 is 25L gcd 5L, 5, "gcd of 25 and 5 is 5";
232 is 25L gcd 30L, 5, "gcd of 25 and 30 is 5";
233 isa_ok 25L gcd 24, Math::BigInt, "25L gcd 24 is a Math::BigInt";
234 is 25L gcd 24, 1, "gcd of 25 and 24 is 1";
235 is 25L gcd 5, 5, "gcd of 25 and 5 is 5";
236 is 25L gcd 30, 5, "gcd of 25 and 30 is 5";
237 isa_ok 25 gcd 24L, Math::BigInt, "25 gcd 24L is a Math::BigInt";
238 is 25 gcd 24L, 1, "gcd of 25 and 24 is 1";
239 is 25 gcd 5L, 5, "gcd of 25 and 5 is 5";
240 is 25 gcd 30L, 5, "gcd of 25 and 30 is 5";
5affaa7 @colomon Add gcd routine which calls into BigDigits for efficiency.
authored
241 }
5627557 @colomon Add Math::BigInt.Bool and infix:<**>.
authored
242
17ff128 @colomon Add infix:<%>.
authored
243
244
6cc43b1 @colomon Multiply up to spec.
authored
245
025316a @colomon Move the infix operators into Math::BigInt, and add lots of tests.
authored
246
247 done;
Something went wrong with that request. Please try again.