diff options
author | Tels <nospam-abuse@bloodgate.com> | 2004-03-12 19:02:30 +0100 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2004-03-12 20:08:31 +0000 |
commit | 27e7b8bb4225378079f42e58c59f6131c62cace5 (patch) | |
tree | 044e7a68574da0633769a12d0a28728236123f2a /lib/Math | |
parent | e01c5899dd5c15c076ff80faae646f1bb851ca52 (diff) | |
download | perl-27e7b8bb4225378079f42e58c59f6131c62cace5.tar.gz |
Math::BigInt v1.70, bignum 0.15, Math::BigRat 0.12
Message-Id: <200403121802.31679@bloodgate.com>
p4raw-id: //depot/perl@22491
Diffstat (limited to 'lib/Math')
-rw-r--r-- | lib/Math/BigFloat.pm | 25 | ||||
-rw-r--r-- | lib/Math/BigInt/t/_e_math.t | 111 | ||||
-rw-r--r-- | lib/Math/BigInt/t/bare_mbf.t | 2 | ||||
-rw-r--r-- | lib/Math/BigInt/t/bigfltpm.inc | 5 | ||||
-rwxr-xr-x | lib/Math/BigInt/t/bigfltpm.t | 2 | ||||
-rwxr-xr-x | lib/Math/BigInt/t/sub_mbf.t | 2 | ||||
-rw-r--r-- | lib/Math/BigInt/t/with_sub.t | 2 |
7 files changed, 140 insertions, 9 deletions
diff --git a/lib/Math/BigFloat.pm b/lib/Math/BigFloat.pm index a4ddd3807a..f7008aacf6 100644 --- a/lib/Math/BigFloat.pm +++ b/lib/Math/BigFloat.pm @@ -67,6 +67,7 @@ my $LOG_10_A = length($LOG_10)-1; my $LOG_2 = '0.6931471805599453094172321214581765680755001343602552541206800094933936220'; my $LOG_2_A = length($LOG_2)-1; +my $HALF = '0.5'; # made into an object if necc. ############################################################################## # the old code had $rnd_mode, so we need to support it, too @@ -1043,10 +1044,11 @@ sub _log_10 ### Since $x in the range 0.5 .. 1.5 is MUCH faster, we do a repeated div ### or mul by 2 (maximum times 3, since x < 10 and x > 0.1) - my $half = $self->new('0.5'); + $HALF = $self->new($HALF) unless ref($HALF); + my $twos = 0; # default: none (0 times) my $two = $self->new(2); - while ($x->bacmp($half) <= 0) + while ($x->bacmp($HALF) <= 0) { $twos--; $x->bmul($two); } @@ -1506,9 +1508,20 @@ sub broot local $Math::BigInt::upgrade = undef; # should be really parent class vs MBI # remember sign and make $x positive, since -4 ** (1/2) => -2 - my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->babs(); + my $sign = 0; $sign = 1 if $x->{sign} eq '-'; $x->{sign} = '+'; + + my $is_two = 0; + if ($y->isa('Math::BigFloat')) + { + $is_two = ($y->{sign} eq '+' && $MBI->_is_two($y->{_m}) && $MBI->_is_zero($y->{_e})); + } + else + { + $is_two = ($y == 2); + } - if ($y->bcmp(2) == 0) # normal square root + # normal square root if $y == 2: + if ($is_two) { $x->bsqrt($scale+4); } @@ -1770,7 +1783,8 @@ sub _pow my $self = ref($x); # if $y == 0.5, it is sqrt($x) - return $x->bsqrt($a,$p,$r,$y) if $y->bcmp('0.5') == 0; + $HALF = $self->new($HALF) unless ref($HALF); + return $x->bsqrt($a,$p,$r,$y) if $y->bcmp($HALF) == 0; # Using: # a ** x == e ** (x * ln a) @@ -2342,6 +2356,7 @@ sub bnorm if ($MBI->_acmp($x->{_e},$z) >= 0) { $x->{_e} = $MBI->_sub ($x->{_e}, $z); + $x->{_es} = '+' if $MBI->_is_zero($x->{_e}); } else { diff --git a/lib/Math/BigInt/t/_e_math.t b/lib/Math/BigInt/t/_e_math.t new file mode 100644 index 0000000000..3db33183a8 --- /dev/null +++ b/lib/Math/BigInt/t/_e_math.t @@ -0,0 +1,111 @@ +#!/usr/bin/perl -w + +# test the helper math routines in Math::BigFloat + +use Test::More; +use strict; + +BEGIN + { + $| = 1; + # to locate the testing files + my $location = $0; $location =~ s/_e_math.t//i; + if ($ENV{PERL_CORE}) + { + # testing with the core distribution + @INC = qw(../lib); + } + unshift @INC, '../lib'; + if (-d 't') + { + chdir 't'; + require File::Spec; + unshift @INC, File::Spec->catdir(File::Spec->updir, $location); + } + else + { + unshift @INC, $location; + } + print "# INC = @INC\n"; + + plan tests => 26; + } + +use Math::BigFloat; + +############################################################################# +# add + +my $a = Math::BigInt::Calc->_new("123"); +my $b = Math::BigInt::Calc->_new("321"); + +my ($x, $xs) = Math::BigFloat::_e_add($a,$b,'+','+'); +is (_str($x,$xs), '+444', 'add two positive numbers'); +is (_str($a,''), '444', 'a modified'); + +($x,$xs) = _add (123,321,'+','+'); +is (_str($x,$xs), '+444', 'add two positive numbers'); + +($x,$xs) = _add (123,321,'+','-'); +is (_str($x,$xs), '-198', 'add +x + -y'); +($x,$xs) = _add (123,321,'-','+'); +is (_str($x,$xs), '+198', 'add -x + +y'); + +($x,$xs) = _add (321,123,'-','+'); +is (_str($x,$xs), '-198', 'add -x + +y'); +($x,$xs) = _add (321,123,'+','-'); +is (_str($x,$xs), '+198', 'add +x + -y'); + +($x,$xs) = _add (10,1,'+','-'); +is (_str($x,$xs), '+9', 'add 10 + -1'); +($x,$xs) = _add (10,1,'-','+'); +is (_str($x,$xs), '-9', 'add -10 + +1'); +($x,$xs) = _add (1,10,'-','+'); +is (_str($x,$xs), '+9', 'add -1 + 10'); +($x,$xs) = _add (1,10,'+','-'); +is (_str($x,$xs), '-9', 'add 1 + -10'); + +############################################################################# +# sub + +$a = Math::BigInt::Calc->_new("123"); +$b = Math::BigInt::Calc->_new("321"); +($x, $xs) = Math::BigFloat::_e_sub($b,$a,'+','+'); +is (_str($x,$xs), '+198', 'sub two positive numbers'); +is (_str($b,''), '198', 'a modified'); + +($x,$xs) = _sub (123,321,'+','-'); +is (_str($x,$xs), '+444', 'sub +x + -y'); +($x,$xs) = _sub (123,321,'-','+'); +is (_str($x,$xs), '-444', 'sub -x + +y'); + +sub _add + { + my ($a,$b,$as,$bs) = @_; + + my $aa = Math::BigInt::Calc->_new($a); + my $bb = Math::BigInt::Calc->_new($b); + my ($x, $xs) = Math::BigFloat::_e_add($aa,$bb,$as,$bs); + is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), + 'param0 modified'); + ($x,$xs); + } + +sub _sub + { + my ($a,$b,$as,$bs) = @_; + + my $aa = Math::BigInt::Calc->_new($a); + my $bb = Math::BigInt::Calc->_new($b); + my ($x, $xs) = Math::BigFloat::_e_sub($aa,$bb,$as,$bs); + is (Math::BigInt::Calc->_str($x), Math::BigInt::Calc->_str($aa), + 'param0 modified'); + ($x,$xs); + } + +sub _str + { + my ($x,$s) = @_; + + $s . Math::BigInt::Calc->_str($x); + } diff --git a/lib/Math/BigInt/t/bare_mbf.t b/lib/Math/BigInt/t/bare_mbf.t index 9f946717d2..cbca372454 100644 --- a/lib/Math/BigInt/t/bare_mbf.t +++ b/lib/Math/BigInt/t/bare_mbf.t @@ -27,7 +27,7 @@ BEGIN } print "# INC = @INC\n"; - plan tests => 1814; + plan tests => 1815; } use Math::BigFloat lib => 'BareCalc'; diff --git a/lib/Math/BigInt/t/bigfltpm.inc b/lib/Math/BigInt/t/bigfltpm.inc index d307ee6a27..5e1c19f921 100644 --- a/lib/Math/BigInt/t/bigfltpm.inc +++ b/lib/Math/BigInt/t/bigfltpm.inc @@ -252,6 +252,11 @@ $class->precision(-3); $x = $class->new(12); $x->fsqrt(); ok ($x,'3.464'); ok ($class->new(-1)->is_one(),0); ok ($class->new(-1)->is_one('-'),1); +############################################################################# +# bug 1/0.5 leaving 2e-0 instead of 2e0 + +ok ($class->new(1)->fdiv('0.5')->bsstr(),'2e+0'); + 1; # all done ############################################################################### diff --git a/lib/Math/BigInt/t/bigfltpm.t b/lib/Math/BigInt/t/bigfltpm.t index 3fce4605b3..9e50f5e7c4 100755 --- a/lib/Math/BigInt/t/bigfltpm.t +++ b/lib/Math/BigInt/t/bigfltpm.t @@ -26,7 +26,7 @@ BEGIN } print "# INC = @INC\n"; - plan tests => 1814 + plan tests => 1815 + 2; # own tests } diff --git a/lib/Math/BigInt/t/sub_mbf.t b/lib/Math/BigInt/t/sub_mbf.t index 9a8b9a396c..8550a97ded 100755 --- a/lib/Math/BigInt/t/sub_mbf.t +++ b/lib/Math/BigInt/t/sub_mbf.t @@ -26,7 +26,7 @@ BEGIN } print "# INC = @INC\n"; - plan tests => 1814 + plan tests => 1815 + 6; # + our own tests } diff --git a/lib/Math/BigInt/t/with_sub.t b/lib/Math/BigInt/t/with_sub.t index d7391d94ae..3d480304a9 100644 --- a/lib/Math/BigInt/t/with_sub.t +++ b/lib/Math/BigInt/t/with_sub.t @@ -28,7 +28,7 @@ BEGIN } print "# INC = @INC\n"; - plan tests => 1814 + plan tests => 1815 + 1; } |