This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Math::BigRat from version 0.260802 to 0.260804
[perl5.git] / cpan / Math-BigRat / t / bigrat.t
CommitLineData
6320cdc0 1#!perl
184f15d5
JH
2
3use strict;
11c955be
SH
4use warnings;
5
d93f0209 6use Test::More tests => 202;
184f15d5 7
12fc2493 8# basic testing of Math::BigRat
184f15d5
JH
9
10use Math::BigRat;
6de7f0cc
JH
11use Math::BigInt;
12use Math::BigFloat;
13
14# shortcuts
11c955be 15my $mbr = 'Math::BigRat';
6de7f0cc
JH
16my $mbi = 'Math::BigInt';
17my $mbf = 'Math::BigFloat';
184f15d5 18
11c955be 19my ($x, $y, $z);
184f15d5 20
11c955be
SH
21$x = Math::BigRat->new(1234);
22is($x, 1234);
23isa_ok($x, 'Math::BigRat');
24is($x->isa('Math::BigFloat'), 0);
25is($x->isa('Math::BigInt'), 0);
8f675a64
JH
26
27##############################################################################
6de7f0cc 28# new and bnorm()
8f675a64 29
6320cdc0 30foreach 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
108my $n = 'numerator';
109my $d = 'denominator';
110
6320cdc0 111$x = $mbr->new('-0');
11c955be 112is($x, '0');
6320cdc0 113is($x->$n(), '0');
11c955be
SH
114is($x->$d(), '1');
115
6320cdc0 116$x = $mbr->new('NaN');
11c955be
SH
117is($x, 'NaN'); is($x->$n(), 'NaN');
118is($x->$d(), 'NaN');
119
6320cdc0 120$x = $mbr->new('-NaN');
11c955be
SH
121is($x, 'NaN'); is($x->$n(), 'NaN');
122is($x->$d(), 'NaN');
123
6320cdc0 124$x = $mbr->new('-1r4');
11c955be
SH
125is($x, 'NaN'); is($x->$n(), 'NaN');
126is($x->$d(), 'NaN');
184f15d5 127
6320cdc0 128$x = $mbr->new('+inf');
11c955be
SH
129is($x, 'inf'); is($x->$n(), 'inf');
130is($x->$d(), '1');
184f15d5 131
6320cdc0 132$x = $mbr->new('-inf');
11c955be
SH
133is($x, '-inf');
134is($x->$n(), '-inf');
135is($x->$d(), '1');
136
6320cdc0 137$x = $mbr->new('123a4');
11c955be
SH
138is($x, 'NaN');
139is($x->$n(), 'NaN');
140is($x->$d(), 'NaN');
141
142# wrong inputs
6320cdc0 143$x = $mbr->new('1e2e2');
11c955be
SH
144is($x, 'NaN');
145is($x->$n(), 'NaN');
146is($x->$d(), 'NaN');
147
6320cdc0 148$x = $mbr->new('1+2+2');
11c955be
SH
149is($x, 'NaN');
150is($x->$n(), 'NaN');
151is($x->$d(), 'NaN');
152
153# failed due to BigFloat bug
6320cdc0 154$x = $mbr->new('1.2.2');
11c955be
SH
155is($x, 'NaN');
156is($x->$n(), 'NaN');
157is($x->$d(), 'NaN');
158
159is($mbr->new('123a4'), 'NaN');
160is($mbr->new('123e4'), '1230000');
161is($mbr->new('-NaN'), 'NaN');
162is($mbr->new('NaN'), 'NaN');
163is($mbr->new('+inf'), 'inf');
164is($mbr->new('-inf'), '-inf');
184f15d5 165
11c955be
SH
166##############################################################################
167# two Bigints
184f15d5 168
11c955be
SH
169is($mbr->new($mbi->new(3), $mbi->new(7))->badd(1), '10/7');
170is($mbr->new($mbi->new(-13), $mbi->new(7)), '-13/7');
171is($mbr->new($mbi->new(13), $mbi->new(-7)), '-13/7');
172is($mbr->new($mbi->new(-13), $mbi->new(-7)), '13/7');
184f15d5 173
11c955be
SH
174##############################################################################
175# mixed arguments
8f675a64 176
11c955be
SH
177is($mbr->new('3/7')->badd(1), '10/7');
178is($mbr->new('3/10')->badd(1.1), '7/5');
179is($mbr->new('3/7')->badd($mbi->new(1)), '10/7');
180is($mbr->new('3/10')->badd($mbf->new('1.1')), '7/5');
8f675a64 181
11c955be
SH
182is($mbr->new('3/7')->bsub(1), '-4/7');
183is($mbr->new('3/10')->bsub(1.1), '-4/5');
184is($mbr->new('3/7')->bsub($mbi->new(1)), '-4/7');
185is($mbr->new('3/10')->bsub($mbf->new('1.1')), '-4/5');
8f675a64 186
11c955be
SH
187is($mbr->new('3/7')->bmul(1), '3/7');
188is($mbr->new('3/10')->bmul(1.1), '33/100');
189is($mbr->new('3/7')->bmul($mbi->new(1)), '3/7');
190is($mbr->new('3/10')->bmul($mbf->new('1.1')), '33/100');
8f675a64 191
11c955be
SH
192is($mbr->new('3/7')->bdiv(1), '3/7');
193is($mbr->new('3/10')->bdiv(1.1), '3/11');
194is($mbr->new('3/7')->bdiv($mbi->new(1)), '3/7');
195is($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
201is($x + $y, '7/12');
202is($x * $y, '1/12');
203is($x / $y, '3/4');
93c87d9d 204
11c955be
SH
205$x = $mbr->new('7/5');
206$x *= '3/2';
207is($x, '21/10');
208$x -= '0.1';
209is($x, '2'); # not 21/10
210
211$x = $mbr->new('2/3');
212$y = $mbr->new('3/2');
213is($x > $y, '');
214is($x < $y, 1);
215is($x == $y, '');
216
217$x = $mbr->new('-2/3');
218$y = $mbr->new('3/2');
219is($x > $y, '');
220is($x < $y, '1');
221is($x == $y, '');
222
223$x = $mbr->new('-2/3');
224$y = $mbr->new('-2/3');
225is($x > $y, '');
226is($x < $y, '');
227is($x == $y, '1');
228
229$x = $mbr->new('-2/3');
230$y = $mbr->new('-1/3');
231is($x > $y, '');
232is($x < $y, '1');
233is($x == $y, '');
234
235$x = $mbr->new('-124');
236$y = $mbr->new('-122');
237is($x->bacmp($y), 1);
238
239$x = $mbr->new('-124');
240$y = $mbr->new('-122');
241is($x->bcmp($y), -1);
242
243$x = $mbr->new('3/7');
244$y = $mbr->new('5/7');
245is($x+$y, '8/7');
246
247$x = $mbr->new('3/7');
248$y = $mbr->new('5/7');
249is($x*$y, '15/49');
250
251$x = $mbr->new('3/5');
252$y = $mbr->new('5/7');
253is($x*$y, '3/7');
254
255$x = $mbr->new('3/5');
256$y = $mbr->new('5/7');
257is($x/$y, '21/25');
258
259$x = $mbr->new('7/4');
260$y = $mbr->new('1');
261is($x % $y, '3/4');
262
263$x = $mbr->new('7/4');
264$y = $mbr->new('5/13');
265is($x % $y, '11/52');
266
267$x = $mbr->new('7/4');
268$y = $mbr->new('5/9');
269is($x % $y, '1/12');
270
271$x = $mbr->new('-144/9')->bsqrt();
272is($x, 'NaN');
273
274$x = $mbr->new('144/9')->bsqrt();
275is($x, '4');
276
277$x = $mbr->new('3/4')->bsqrt();
278is($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');
287is($x, '8');
7d341013 288
11c955be
SH
289$x = $mbr->new('1/2');
290$z = $x->bpow('3/1');
291is($x, '1/8');
292
293$x = $mbr->new('1/3');
294$z = $x->bpow('4/1');
295is($x, '1/81');
296
297$x = $mbr->new('2/3');
298$z = $x->bpow('4/1');
299is($x, '16/81');
300
301$x = $mbr->new('2/3');
302$z = $x->bpow('5/3');
303is($x, '31797617848703662994667839220546583581/62500000000000000000000000000000000000');
a4e2b1c6
JH
304
305##############################################################################
306# bfac
307
11c955be
SH
308$x = $mbr->new('1');
309$x->bfac();
310is($x, '1');
311
312for (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();
320is($x, 'NaN');
a4e2b1c6
JH
321
322##############################################################################
323# binc/bdec
324
11c955be
SH
325$x = $mbr->new('3/2');
326is($x->binc(), '5/2');
327$x = $mbr->new('15/6');
328is($x->bdec(), '3/2');
a4e2b1c6
JH
329
330##############################################################################
331# bfloor/bceil
332
11c955be
SH
333$x = $mbr->new('-7/7');
334is($x->$n(), '-1');
335is($x->$d(), '1');
336$x = $mbr->new('-7/7')->bfloor();
337is($x->$n(), '-1');
338is($x->$d(), '1');
a4e2b1c6
JH
339
340##############################################################################
7d341013
T
341# bsstr
342
11c955be
SH
343$x = $mbr->new('7/5')->bsstr();
344is($x, '7/5');
345$x = $mbr->new('-7/5')->bsstr();
346is($x, '-7/5');
7d341013
T
347
348##############################################################################
349# numify()
350
351my @array = qw/1 2 3 4 5 6 7 8 9/;
11c955be
SH
352$x = $mbr->new('8/8');
353is($array[$x], 2);
354
355$x = $mbr->new('16/8');
356is($array[$x], 3);
357
358$x = $mbr->new('17/8');
359is($array[$x], 3);
7d341013 360
11c955be
SH
361$x = $mbr->new('33/8');
362is($array[$x], 5);
7d341013 363
11c955be
SH
364$x = $mbr->new('-33/8');
365is($array[$x], 6);
366
367$x = $mbr->new('-8/1');
368is($array[$x], 2); # -8 => 2
369
370$x = $mbr->new('33/8');
371is($x->numify() * 1000, 4125);
372
373$x = $mbr->new('-33/8');
374is($x->numify() * 1000, -4125);
375
376$x = $mbr->new('inf');
377is($x->numify(), 'inf');
378
379$x = $mbr->new('-inf');
380is($x->numify(), '-inf');
381
382$x = $mbr->new('NaN');
383is($x->numify(), 'NaN');
384
385$x = $mbr->new('4/3');
386is($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');
392is($x->as_hex(), '0x1');
393is($x->as_bin(), '0b1');
394is($x->as_oct(), '01');
395
396$x = $mbr->new('80/8');
397is($x->as_hex(), '0xa');
398is($x->as_bin(), '0b1010');
399is($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
408is($x->copy()->broot($y), 2 ** 8);
409is(ref($x->copy()->broot($y)), $mbr);
7afd7a91 410
11c955be
SH
411is($x->copy()->bmodpow($y, $z), 1);
412is(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
418is($x->copy()->bmodinv($y), $z);
419is(ref($x->copy()->bmodinv($y)), $mbr);
7afd7a91 420
9b924220 421# square root with exact result
11c955be
SH
422$x = $mbr->new('1.44');
423is($x->copy()->broot(2), '6/5');
424is(ref($x->copy()->broot(2)), $mbr);
9b924220
RGS
425
426# log with exact result
11c955be
SH
427$x = $mbr->new('256.1');
428is($x->copy()->blog(2), '8000563442710106079310294693803606983661/1000000000000000000000000000000000000000');
429is(ref($x->copy()->blog(2)), $mbr);
9b924220 430
11c955be
SH
431$x = $mbr->new(144);
432is($x->copy()->broot('2'), 12, 'v/144 = 12');
12fc2493 433
11c955be
SH
434$x = $mbr->new(12*12*12);
435is($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
441is($x, '256', 'from_hex');
442
443$x = $mbr->from_hex('0x100');
444is($x, '256', 'from_hex');
b8884ce4
T
445
446$x = Math::BigRat->from_bin('0b100');
11c955be
SH
447is($x, '4', 'from_bin');
448
449$x = $mbr->from_bin('0b100');
450is($x, '4', 'from_bin');
b8884ce4
T
451
452$x = Math::BigRat->from_oct('0100');
11c955be
SH
453is($x, '64', 'from_oct');
454
455$x = $mbr->from_oct('0100');
456is($x, '64', 'from_oct');
b8884ce4
T
457
458##############################################################################
4de3d162
T
459# as_float()
460
11c955be
SH
461$x = Math::BigRat->new('1/2');
462my $f = $x->as_float();
4de3d162 463
11c955be
SH
464is($x, '1/2', '$x unmodified');
465is($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
470is($x, '2/3', '$x unmodified');
471is($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');
477is(int($x), '2', '5/2 converted to integer');
478
479$x = Math::BigRat->new('-1/2');
480is(int($x), '0', '-1/2 converted to integer');
d93f0209
FC
481
482##############################################################################
184f15d5
JH
483# done
484
4851;