Upgrade BigInt and BigRat
[p5sagit/p5-mst-13.2.git] / lib / Math / BigRat / t / bigrat.t
CommitLineData
184f15d5 1#!/usr/bin/perl -w
2
3use strict;
4use Test;
5
6BEGIN
7 {
8 $| = 1;
9 chdir 't' if -d 't';
10 unshift @INC, '../lib'; # for running manually
93c87d9d 11 plan tests => 164;
184f15d5 12 }
13
14# testing of Math::BigRat
15
16use Math::BigRat;
6de7f0cc 17use Math::BigInt;
18use Math::BigFloat;
19
20# shortcuts
21my $cr = 'Math::BigRat';
22my $mbi = 'Math::BigInt';
23my $mbf = 'Math::BigFloat';
184f15d5 24
25my ($x,$y,$z);
26
27$x = Math::BigRat->new(1234); ok ($x,1234);
8f675a64 28ok ($x->isa('Math::BigRat'));
29ok (!$x->isa('Math::BigFloat'));
30ok (!$x->isa('Math::BigInt'));
31
32##############################################################################
6de7f0cc 33# new and bnorm()
8f675a64 34
6de7f0cc 35foreach my $func (qw/new bnorm/)
36 {
37 $x = $cr->$func(1234); ok ($x,1234);
38
39 $x = $cr->$func('1234/1'); ok ($x,1234);
40 $x = $cr->$func('1234/2'); ok ($x,617);
41
42 $x = $cr->$func('100/1.0'); ok ($x,100);
43 $x = $cr->$func('10.0/1.0'); ok ($x,10);
44 $x = $cr->$func('0.1/10'); ok ($x,'1/100');
45 $x = $cr->$func('0.1/0.1'); ok ($x,'1');
46 $x = $cr->$func('1e2/10'); ok ($x,10);
990fb837 47 $x = $cr->$func('5/1e2'); ok ($x,'1/20');
6de7f0cc 48 $x = $cr->$func('1e2/1e1'); ok ($x,10);
a4e2b1c6 49 $x = $cr->$func('1 / 3'); ok ($x,'1/3');
6de7f0cc 50 $x = $cr->$func('-1 / 3'); ok ($x,'-1/3');
a4e2b1c6 51 $x = $cr->$func('NaN'); ok ($x,'NaN');
52 $x = $cr->$func('inf'); ok ($x,'inf');
53 $x = $cr->$func('-inf'); ok ($x,'-inf');
54 $x = $cr->$func('1/'); ok ($x,'NaN');
6de7f0cc 55
56 # input ala '1+1/3' isn't parsed ok yet
a4e2b1c6 57 $x = $cr->$func('1+1/3'); ok ($x,'NaN');
990fb837 58
59 $x = $cr->$func('1/1.2'); ok ($x,'5/6');
60 $x = $cr->$func('1.3/1.2'); ok ($x,'13/12');
61 $x = $cr->$func('1.2/1'); ok ($x,'6/5');
6de7f0cc 62
63 ############################################################################
64 # other classes as input
65
a4e2b1c6 66 $x = $cr->$func($mbi->new(1231)); ok ($x,'1231');
67 $x = $cr->$func($mbf->new(1232)); ok ($x,'1232');
6de7f0cc 68 $x = $cr->$func($mbf->new(1232.3)); ok ($x,'12323/10');
69 }
a4e2b1c6 70
71$x = $cr->new('-0'); ok ($x,'0'); ok ($x->{_n}, '0'); ok ($x->{_d},'1');
72$x = $cr->new('NaN'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
73$x = $cr->new('-NaN'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
74$x = $cr->new('-1r4'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
75
76$x = $cr->new('+inf'); ok ($x,'inf'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
77$x = $cr->new('-inf'); ok ($x,'-inf'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
78$x = $cr->new('123a4'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
79
80# wrong inputs
81$x = $cr->new('1e2e2'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
82$x = $cr->new('1+2+2'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
83# failed due to BigFlaot bug
84$x = $cr->new('1.2.2'); ok ($x,'NaN'); ok ($x->{_n}, '0'); ok ($x->{_d},'0');
85
86ok ($cr->new('123a4'),'NaN');
87ok ($cr->new('123e4'),'1230000');
88ok ($cr->new('-NaN'),'NaN');
89ok ($cr->new('NaN'),'NaN');
90ok ($cr->new('+inf'),'inf');
91ok ($cr->new('-inf'),'-inf');
8f675a64 92
93##############################################################################
94# mixed arguments
95
a4e2b1c6 96ok ($cr->new('3/7')->badd(1),'10/7');
97ok ($cr->new('3/10')->badd(1.1),'7/5');
98ok ($cr->new('3/7')->badd($mbi->new(1)),'10/7');
99ok ($cr->new('3/10')->badd($mbf->new('1.1')),'7/5');
8f675a64 100
a4e2b1c6 101ok ($cr->new('3/7')->bsub(1),'-4/7');
102ok ($cr->new('3/10')->bsub(1.1),'-4/5');
103ok ($cr->new('3/7')->bsub($mbi->new(1)),'-4/7');
104ok ($cr->new('3/10')->bsub($mbf->new('1.1')),'-4/5');
8f675a64 105
a4e2b1c6 106ok ($cr->new('3/7')->bmul(1),'3/7');
107ok ($cr->new('3/10')->bmul(1.1),'33/100');
108ok ($cr->new('3/7')->bmul($mbi->new(1)),'3/7');
109ok ($cr->new('3/10')->bmul($mbf->new('1.1')),'33/100');
8f675a64 110
a4e2b1c6 111ok ($cr->new('3/7')->bdiv(1),'3/7');
112ok ($cr->new('3/10')->bdiv(1.1),'3/11');
113ok ($cr->new('3/7')->bdiv($mbi->new(1)),'3/7');
114ok ($cr->new('3/10')->bdiv($mbf->new('1.1')),'3/11');
8f675a64 115
116##############################################################################
a4e2b1c6 117$x = $cr->new('1/4'); $y = $cr->new('1/3');
184f15d5 118ok ($x + $y, '7/12');
119ok ($x * $y, '1/12');
120ok ($x / $y, '3/4');
121
a4e2b1c6 122$x = $cr->new('7/5'); $x *= '3/2';
184f15d5 123ok ($x,'21/10');
124$x -= '0.1';
125ok ($x,'2'); # not 21/10
126
a4e2b1c6 127$x = $cr->new('2/3'); $y = $cr->new('3/2');
184f15d5 128ok ($x > $y,'');
129ok ($x < $y,1);
130ok ($x == $y,'');
131
a4e2b1c6 132$x = $cr->new('-2/3'); $y = $cr->new('3/2');
184f15d5 133ok ($x > $y,'');
134ok ($x < $y,'1');
135ok ($x == $y,'');
136
a4e2b1c6 137$x = $cr->new('-2/3'); $y = $cr->new('-2/3');
184f15d5 138ok ($x > $y,'');
139ok ($x < $y,'');
140ok ($x == $y,'1');
141
a4e2b1c6 142$x = $cr->new('-2/3'); $y = $cr->new('-1/3');
184f15d5 143ok ($x > $y,'');
144ok ($x < $y,'1');
145ok ($x == $y,'');
146
a4e2b1c6 147$x = $cr->new('-124'); $y = $cr->new('-122');
184f15d5 148ok ($x->bacmp($y),1);
149
a4e2b1c6 150$x = $cr->new('-124'); $y = $cr->new('-122');
184f15d5 151ok ($x->bcmp($y),-1);
152
a4e2b1c6 153$x = $cr->new('3/7'); $y = $cr->new('5/7');
8f675a64 154ok ($x+$y,'8/7');
155
a4e2b1c6 156$x = $cr->new('3/7'); $y = $cr->new('5/7');
8f675a64 157ok ($x*$y,'15/49');
158
a4e2b1c6 159$x = $cr->new('3/5'); $y = $cr->new('5/7');
8f675a64 160ok ($x*$y,'3/7');
161
a4e2b1c6 162$x = $cr->new('3/5'); $y = $cr->new('5/7');
8f675a64 163ok ($x/$y,'21/25');
164
93c87d9d 165$x = $cr->new('7/4'); $y = $cr->new('1');
166ok ($x % $y,'3/4');
167
168$x = $cr->new('7/4'); $y = $cr->new('5/13');
169ok ($x % $y,'11/52');
170
171$x = $cr->new('7/4'); $y = $cr->new('5/9');
172ok ($x % $y,'1/12');
173
a4e2b1c6 174$x = $cr->new('-144/9')->bsqrt(); ok ($x,'NaN');
175$x = $cr->new('144/9')->bsqrt(); ok ($x,'4');
93c87d9d 176$x = $cr->new('3/4')->bsqrt(); ok ($x,
177 '1732050807568877293527446341505872366943/'
178 .'2000000000000000000000000000000000000000');
184f15d5 179
a4e2b1c6 180##############################################################################
181# bpow
182
183$x = $cr->new('2/1'); $z = $x->bpow('3/1'); ok ($x,'8');
7d341013 184$x = $cr->new('1/2'); $z = $x->bpow('3/1'); ok ($x,'1/8');
185$x = $cr->new('1/3'); $z = $x->bpow('4/1'); ok ($x,'1/81');
186$x = $cr->new('2/3'); $z = $x->bpow('4/1'); ok ($x,'16/81');
187
188# XXX todo:
189#$x = $cr->new('2/3'); $z = $x->bpow('5/3'); ok ($x,'32/81 ???');
a4e2b1c6 190
191##############################################################################
192# bfac
193
194$x = $cr->new('1'); $x->bfac(); ok ($x,'1');
195for (my $i = 0; $i < 8; $i++)
196 {
197 $x = $cr->new("$i/1")->bfac(); ok ($x,$mbi->new($i)->bfac());
198 }
199
200# test for $self->bnan() vs. $x->bnan();
201$x = $cr->new('-1'); $x->bfac(); ok ($x,'NaN');
202
203##############################################################################
204# binc/bdec
205
206$x = $cr->new('3/2'); ok ($x->binc(),'5/2');
207$x = $cr->new('15/6'); ok ($x->bdec(),'3/2');
208
209##############################################################################
210# bfloor/bceil
211
212$x = $cr->new('-7/7'); ok ($x->{_n}, '1'); ok ($x->{_d}, '1');
213$x = $cr->new('-7/7')->bfloor(); ok ($x->{_n}, '1'); ok ($x->{_d}, '1');
214
215##############################################################################
7d341013 216# bsstr
217
218$x = $cr->new('7/5')->bsstr(); ok ($x,'7/5');
219$x = $cr->new('-7/5')->bsstr(); ok ($x,'-7/5');
220
221##############################################################################
222# numify()
223
224my @array = qw/1 2 3 4 5 6 7 8 9/;
225$x = $cr->new('8/8'); ok ($array[$x],2);
226$x = $cr->new('16/8'); ok ($array[$x],3);
227$x = $cr->new('17/8'); ok ($array[$x],3);
228$x = $cr->new('33/8'); ok ($array[$x],5);
229$x = $cr->new('-33/8'); ok ($array[$x],6);
230
231$x = $cr->new('33/8'); ok ($x->numify() * 1000, 4125);
232$x = $cr->new('-33/8'); ok ($x->numify() * 1000, -4125);
233$x = $cr->new('inf'); ok ($x->numify(), 'inf');
234$x = $cr->new('-inf'); ok ($x->numify(), '-inf');
235$x = $cr->new('NaN'); ok ($x->numify(), 'NaN');
236
93c87d9d 237$x = $cr->new('4/3'); ok ($x->numify(), 4/3);
238
7d341013 239##############################################################################
184f15d5 240# done
241
2421;
243