1#!/usr/bin/perl -w 2 3# test the helper math routines in Math::BigFloat 4 5use Test::More; 6use strict; 7 8BEGIN 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 34use Math::BigFloat lib => 'Calc'; 35 36############################################################################# 37# add 38 39my $a = Math::BigInt::Calc->_new("123"); 40my $b = Math::BigInt::Calc->_new("321"); 41 42my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+'); 43is (_str($x,$xs), '+444', 'add two positive numbers'); 44is (_str($a,''), '444', 'a modified'); 45 46($x,$xs) = _add (123,321,'+','+'); 47is (_str($x,$xs), '+444', 'add two positive numbers'); 48 49($x,$xs) = _add (123,321,'+','-'); 50is (_str($x,$xs), '-198', 'add +x + -y'); 51($x,$xs) = _add (123,321,'-','+'); 52is (_str($x,$xs), '+198', 'add -x + +y'); 53 54($x,$xs) = _add (321,123,'-','+'); 55is (_str($x,$xs), '-198', 'add -x + +y'); 56($x,$xs) = _add (321,123,'+','-'); 57is (_str($x,$xs), '+198', 'add +x + -y'); 58 59($x,$xs) = _add (10,1,'+','-'); 60is (_str($x,$xs), '+9', 'add 10 + -1'); 61($x,$xs) = _add (10,1,'-','+'); 62is (_str($x,$xs), '-9', 'add -10 + +1'); 63($x,$xs) = _add (1,10,'-','+'); 64is (_str($x,$xs), '+9', 'add -1 + 10'); 65($x,$xs) = _add (1,10,'+','-'); 66is (_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,'+','+'); 74is (_str($x,$xs), '+198', 'sub two positive numbers'); 75is (_str($b,''), '198', 'a modified'); 76 77($x,$xs) = _sub (123,321,'+','-'); 78is (_str($x,$xs), '+444', 'sub +x + -y'); 79($x,$xs) = _sub (123,321,'-','+'); 80is (_str($x,$xs), '-444', 'sub -x + +y'); 81 82sub _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 94sub _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 106sub _str 107 { 108 my ($x,$s) = @_; 109 110 $s . Math::BigInt::Calc->_str($x); 111 } 112