Commit | Line | Data |
---|---|---|
6320cdc0 | 1 | #!perl |
184f15d5 JH |
2 | |
3 | use strict; | |
11c955be SH |
4 | use warnings; |
5 | ||
d93f0209 | 6 | use Test::More tests => 202; |
184f15d5 | 7 | |
12fc2493 | 8 | # basic testing of Math::BigRat |
184f15d5 JH |
9 | |
10 | use Math::BigRat; | |
6de7f0cc JH |
11 | use Math::BigInt; |
12 | use Math::BigFloat; | |
13 | ||
14 | # shortcuts | |
11c955be | 15 | my $mbr = 'Math::BigRat'; |
6de7f0cc JH |
16 | my $mbi = 'Math::BigInt'; |
17 | my $mbf = 'Math::BigFloat'; | |
184f15d5 | 18 | |
11c955be | 19 | my ($x, $y, $z); |
184f15d5 | 20 | |
11c955be SH |
21 | $x = Math::BigRat->new(1234); |
22 | is($x, 1234); | |
23 | isa_ok($x, 'Math::BigRat'); | |
24 | is($x->isa('Math::BigFloat'), 0); | |
25 | is($x->isa('Math::BigInt'), 0); | |
8f675a64 JH |
26 | |
27 | ############################################################################## | |
6de7f0cc | 28 | # new and bnorm() |
8f675a64 | 29 | |
6320cdc0 | 30 | foreach my $func (qw/ new bnorm /) { |
11c955be SH |
31 | $x = $mbr->$func(1234); |
32 | is($x, 1234, qq|\$x = $mbr->$func(1234)|); | |
c32198f6 | 33 | |
11c955be SH |
34 | $x = $mbr->$func("1234/1"); |
35 | is($x, 1234, qq|\$x = $mbr->$func("1234/1")|); | |
c32198f6 | 36 | |
11c955be SH |
37 | $x = $mbr->$func("1234/2"); |
38 | is($x, 617, qq|\$x = $mbr->$func("1234/2")|); | |
c32198f6 | 39 | |
11c955be SH |
40 | $x = $mbr->$func("100/1.0"); |
41 | is($x, 100, qq|\$x = $mbr->$func("100/1.0")|); | |
c6c613ed | 42 | |
11c955be SH |
43 | $x = $mbr->$func("10.0/1.0"); |
44 | is($x, 10, qq|\$x = $mbr->$func("10.0/1.0")|); | |
ccbfef19 | 45 | |
11c955be SH |
46 | $x = $mbr->$func("0.1/10"); |
47 | is($x, "1/100", qq|\$x = $mbr->$func("0.1/10")|); | |
6de7f0cc | 48 | |
11c955be SH |
49 | $x = $mbr->$func("0.1/0.1"); |
50 | is($x, "1", qq|\$x = $mbr->$func("0.1/0.1")|); | |
6de7f0cc | 51 | |
11c955be SH |
52 | $x = $mbr->$func("1e2/10"); |
53 | is($x, 10, qq|\$x = $mbr->$func("1e2/10")|); | |
9b924220 | 54 | |
11c955be SH |
55 | $x = $mbr->$func("5/1e2"); |
56 | is($x, "1/20", qq|\$x = $mbr->$func("5/1e2")|); | |
a4e2b1c6 | 57 | |
11c955be SH |
58 | $x = $mbr->$func("1e2/1e1"); |
59 | is($x, 10, qq|\$x = $mbr->$func("1e2/1e1")|); | |
12fc2493 | 60 | |
11c955be SH |
61 | $x = $mbr->$func("1 / 3"); |
62 | is($x, "1/3", qq|\$x = $mbr->$func("1 / 3")|); | |
a4e2b1c6 | 63 | |
11c955be SH |
64 | $x = $mbr->$func("-1 / 3"); |
65 | is($x, "-1/3", qq|\$x = $mbr->$func("-1 / 3")|); | |
a4e2b1c6 | 66 | |
11c955be SH |
67 | $x = $mbr->$func("NaN"); |
68 | is($x, "NaN", qq|\$x = $mbr->$func("NaN")|); | |
8f675a64 | 69 | |
11c955be SH |
70 | $x = $mbr->$func("inf"); |
71 | is($x, "inf", qq|\$x = $mbr->$func("inf")|); | |
b68b7ab1 | 72 | |
11c955be SH |
73 | $x = $mbr->$func("-inf"); |
74 | is($x, "-inf", qq|\$x = $mbr->$func("-inf")|); | |
b68b7ab1 | 75 | |
11c955be SH |
76 | $x = $mbr->$func("1/"); |
77 | is($x, "NaN", qq|\$x = $mbr->$func("1/")|); | |
8f675a64 | 78 | |
11c955be SH |
79 | $x = $mbr->$func("0x7e"); |
80 | is($x, 126, qq|\$x = $mbr->$func("0x7e")|); | |
8f675a64 | 81 | |
11c955be SH |
82 | # input ala "1+1/3" isn"t parsed ok yet |
83 | $x = $mbr->$func("1+1/3"); | |
84 | is($x, "NaN", qq|\$x = $mbr->$func("1+1/3")|); | |
8f675a64 | 85 | |
11c955be SH |
86 | $x = $mbr->$func("1/1.2"); |
87 | is($x, "5/6", qq|\$x = $mbr->$func("1/1.2")|); | |
8f675a64 | 88 | |
11c955be SH |
89 | $x = $mbr->$func("1.3/1.2"); |
90 | is($x, "13/12", qq|\$x = $mbr->$func("1.3/1.2")|); | |
8f675a64 | 91 | |
11c955be SH |
92 | $x = $mbr->$func("1.2/1"); |
93 | is($x, "6/5", qq|\$x = $mbr->$func("1.2/1")|); | |
9b924220 | 94 | |
11c955be SH |
95 | ############################################################################ |
96 | # other classes as input | |
184f15d5 | 97 | |
11c955be SH |
98 | $x = $mbr->$func($mbi->new(1231)); |
99 | is($x, "1231", qq|\$x = $mbr->$func($mbi->new(1231))|); | |
100 | ||
101 | $x = $mbr->$func($mbf->new(1232)); | |
102 | is($x, "1232", qq|\$x = $mbr->$func($mbf->new(1232))|); | |
184f15d5 | 103 | |
11c955be SH |
104 | $x = $mbr->$func($mbf->new(1232.3)); |
105 | is($x, "12323/10", qq|\$x = $mbr->$func($mbf->new(1232.3))|); | |
106 | } | |
184f15d5 | 107 | |
11c955be SH |
108 | my $n = 'numerator'; |
109 | my $d = 'denominator'; | |
110 | ||
6320cdc0 | 111 | $x = $mbr->new('-0'); |
11c955be | 112 | is($x, '0'); |
6320cdc0 | 113 | is($x->$n(), '0'); |
11c955be SH |
114 | is($x->$d(), '1'); |
115 | ||
6320cdc0 | 116 | $x = $mbr->new('NaN'); |
11c955be SH |
117 | is($x, 'NaN'); is($x->$n(), 'NaN'); |
118 | is($x->$d(), 'NaN'); | |
119 | ||
6320cdc0 | 120 | $x = $mbr->new('-NaN'); |
11c955be SH |
121 | is($x, 'NaN'); is($x->$n(), 'NaN'); |
122 | is($x->$d(), 'NaN'); | |
123 | ||
6320cdc0 | 124 | $x = $mbr->new('-1r4'); |
11c955be SH |
125 | is($x, 'NaN'); is($x->$n(), 'NaN'); |
126 | is($x->$d(), 'NaN'); | |
184f15d5 | 127 | |
6320cdc0 | 128 | $x = $mbr->new('+inf'); |
11c955be SH |
129 | is($x, 'inf'); is($x->$n(), 'inf'); |
130 | is($x->$d(), '1'); | |
184f15d5 | 131 | |
6320cdc0 | 132 | $x = $mbr->new('-inf'); |
11c955be SH |
133 | is($x, '-inf'); |
134 | is($x->$n(), '-inf'); | |
135 | is($x->$d(), '1'); | |
136 | ||
6320cdc0 | 137 | $x = $mbr->new('123a4'); |
11c955be SH |
138 | is($x, 'NaN'); |
139 | is($x->$n(), 'NaN'); | |
140 | is($x->$d(), 'NaN'); | |
141 | ||
142 | # wrong inputs | |
6320cdc0 | 143 | $x = $mbr->new('1e2e2'); |
11c955be SH |
144 | is($x, 'NaN'); |
145 | is($x->$n(), 'NaN'); | |
146 | is($x->$d(), 'NaN'); | |
147 | ||
6320cdc0 | 148 | $x = $mbr->new('1+2+2'); |
11c955be SH |
149 | is($x, 'NaN'); |
150 | is($x->$n(), 'NaN'); | |
151 | is($x->$d(), 'NaN'); | |
152 | ||
153 | # failed due to BigFloat bug | |
6320cdc0 | 154 | $x = $mbr->new('1.2.2'); |
11c955be SH |
155 | is($x, 'NaN'); |
156 | is($x->$n(), 'NaN'); | |
157 | is($x->$d(), 'NaN'); | |
158 | ||
159 | is($mbr->new('123a4'), 'NaN'); | |
160 | is($mbr->new('123e4'), '1230000'); | |
161 | is($mbr->new('-NaN'), 'NaN'); | |
162 | is($mbr->new('NaN'), 'NaN'); | |
163 | is($mbr->new('+inf'), 'inf'); | |
164 | is($mbr->new('-inf'), '-inf'); | |
184f15d5 | 165 | |
11c955be SH |
166 | ############################################################################## |
167 | # two Bigints | |
184f15d5 | 168 | |
11c955be SH |
169 | is($mbr->new($mbi->new(3), $mbi->new(7))->badd(1), '10/7'); |
170 | is($mbr->new($mbi->new(-13), $mbi->new(7)), '-13/7'); | |
171 | is($mbr->new($mbi->new(13), $mbi->new(-7)), '-13/7'); | |
172 | is($mbr->new($mbi->new(-13), $mbi->new(-7)), '13/7'); | |
184f15d5 | 173 | |
11c955be SH |
174 | ############################################################################## |
175 | # mixed arguments | |
8f675a64 | 176 | |
11c955be SH |
177 | is($mbr->new('3/7')->badd(1), '10/7'); |
178 | is($mbr->new('3/10')->badd(1.1), '7/5'); | |
179 | is($mbr->new('3/7')->badd($mbi->new(1)), '10/7'); | |
180 | is($mbr->new('3/10')->badd($mbf->new('1.1')), '7/5'); | |
8f675a64 | 181 | |
11c955be SH |
182 | is($mbr->new('3/7')->bsub(1), '-4/7'); |
183 | is($mbr->new('3/10')->bsub(1.1), '-4/5'); | |
184 | is($mbr->new('3/7')->bsub($mbi->new(1)), '-4/7'); | |
185 | is($mbr->new('3/10')->bsub($mbf->new('1.1')), '-4/5'); | |
8f675a64 | 186 | |
11c955be SH |
187 | is($mbr->new('3/7')->bmul(1), '3/7'); |
188 | is($mbr->new('3/10')->bmul(1.1), '33/100'); | |
189 | is($mbr->new('3/7')->bmul($mbi->new(1)), '3/7'); | |
190 | is($mbr->new('3/10')->bmul($mbf->new('1.1')), '33/100'); | |
8f675a64 | 191 | |
11c955be SH |
192 | is($mbr->new('3/7')->bdiv(1), '3/7'); |
193 | is($mbr->new('3/10')->bdiv(1.1), '3/11'); | |
194 | is($mbr->new('3/7')->bdiv($mbi->new(1)), '3/7'); | |
195 | is($mbr->new('3/10')->bdiv($mbf->new('1.1')), '3/11'); | |
93c87d9d | 196 | |
11c955be SH |
197 | ############################################################################## |
198 | $x = $mbr->new('1/4'); | |
199 | $y = $mbr->new('1/3'); | |
93c87d9d | 200 | |
11c955be SH |
201 | is($x + $y, '7/12'); |
202 | is($x * $y, '1/12'); | |
203 | is($x / $y, '3/4'); | |
93c87d9d | 204 | |
11c955be SH |
205 | $x = $mbr->new('7/5'); |
206 | $x *= '3/2'; | |
207 | is($x, '21/10'); | |
208 | $x -= '0.1'; | |
209 | is($x, '2'); # not 21/10 | |
210 | ||
211 | $x = $mbr->new('2/3'); | |
212 | $y = $mbr->new('3/2'); | |
213 | is($x > $y, ''); | |
214 | is($x < $y, 1); | |
215 | is($x == $y, ''); | |
216 | ||
217 | $x = $mbr->new('-2/3'); | |
218 | $y = $mbr->new('3/2'); | |
219 | is($x > $y, ''); | |
220 | is($x < $y, '1'); | |
221 | is($x == $y, ''); | |
222 | ||
223 | $x = $mbr->new('-2/3'); | |
224 | $y = $mbr->new('-2/3'); | |
225 | is($x > $y, ''); | |
226 | is($x < $y, ''); | |
227 | is($x == $y, '1'); | |
228 | ||
229 | $x = $mbr->new('-2/3'); | |
230 | $y = $mbr->new('-1/3'); | |
231 | is($x > $y, ''); | |
232 | is($x < $y, '1'); | |
233 | is($x == $y, ''); | |
234 | ||
235 | $x = $mbr->new('-124'); | |
236 | $y = $mbr->new('-122'); | |
237 | is($x->bacmp($y), 1); | |
238 | ||
239 | $x = $mbr->new('-124'); | |
240 | $y = $mbr->new('-122'); | |
241 | is($x->bcmp($y), -1); | |
242 | ||
243 | $x = $mbr->new('3/7'); | |
244 | $y = $mbr->new('5/7'); | |
245 | is($x+$y, '8/7'); | |
246 | ||
247 | $x = $mbr->new('3/7'); | |
248 | $y = $mbr->new('5/7'); | |
249 | is($x*$y, '15/49'); | |
250 | ||
251 | $x = $mbr->new('3/5'); | |
252 | $y = $mbr->new('5/7'); | |
253 | is($x*$y, '3/7'); | |
254 | ||
255 | $x = $mbr->new('3/5'); | |
256 | $y = $mbr->new('5/7'); | |
257 | is($x/$y, '21/25'); | |
258 | ||
259 | $x = $mbr->new('7/4'); | |
260 | $y = $mbr->new('1'); | |
261 | is($x % $y, '3/4'); | |
262 | ||
263 | $x = $mbr->new('7/4'); | |
264 | $y = $mbr->new('5/13'); | |
265 | is($x % $y, '11/52'); | |
266 | ||
267 | $x = $mbr->new('7/4'); | |
268 | $y = $mbr->new('5/9'); | |
269 | is($x % $y, '1/12'); | |
270 | ||
271 | $x = $mbr->new('-144/9')->bsqrt(); | |
272 | is($x, 'NaN'); | |
273 | ||
274 | $x = $mbr->new('144/9')->bsqrt(); | |
275 | is($x, '4'); | |
276 | ||
277 | $x = $mbr->new('3/4')->bsqrt(); | |
278 | is($x, | |
6320cdc0 SH |
279 | '4330127018922193233818615853764680917357/' . |
280 | '5000000000000000000000000000000000000000'); | |
184f15d5 | 281 | |
a4e2b1c6 JH |
282 | ############################################################################## |
283 | # bpow | |
284 | ||
11c955be SH |
285 | $x = $mbr->new('2/1'); |
286 | $z = $x->bpow('3/1'); | |
287 | is($x, '8'); | |
7d341013 | 288 | |
11c955be SH |
289 | $x = $mbr->new('1/2'); |
290 | $z = $x->bpow('3/1'); | |
291 | is($x, '1/8'); | |
292 | ||
293 | $x = $mbr->new('1/3'); | |
294 | $z = $x->bpow('4/1'); | |
295 | is($x, '1/81'); | |
296 | ||
297 | $x = $mbr->new('2/3'); | |
298 | $z = $x->bpow('4/1'); | |
299 | is($x, '16/81'); | |
300 | ||
301 | $x = $mbr->new('2/3'); | |
302 | $z = $x->bpow('5/3'); | |
303 | is($x, '31797617848703662994667839220546583581/62500000000000000000000000000000000000'); | |
a4e2b1c6 JH |
304 | |
305 | ############################################################################## | |
306 | # bfac | |
307 | ||
11c955be SH |
308 | $x = $mbr->new('1'); |
309 | $x->bfac(); | |
310 | is($x, '1'); | |
311 | ||
312 | for (my $i = 0; $i < 8; $i++) { | |
313 | $x = $mbr->new("$i/1")->bfac(); | |
314 | is($x, $mbi->new($i)->bfac()); | |
315 | } | |
a4e2b1c6 JH |
316 | |
317 | # test for $self->bnan() vs. $x->bnan(); | |
11c955be SH |
318 | $x = $mbr->new('-1'); |
319 | $x->bfac(); | |
320 | is($x, 'NaN'); | |
a4e2b1c6 JH |
321 | |
322 | ############################################################################## | |
323 | # binc/bdec | |
324 | ||
11c955be SH |
325 | $x = $mbr->new('3/2'); |
326 | is($x->binc(), '5/2'); | |
327 | $x = $mbr->new('15/6'); | |
328 | is($x->bdec(), '3/2'); | |
a4e2b1c6 JH |
329 | |
330 | ############################################################################## | |
331 | # bfloor/bceil | |
332 | ||
11c955be SH |
333 | $x = $mbr->new('-7/7'); |
334 | is($x->$n(), '-1'); | |
335 | is($x->$d(), '1'); | |
336 | $x = $mbr->new('-7/7')->bfloor(); | |
337 | is($x->$n(), '-1'); | |
338 | is($x->$d(), '1'); | |
a4e2b1c6 JH |
339 | |
340 | ############################################################################## | |
7d341013 T |
341 | # bsstr |
342 | ||
11c955be SH |
343 | $x = $mbr->new('7/5')->bsstr(); |
344 | is($x, '7/5'); | |
345 | $x = $mbr->new('-7/5')->bsstr(); | |
346 | is($x, '-7/5'); | |
7d341013 T |
347 | |
348 | ############################################################################## | |
349 | # numify() | |
350 | ||
351 | my @array = qw/1 2 3 4 5 6 7 8 9/; | |
11c955be SH |
352 | $x = $mbr->new('8/8'); |
353 | is($array[$x], 2); | |
354 | ||
355 | $x = $mbr->new('16/8'); | |
356 | is($array[$x], 3); | |
357 | ||
358 | $x = $mbr->new('17/8'); | |
359 | is($array[$x], 3); | |
7d341013 | 360 | |
11c955be SH |
361 | $x = $mbr->new('33/8'); |
362 | is($array[$x], 5); | |
7d341013 | 363 | |
11c955be SH |
364 | $x = $mbr->new('-33/8'); |
365 | is($array[$x], 6); | |
366 | ||
367 | $x = $mbr->new('-8/1'); | |
368 | is($array[$x], 2); # -8 => 2 | |
369 | ||
370 | $x = $mbr->new('33/8'); | |
371 | is($x->numify() * 1000, 4125); | |
372 | ||
373 | $x = $mbr->new('-33/8'); | |
374 | is($x->numify() * 1000, -4125); | |
375 | ||
376 | $x = $mbr->new('inf'); | |
377 | is($x->numify(), 'inf'); | |
378 | ||
379 | $x = $mbr->new('-inf'); | |
380 | is($x->numify(), '-inf'); | |
381 | ||
382 | $x = $mbr->new('NaN'); | |
383 | is($x->numify(), 'NaN'); | |
384 | ||
385 | $x = $mbr->new('4/3'); | |
386 | is($x->numify(), 4/3); | |
93c87d9d | 387 | |
7d341013 | 388 | ############################################################################## |
b8884ce4 | 389 | # as_hex(), as_bin(), as_oct() |
12fc2493 | 390 | |
11c955be SH |
391 | $x = $mbr->new('8/8'); |
392 | is($x->as_hex(), '0x1'); | |
393 | is($x->as_bin(), '0b1'); | |
394 | is($x->as_oct(), '01'); | |
395 | ||
396 | $x = $mbr->new('80/8'); | |
397 | is($x->as_hex(), '0xa'); | |
398 | is($x->as_bin(), '0b1010'); | |
399 | is($x->as_oct(), '012'); | |
12fc2493 AMS |
400 | |
401 | ############################################################################## | |
402 | # broot(), blog(), bmodpow() and bmodinv() | |
7afd7a91 | 403 | |
11c955be SH |
404 | $x = $mbr->new(2) ** 32; |
405 | $y = $mbr->new(4); | |
406 | $z = $mbr->new(3); | |
7afd7a91 | 407 | |
11c955be SH |
408 | is($x->copy()->broot($y), 2 ** 8); |
409 | is(ref($x->copy()->broot($y)), $mbr); | |
7afd7a91 | 410 | |
11c955be SH |
411 | is($x->copy()->bmodpow($y, $z), 1); |
412 | is(ref($x->copy()->bmodpow($y, $z)), $mbr); | |
7afd7a91 | 413 | |
11c955be SH |
414 | $x = $mbr->new(8); |
415 | $y = $mbr->new(5033); | |
416 | $z = $mbr->new(4404); | |
7afd7a91 | 417 | |
11c955be SH |
418 | is($x->copy()->bmodinv($y), $z); |
419 | is(ref($x->copy()->bmodinv($y)), $mbr); | |
7afd7a91 | 420 | |
9b924220 | 421 | # square root with exact result |
11c955be SH |
422 | $x = $mbr->new('1.44'); |
423 | is($x->copy()->broot(2), '6/5'); | |
424 | is(ref($x->copy()->broot(2)), $mbr); | |
9b924220 RGS |
425 | |
426 | # log with exact result | |
11c955be SH |
427 | $x = $mbr->new('256.1'); |
428 | is($x->copy()->blog(2), '8000563442710106079310294693803606983661/1000000000000000000000000000000000000000'); | |
429 | is(ref($x->copy()->blog(2)), $mbr); | |
9b924220 | 430 | |
11c955be SH |
431 | $x = $mbr->new(144); |
432 | is($x->copy()->broot('2'), 12, 'v/144 = 12'); | |
12fc2493 | 433 | |
11c955be SH |
434 | $x = $mbr->new(12*12*12); |
435 | is($x->copy()->broot('3'), 12, '(12*12*12) ** 1/3 = 12'); | |
9b924220 | 436 | |
7afd7a91 | 437 | ############################################################################## |
b8884ce4 T |
438 | # from_hex(), from_bin(), from_oct() |
439 | ||
440 | $x = Math::BigRat->from_hex('0x100'); | |
11c955be SH |
441 | is($x, '256', 'from_hex'); |
442 | ||
443 | $x = $mbr->from_hex('0x100'); | |
444 | is($x, '256', 'from_hex'); | |
b8884ce4 T |
445 | |
446 | $x = Math::BigRat->from_bin('0b100'); | |
11c955be SH |
447 | is($x, '4', 'from_bin'); |
448 | ||
449 | $x = $mbr->from_bin('0b100'); | |
450 | is($x, '4', 'from_bin'); | |
b8884ce4 T |
451 | |
452 | $x = Math::BigRat->from_oct('0100'); | |
11c955be SH |
453 | is($x, '64', 'from_oct'); |
454 | ||
455 | $x = $mbr->from_oct('0100'); | |
456 | is($x, '64', 'from_oct'); | |
b8884ce4 T |
457 | |
458 | ############################################################################## | |
4de3d162 T |
459 | # as_float() |
460 | ||
11c955be SH |
461 | $x = Math::BigRat->new('1/2'); |
462 | my $f = $x->as_float(); | |
4de3d162 | 463 | |
11c955be SH |
464 | is($x, '1/2', '$x unmodified'); |
465 | is($f, '0.5', 'as_float(0.5)'); | |
4de3d162 | 466 | |
11c955be SH |
467 | $x = Math::BigRat->new('2/3'); |
468 | $f = $x->as_float(5); | |
4de3d162 | 469 | |
11c955be SH |
470 | is($x, '2/3', '$x unmodified'); |
471 | is($f, '0.66667', 'as_float(2/3, 5)'); | |
4de3d162 T |
472 | |
473 | ############################################################################## | |
d93f0209 FC |
474 | # int() |
475 | ||
11c955be SH |
476 | $x = Math::BigRat->new('5/2'); |
477 | is(int($x), '2', '5/2 converted to integer'); | |
478 | ||
479 | $x = Math::BigRat->new('-1/2'); | |
480 | is(int($x), '0', '-1/2 converted to integer'); | |
d93f0209 FC |
481 | |
482 | ############################################################################## | |
184f15d5 JH |
483 | # done |
484 | ||
485 | 1; |