summaryrefslogtreecommitdiff
path: root/lib/Math/BigRat/t/bigrat.t
blob: 9475426c7aa1a3f3d632377f41d95233b87e22b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/perl -w

use strict;
use Test;

BEGIN 
  {
  $| = 1;
  chdir 't' if -d 't';
  unshift @INC, '../lib'; # for running manually
  plan tests => 61;
  }

# testing of Math::BigRat

use Math::BigRat;

my ($x,$y,$z);

$x = Math::BigRat->new(1234); 		ok ($x,1234);
ok ($x->isa('Math::BigRat'));
ok (!$x->isa('Math::BigFloat'));
ok (!$x->isa('Math::BigInt'));

##############################################################################
# new

$x = Math::BigRat->new(1234); 		ok ($x,1234);
$x = Math::BigRat->new('1234/1'); 	ok ($x,1234);
$x = Math::BigRat->new('1234/2'); 	ok ($x,617);

$x = Math::BigRat->new('100/1.0');	ok ($x,100);
$x = Math::BigRat->new('10.0/1.0');	ok ($x,10);
$x = Math::BigRat->new('0.1/10');	ok ($x,'1/100');
$x = Math::BigRat->new('0.1/0.1');	ok ($x,'1');
$x = Math::BigRat->new('1e2/10');	ok ($x,10);
$x = Math::BigRat->new('1e2/1e1');	ok ($x,10);
$x = Math::BigRat->new('1 / 3');	ok ($x,'1/3');
$x = Math::BigRat->new('-1 / 3');	ok ($x,'-1/3');
$x = Math::BigRat->new('NaN');		ok ($x,'NaN');
$x = Math::BigRat->new('inf');		ok ($x,'inf');
$x = Math::BigRat->new('-inf');		ok ($x,'-inf');
$x = Math::BigRat->new('1/');		ok ($x,'NaN');

# input ala '1+1/3' isn't parsed ok yet
$x = Math::BigRat->new('1+1/3');		ok ($x,'NaN');

##############################################################################
# mixed arguments

ok (Math::BigRat->new('3/7')->badd(1),'10/7');
ok (Math::BigRat->new('3/10')->badd(1.1),'7/5');
ok (Math::BigRat->new('3/7')->badd(Math::BigInt->new(1)),'10/7');
ok (Math::BigRat->new('3/10')->badd(Math::BigFloat->new('1.1')),'7/5');

ok (Math::BigRat->new('3/7')->bsub(1),'-4/7');
ok (Math::BigRat->new('3/10')->bsub(1.1),'-4/5');
ok (Math::BigRat->new('3/7')->bsub(Math::BigInt->new(1)),'-4/7');
ok (Math::BigRat->new('3/10')->bsub(Math::BigFloat->new('1.1')),'-4/5');

ok (Math::BigRat->new('3/7')->bmul(1),'3/7');
ok (Math::BigRat->new('3/10')->bmul(1.1),'33/100');
ok (Math::BigRat->new('3/7')->bmul(Math::BigInt->new(1)),'3/7');
ok (Math::BigRat->new('3/10')->bmul(Math::BigFloat->new('1.1')),'33/100');

ok (Math::BigRat->new('3/7')->bdiv(1),'3/7');
ok (Math::BigRat->new('3/10')->bdiv(1.1),'3/11');
ok (Math::BigRat->new('3/7')->bdiv(Math::BigInt->new(1)),'3/7');
ok (Math::BigRat->new('3/10')->bdiv(Math::BigFloat->new('1.1')),'3/11');

##############################################################################
$x = Math::BigRat->new('1/4'); $y = Math::BigRat->new('1/3');
ok ($x + $y, '7/12');
ok ($x * $y, '1/12');
ok ($x / $y, '3/4');

$x = Math::BigRat->new('7/5'); $x *= '3/2'; 
ok ($x,'21/10');
$x -= '0.1';
ok ($x,'2');	# not 21/10

$x = Math::BigRat->new('2/3');		$y = Math::BigRat->new('3/2');
ok ($x > $y,'');		
ok ($x < $y,1);
ok ($x == $y,'');

$x = Math::BigRat->new('-2/3');		$y = Math::BigRat->new('3/2');
ok ($x > $y,'');		
ok ($x < $y,'1');
ok ($x == $y,'');

$x = Math::BigRat->new('-2/3');		$y = Math::BigRat->new('-2/3');
ok ($x > $y,'');		
ok ($x < $y,'');
ok ($x == $y,'1');

$x = Math::BigRat->new('-2/3');		$y = Math::BigRat->new('-1/3');
ok ($x > $y,'');		
ok ($x < $y,'1');
ok ($x == $y,'');

$x = Math::BigRat->new('-124');		$y = Math::BigRat->new('-122');
ok ($x->bacmp($y),1);

$x = Math::BigRat->new('-124');		$y = Math::BigRat->new('-122');
ok ($x->bcmp($y),-1);

$x = Math::BigRat->new('3/7');		$y = Math::BigRat->new('5/7');
ok ($x+$y,'8/7');

$x = Math::BigRat->new('3/7');		$y = Math::BigRat->new('5/7');
ok ($x*$y,'15/49');

$x = Math::BigRat->new('3/5');		$y = Math::BigRat->new('5/7');
ok ($x*$y,'3/7');

$x = Math::BigRat->new('3/5');		$y = Math::BigRat->new('5/7');
ok ($x/$y,'21/25');

$x = Math::BigRat->new('-144/9'); $x->bsqrt(); ok ($x,'NaN');
$x = Math::BigRat->new('144/9');  $x->bsqrt(); ok ($x,'4');

# done

1;