Commit | Line | Data |
27e7b8bb |
1 | #!/usr/bin/perl -w |
2 | |
3 | # test the helper math routines in Math::BigFloat |
4 | |
5 | use Test::More; |
6 | use strict; |
7 | |
8 | BEGIN |
9 | { |
10 | $| = 1; |
11 | # to locate the testing files |
12 | my $location = $0; $location =~ s/_e_math.t//i; |
13 | if ($ENV{PERL_CORE}) |
14 | { |
15 | # testing with the core distribution |
16 | @INC = qw(../lib); |
17 | } |
18 | unshift @INC, '../lib'; |
19 | if (-d 't') |
20 | { |
21 | chdir 't'; |
22 | require File::Spec; |
23 | unshift @INC, File::Spec->catdir(File::Spec->updir, $location); |
24 | } |
25 | else |
26 | { |
27 | unshift @INC, $location; |
28 | } |
29 | print "# INC = @INC\n"; |
30 | |
31 | plan tests => 26; |
32 | } |
33 | |
233f7bc0 |
34 | use Math::BigFloat lib => 'Calc'; |
27e7b8bb |
35 | |
36 | ############################################################################# |
37 | # add |
38 | |
39 | my $a = Math::BigInt::Calc->_new("123"); |
40 | my $b = Math::BigInt::Calc->_new("321"); |
41 | |
42 | my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+'); |
43 | is (_str($x,$xs), '+444', 'add two positive numbers'); |
44 | is (_str($a,''), '444', 'a modified'); |
45 | |
46 | ($x,$xs) = _add (123,321,'+','+'); |
47 | is (_str($x,$xs), '+444', 'add two positive numbers'); |
48 | |
49 | ($x,$xs) = _add (123,321,'+','-'); |
50 | is (_str($x,$xs), '-198', 'add +x + -y'); |
51 | ($x,$xs) = _add (123,321,'-','+'); |
52 | is (_str($x,$xs), '+198', 'add -x + +y'); |
53 | |
54 | ($x,$xs) = _add (321,123,'-','+'); |
55 | is (_str($x,$xs), '-198', 'add -x + +y'); |
56 | ($x,$xs) = _add (321,123,'+','-'); |
57 | is (_str($x,$xs), '+198', 'add +x + -y'); |
58 | |
59 | ($x,$xs) = _add (10,1,'+','-'); |
60 | is (_str($x,$xs), '+9', 'add 10 + -1'); |
61 | ($x,$xs) = _add (10,1,'-','+'); |
62 | is (_str($x,$xs), '-9', 'add -10 + +1'); |
63 | ($x,$xs) = _add (1,10,'-','+'); |
64 | is (_str($x,$xs), '+9', 'add -1 + 10'); |
65 | ($x,$xs) = _add (1,10,'+','-'); |
66 | is (_str($x,$xs), '-9', 'add 1 + -10'); |
67 | |
68 | ############################################################################# |
69 | # sub |
70 | |
71 | $a = Math::BigInt::Calc->_new("123"); |
72 | $b = Math::BigInt::Calc->_new("321"); |
73 | ($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+'); |
74 | is (_str($x,$xs), '+198', 'sub two positive numbers'); |
75 | is (_str($b,''), '198', 'a modified'); |
76 | |
77 | ($x,$xs) = _sub (123,321,'+','-'); |
78 | is (_str($x,$xs), '+444', 'sub +x + -y'); |
79 | ($x,$xs) = _sub (123,321,'-','+'); |
80 | is (_str($x,$xs), '-444', 'sub -x + +y'); |
81 | |
82 | sub _add |
83 | { |
84 | my ($a,$b,$as,$bs) = @_; |
85 | |
86 | my $aa = Math::BigInt::Calc->_new($a); |
87 | my $bb = Math::BigInt::Calc->_new($b); |
88 | my ($x, $xs) = Math::BigFloat::_e_add($aa,$bb,$as,$bs); |
89 | is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), |
90 | 'param0 modified'); |
91 | ($x,$xs); |
92 | } |
93 | |
94 | sub _sub |
95 | { |
96 | my ($a,$b,$as,$bs) = @_; |
97 | |
98 | my $aa = Math::BigInt::Calc->_new($a); |
99 | my $bb = Math::BigInt::Calc->_new($b); |
100 | my ($x, $xs) = Math::BigFloat::_e_sub($aa,$bb,$as,$bs); |
101 | is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), |
102 | 'param0 modified'); |
103 | ($x,$xs); |
104 | } |
105 | |
106 | sub _str |
107 | { |
108 | my ($x,$s) = @_; |
109 | |
110 | $s . Math::BigInt::Calc->_str($x); |
111 | } |