This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade Math::BigRat from version 0.25 to 0.26
[perl5.git] / cpan / Math-BigRat / t / bigrat.t
CommitLineData
184f15d5
JH
1#!/usr/bin/perl -w
2
3use strict;
c32198f6 4use Test::More tests => 200;
184f15d5 5
12fc2493 6# basic testing of Math::BigRat
184f15d5
JH
7
8use Math::BigRat;
6de7f0cc
JH
9use Math::BigInt;
10use Math::BigFloat;
11
12# shortcuts
13my $cr = 'Math::BigRat';
14my $mbi = 'Math::BigInt';
15my $mbf = 'Math::BigFloat';
184f15d5
JH
16
17my ($x,$y,$z);
18
c32198f6
FR
19$x = Math::BigRat->new(1234); is ($x,1234);
20isa_ok ($x, 'Math::BigRat');
21is ($x->isa('Math::BigFloat'), 0);
22is ($x->isa('Math::BigInt'), 0);
8f675a64
JH
23
24##############################################################################
6de7f0cc 25# new and bnorm()
8f675a64 26
6de7f0cc
JH
27foreach my $func (qw/new bnorm/)
28 {
c32198f6
FR
29 $x = $cr->$func(1234); is ($x,1234);
30
31 $x = $cr->$func('1234/1'); is ($x,1234);
32 $x = $cr->$func('1234/2'); is ($x,617);
33
34 $x = $cr->$func('100/1.0'); is ($x,100);
35 $x = $cr->$func('10.0/1.0'); is ($x,10);
36 $x = $cr->$func('0.1/10'); is ($x,'1/100');
37 $x = $cr->$func('0.1/0.1'); is ($x,'1');
38 $x = $cr->$func('1e2/10'); is ($x,10);
39 $x = $cr->$func('5/1e2'); is ($x,'1/20');
40 $x = $cr->$func('1e2/1e1'); is ($x,10);
41 $x = $cr->$func('1 / 3'); is ($x,'1/3');
42 $x = $cr->$func('-1 / 3'); is ($x,'-1/3');
43 $x = $cr->$func('NaN'); is ($x,'NaN');
44 $x = $cr->$func('inf'); is ($x,'inf');
45 $x = $cr->$func('-inf'); is ($x,'-inf');
46 $x = $cr->$func('1/'); is ($x,'NaN');
47
48 $x = $cr->$func("0x7e"); is ($x,126);
c6c613ed 49
6de7f0cc 50 # input ala '1+1/3' isn't parsed ok yet
c32198f6 51 $x = $cr->$func('1+1/3'); is ($x,'NaN');
990fb837 52
c32198f6
FR
53 $x = $cr->$func('1/1.2'); is ($x,'5/6');
54 $x = $cr->$func('1.3/1.2'); is ($x,'13/12');
55 $x = $cr->$func('1.2/1'); is ($x,'6/5');
6de7f0cc
JH
56
57 ############################################################################
58 # other classes as input
59
c32198f6
FR
60 $x = $cr->$func($mbi->new(1231)); is ($x,'1231');
61 $x = $cr->$func($mbf->new(1232)); is ($x,'1232');
62 $x = $cr->$func($mbf->new(1232.3)); is ($x,'12323/10');
6de7f0cc 63 }
9b924220 64
12fc2493
AMS
65my $n = 'numerator';
66my $d = 'denominator';
a4e2b1c6 67
c32198f6
FR
68$x = $cr->new('-0'); is ($x,'0'); is ($x->$n(), '0'); is ($x->$d(),'1');
69$x = $cr->new('NaN'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
70$x = $cr->new('-NaN'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
71$x = $cr->new('-1r4'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
12fc2493 72
c32198f6
FR
73$x = $cr->new('+inf'); is ($x,'inf'); is ($x->$n(), 'inf'); is ($x->$d(),'1');
74$x = $cr->new('-inf'); is ($x,'-inf'); is ($x->$n(), '-inf'); is ($x->$d(),'1');
75$x = $cr->new('123a4'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
a4e2b1c6
JH
76
77# wrong inputs
c32198f6
FR
78$x = $cr->new('1e2e2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
79$x = $cr->new('1+2+2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
12fc2493 80# failed due to BigFloat bug
c32198f6 81$x = $cr->new('1.2.2'); is ($x,'NaN'); is ($x->$n(), 'NaN'); is ($x->$d(),'NaN');
a4e2b1c6 82
c32198f6
FR
83is ($cr->new('123a4'),'NaN');
84is ($cr->new('123e4'),'1230000');
85is ($cr->new('-NaN'),'NaN');
86is ($cr->new('NaN'),'NaN');
87is ($cr->new('+inf'),'inf');
88is ($cr->new('-inf'),'-inf');
8f675a64
JH
89
90##############################################################################
b68b7ab1
T
91# two Bigints
92
c32198f6
FR
93is ($cr->new($mbi->new(3),$mbi->new(7))->badd(1),'10/7');
94is ($cr->new($mbi->new(-13),$mbi->new(7)),'-13/7');
95is ($cr->new($mbi->new(13),$mbi->new(-7)),'-13/7');
96is ($cr->new($mbi->new(-13),$mbi->new(-7)),'13/7');
b68b7ab1
T
97
98##############################################################################
8f675a64
JH
99# mixed arguments
100
c32198f6
FR
101is ($cr->new('3/7')->badd(1),'10/7');
102is ($cr->new('3/10')->badd(1.1),'7/5');
103is ($cr->new('3/7')->badd($mbi->new(1)),'10/7');
104is ($cr->new('3/10')->badd($mbf->new('1.1')),'7/5');
8f675a64 105
c32198f6
FR
106is ($cr->new('3/7')->bsub(1),'-4/7');
107is ($cr->new('3/10')->bsub(1.1),'-4/5');
108is ($cr->new('3/7')->bsub($mbi->new(1)),'-4/7');
109is ($cr->new('3/10')->bsub($mbf->new('1.1')),'-4/5');
8f675a64 110
c32198f6
FR
111is ($cr->new('3/7')->bmul(1),'3/7');
112is ($cr->new('3/10')->bmul(1.1),'33/100');
113is ($cr->new('3/7')->bmul($mbi->new(1)),'3/7');
114is ($cr->new('3/10')->bmul($mbf->new('1.1')),'33/100');
8f675a64 115
c32198f6
FR
116is ($cr->new('3/7')->bdiv(1),'3/7');
117is ($cr->new('3/10')->bdiv(1.1),'3/11');
118is ($cr->new('3/7')->bdiv($mbi->new(1)),'3/7');
119is ($cr->new('3/10')->bdiv($mbf->new('1.1')),'3/11');
8f675a64
JH
120
121##############################################################################
a4e2b1c6 122$x = $cr->new('1/4'); $y = $cr->new('1/3');
9b924220 123
c32198f6
FR
124is ($x + $y, '7/12');
125is ($x * $y, '1/12');
126is ($x / $y, '3/4');
184f15d5 127
a4e2b1c6 128$x = $cr->new('7/5'); $x *= '3/2';
c32198f6 129is ($x,'21/10');
184f15d5 130$x -= '0.1';
c32198f6 131is ($x,'2'); # not 21/10
184f15d5 132
a4e2b1c6 133$x = $cr->new('2/3'); $y = $cr->new('3/2');
c32198f6
FR
134is ($x > $y,'');
135is ($x < $y,1);
136is ($x == $y,'');
184f15d5 137
a4e2b1c6 138$x = $cr->new('-2/3'); $y = $cr->new('3/2');
c32198f6
FR
139is ($x > $y,'');
140is ($x < $y,'1');
141is ($x == $y,'');
184f15d5 142
a4e2b1c6 143$x = $cr->new('-2/3'); $y = $cr->new('-2/3');
c32198f6
FR
144is ($x > $y,'');
145is ($x < $y,'');
146is ($x == $y,'1');
184f15d5 147
a4e2b1c6 148$x = $cr->new('-2/3'); $y = $cr->new('-1/3');
c32198f6
FR
149is ($x > $y,'');
150is ($x < $y,'1');
151is ($x == $y,'');
184f15d5 152
a4e2b1c6 153$x = $cr->new('-124'); $y = $cr->new('-122');
c32198f6 154is ($x->bacmp($y),1);
184f15d5 155
a4e2b1c6 156$x = $cr->new('-124'); $y = $cr->new('-122');
c32198f6 157is ($x->bcmp($y),-1);
184f15d5 158
a4e2b1c6 159$x = $cr->new('3/7'); $y = $cr->new('5/7');
c32198f6 160is ($x+$y,'8/7');
8f675a64 161
a4e2b1c6 162$x = $cr->new('3/7'); $y = $cr->new('5/7');
c32198f6 163is ($x*$y,'15/49');
8f675a64 164
a4e2b1c6 165$x = $cr->new('3/5'); $y = $cr->new('5/7');
c32198f6 166is ($x*$y,'3/7');
8f675a64 167
a4e2b1c6 168$x = $cr->new('3/5'); $y = $cr->new('5/7');
c32198f6 169is ($x/$y,'21/25');
8f675a64 170
93c87d9d 171$x = $cr->new('7/4'); $y = $cr->new('1');
c32198f6 172is ($x % $y,'3/4');
93c87d9d
T
173
174$x = $cr->new('7/4'); $y = $cr->new('5/13');
c32198f6 175is ($x % $y,'11/52');
93c87d9d
T
176
177$x = $cr->new('7/4'); $y = $cr->new('5/9');
c32198f6 178is ($x % $y,'1/12');
93c87d9d 179
c32198f6
FR
180$x = $cr->new('-144/9')->bsqrt(); is ($x,'NaN');
181$x = $cr->new('144/9')->bsqrt(); is ($x,'4');
182$x = $cr->new('3/4')->bsqrt(); is ($x,
93c87d9d
T
183 '1732050807568877293527446341505872366943/'
184 .'2000000000000000000000000000000000000000');
184f15d5 185
a4e2b1c6
JH
186##############################################################################
187# bpow
188
c32198f6
FR
189$x = $cr->new('2/1'); $z = $x->bpow('3/1'); is ($x,'8');
190$x = $cr->new('1/2'); $z = $x->bpow('3/1'); is ($x,'1/8');
191$x = $cr->new('1/3'); $z = $x->bpow('4/1'); is ($x,'1/81');
192$x = $cr->new('2/3'); $z = $x->bpow('4/1'); is ($x,'16/81');
7d341013 193
4de3d162 194$x = $cr->new('2/3'); $z = $x->bpow('5/3');
c32198f6 195is ($x, '31797617848703662994667839220546583581/62500000000000000000000000000000000000');
a4e2b1c6
JH
196
197##############################################################################
198# bfac
199
c32198f6 200$x = $cr->new('1'); $x->bfac(); is ($x,'1');
a4e2b1c6
JH
201for (my $i = 0; $i < 8; $i++)
202 {
c32198f6 203 $x = $cr->new("$i/1")->bfac(); is ($x,$mbi->new($i)->bfac());
a4e2b1c6
JH
204 }
205
206# test for $self->bnan() vs. $x->bnan();
c32198f6 207$x = $cr->new('-1'); $x->bfac(); is ($x,'NaN');
a4e2b1c6
JH
208
209##############################################################################
210# binc/bdec
211
c32198f6
FR
212$x = $cr->new('3/2'); is ($x->binc(),'5/2');
213$x = $cr->new('15/6'); is ($x->bdec(),'3/2');
a4e2b1c6
JH
214
215##############################################################################
216# bfloor/bceil
217
c32198f6
FR
218$x = $cr->new('-7/7'); is ($x->$n(), '-1'); is ($x->$d(), '1');
219$x = $cr->new('-7/7')->bfloor(); is ($x->$n(), '-1'); is ($x->$d(), '1');
a4e2b1c6
JH
220
221##############################################################################
7d341013
T
222# bsstr
223
c32198f6
FR
224$x = $cr->new('7/5')->bsstr(); is ($x,'7/5');
225$x = $cr->new('-7/5')->bsstr(); is ($x,'-7/5');
7d341013
T
226
227##############################################################################
228# numify()
229
230my @array = qw/1 2 3 4 5 6 7 8 9/;
c32198f6
FR
231$x = $cr->new('8/8'); is ($array[$x],2);
232$x = $cr->new('16/8'); is ($array[$x],3);
233$x = $cr->new('17/8'); is ($array[$x],3);
234$x = $cr->new('33/8'); is ($array[$x],5);
235$x = $cr->new('-33/8'); is ($array[$x],6);
236$x = $cr->new('-8/1'); is ($array[$x],2); # -8 => 2
7d341013 237
c32198f6
FR
238$x = $cr->new('33/8'); is ($x->numify() * 1000, 4125);
239$x = $cr->new('-33/8'); is ($x->numify() * 1000, -4125);
240$x = $cr->new('inf'); is ($x->numify(), 'inf');
241$x = $cr->new('-inf'); is ($x->numify(), '-inf');
242$x = $cr->new('NaN'); is ($x->numify(), 'NaN');
7d341013 243
c32198f6 244$x = $cr->new('4/3'); is ($x->numify(), 4/3);
93c87d9d 245
7d341013 246##############################################################################
b8884ce4 247# as_hex(), as_bin(), as_oct()
12fc2493
AMS
248
249$x = $cr->new('8/8');
c32198f6 250is ($x->as_hex(), '0x1'); is ($x->as_bin(), '0b1'); is ($x->as_oct(), '01');
12fc2493 251$x = $cr->new('80/8');
c32198f6 252is ($x->as_hex(), '0xa'); is ($x->as_bin(), '0b1010'); is ($x->as_oct(), '012');
12fc2493
AMS
253
254##############################################################################
255# broot(), blog(), bmodpow() and bmodinv()
7afd7a91
T
256
257$x = $cr->new(2) ** 32;
258$y = $cr->new(4);
259$z = $cr->new(3);
260
c32198f6
FR
261is ($x->copy()->broot($y), 2 ** 8);
262is (ref($x->copy()->broot($y)), $cr);
7afd7a91 263
c32198f6
FR
264is ($x->copy()->bmodpow($y,$z), 1);
265is (ref($x->copy()->bmodpow($y,$z)), $cr);
7afd7a91
T
266
267$x = $cr->new(8);
268$y = $cr->new(5033);
269$z = $cr->new(4404);
270
c32198f6
FR
271is ($x->copy()->bmodinv($y), $z);
272is (ref($x->copy()->bmodinv($y)), $cr);
7afd7a91 273
9b924220
RGS
274# square root with exact result
275$x = $cr->new('1.44');
c32198f6
FR
276is ($x->copy()->broot(2), '6/5');
277is (ref($x->copy()->broot(2)), $cr);
9b924220
RGS
278
279# log with exact result
280$x = $cr->new('256.1');
c32198f6
FR
281is ($x->copy()->blog(2), '8000563442710106079310294693803606983661/1000000000000000000000000000000000000000');
282is (ref($x->copy()->blog(2)), $cr);
9b924220 283
12fc2493 284$x = $cr->new(144);
c32198f6 285is ($x->copy()->broot('2'), 12, 'v/144 = 12');
12fc2493
AMS
286
287$x = $cr->new(12*12*12);
c32198f6 288is ($x->copy()->broot('3'), 12, '(12*12*12) ** 1/3 = 12');
9b924220 289
7afd7a91 290##############################################################################
b8884ce4
T
291# from_hex(), from_bin(), from_oct()
292
293$x = Math::BigRat->from_hex('0x100');
c32198f6 294is ($x, '256', 'from_hex');
b8884ce4 295$x = $cr->from_hex('0x100');
c32198f6 296is ($x, '256', 'from_hex');
b8884ce4
T
297
298$x = Math::BigRat->from_bin('0b100');
c32198f6 299is ($x, '4', 'from_bin');
b8884ce4 300$x = $cr->from_bin('0b100');
c32198f6 301is ($x, '4', 'from_bin');
b8884ce4
T
302
303$x = Math::BigRat->from_oct('0100');
c32198f6 304is ($x, '64', 'from_oct');
b8884ce4 305$x = $cr->from_oct('0100');
c32198f6 306is ($x, '64', 'from_oct');
b8884ce4
T
307
308##############################################################################
4de3d162
T
309# as_float()
310
311$x = Math::BigRat->new('1/2'); my $f = $x->as_float();
312
c32198f6
FR
313is ($x, '1/2', '$x unmodified');
314is ($f, '0.5', 'as_float(0.5)');
4de3d162
T
315
316$x = Math::BigRat->new('2/3'); $f = $x->as_float(5);
317
c32198f6
FR
318is ($x, '2/3', '$x unmodified');
319is ($f, '0.66667', 'as_float(2/3,5)');
4de3d162
T
320
321##############################################################################
184f15d5
JH
322# done
323
3241;
325