10 unshift @INC, '../lib'; # for running manually
14 # testing of Math::BigRat
21 my $cr = 'Math::BigRat';
22 my $mbi = 'Math::BigInt';
23 my $mbf = 'Math::BigFloat';
27 $x = Math::BigRat->new(1234); ok ($x,1234);
28 ok ($x->isa('Math::BigRat'));
29 ok (!$x->isa('Math::BigFloat'));
30 ok (!$x->isa('Math::BigInt'));
32 ##############################################################################
35 foreach my $func (qw/new bnorm/)
37 $x = $cr->$func(1234); ok ($x,1234);
39 $x = $cr->$func('1234/1'); ok ($x,1234);
40 $x = $cr->$func('1234/2'); ok ($x,617);
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);
47 $x = $cr->$func('1e2/1e1'); ok ($x,10);
48 $x = $cr->$func('1 / 3'); ok ($x,'1/3');
49 $x = $cr->$func('-1 / 3'); ok ($x,'-1/3');
50 $x = $cr->$func('NaN'); ok ($x,'NaN');
51 $x = $cr->$func('inf'); ok ($x,'inf');
52 $x = $cr->$func('-inf'); ok ($x,'-inf');
53 $x = $cr->$func('1/'); ok ($x,'NaN');
55 # input ala '1+1/3' isn't parsed ok yet
56 $x = $cr->$func('1+1/3'); ok ($x,'NaN');
58 ############################################################################
59 # other classes as input
61 $x = $cr->$func($mbi->new(1231)); ok ($x,'1231');
62 $x = $cr->$func($mbf->new(1232)); ok ($x,'1232');
63 $x = $cr->$func($mbf->new(1232.3)); ok ($x,'12323/10');
66 ##############################################################################
69 ok (Math::BigRat->new('3/7')->badd(1),'10/7');
70 ok (Math::BigRat->new('3/10')->badd(1.1),'7/5');
71 ok (Math::BigRat->new('3/7')->badd(Math::BigInt->new(1)),'10/7');
72 ok (Math::BigRat->new('3/10')->badd(Math::BigFloat->new('1.1')),'7/5');
74 ok (Math::BigRat->new('3/7')->bsub(1),'-4/7');
75 ok (Math::BigRat->new('3/10')->bsub(1.1),'-4/5');
76 ok (Math::BigRat->new('3/7')->bsub(Math::BigInt->new(1)),'-4/7');
77 ok (Math::BigRat->new('3/10')->bsub(Math::BigFloat->new('1.1')),'-4/5');
79 ok (Math::BigRat->new('3/7')->bmul(1),'3/7');
80 ok (Math::BigRat->new('3/10')->bmul(1.1),'33/100');
81 ok (Math::BigRat->new('3/7')->bmul(Math::BigInt->new(1)),'3/7');
82 ok (Math::BigRat->new('3/10')->bmul(Math::BigFloat->new('1.1')),'33/100');
84 ok (Math::BigRat->new('3/7')->bdiv(1),'3/7');
85 ok (Math::BigRat->new('3/10')->bdiv(1.1),'3/11');
86 ok (Math::BigRat->new('3/7')->bdiv(Math::BigInt->new(1)),'3/7');
87 ok (Math::BigRat->new('3/10')->bdiv(Math::BigFloat->new('1.1')),'3/11');
89 ##############################################################################
90 $x = Math::BigRat->new('1/4'); $y = Math::BigRat->new('1/3');
95 $x = Math::BigRat->new('7/5'); $x *= '3/2';
98 ok ($x,'2'); # not 21/10
100 $x = Math::BigRat->new('2/3'); $y = Math::BigRat->new('3/2');
105 $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('3/2');
110 $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('-2/3');
115 $x = Math::BigRat->new('-2/3'); $y = Math::BigRat->new('-1/3');
120 $x = Math::BigRat->new('-124'); $y = Math::BigRat->new('-122');
121 ok ($x->bacmp($y),1);
123 $x = Math::BigRat->new('-124'); $y = Math::BigRat->new('-122');
124 ok ($x->bcmp($y),-1);
126 $x = Math::BigRat->new('3/7'); $y = Math::BigRat->new('5/7');
129 $x = Math::BigRat->new('3/7'); $y = Math::BigRat->new('5/7');
132 $x = Math::BigRat->new('3/5'); $y = Math::BigRat->new('5/7');
135 $x = Math::BigRat->new('3/5'); $y = Math::BigRat->new('5/7');
138 $x = Math::BigRat->new('-144/9'); $x->bsqrt(); ok ($x,'NaN');
139 $x = Math::BigRat->new('144/9'); $x->bsqrt(); ok ($x,'4');