From: John Peacock Date: Tue, 1 Aug 2000 09:38:12 +0000 (-0400) Subject: Re: [PATCH]Re: Questions about Math::BigFloat X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=3deb277dac3b5f1492bde2aa49ab00aeb793ba7b;p=p5sagit%2Fp5-mst-13.2.git Re: [PATCH]Re: Questions about Math::BigFloat Message-ID: <3986D2C4.54B78FE4@UnivPress.com> p4raw-id: //depot/perl@6673 --- diff --git a/lib/Math/BigFloat.pm b/lib/Math/BigFloat.pm index d8d643c..74a023e 100644 --- a/lib/Math/BigFloat.pm +++ b/lib/Math/BigFloat.pm @@ -4,6 +4,7 @@ use Math::BigInt; use Exporter; # just for use to be happy @ISA = (Exporter); +$VERSION = '0.01'; # never had version before use overload '+' => sub {new Math::BigFloat &fadd}, @@ -85,6 +86,7 @@ sub fnorm { #(string) return fnum_str # normalize number -- for internal use sub norm { #(mantissa, exponent) return fnum_str local($_, $exp) = @_; + $exp = 0 unless defined $exp; if ($_ eq 'NaN') { 'NaN'; } else { @@ -174,12 +176,14 @@ sub round { #(int_str, int_str, int_str) return int_str } else { local($cmp) = Math::BigInt::bcmp(Math::BigInt::bmul($r,'+2'),$base); if ( $cmp < 0 || - ($cmp == 0 && - ( $rnd_mode eq 'zero' || + ($cmp == 0 && ( + ($rnd_mode eq 'zero' ) || ($rnd_mode eq '-inf' && (substr($q,$[,1) eq '+')) || ($rnd_mode eq '+inf' && (substr($q,$[,1) eq '-')) || - ($rnd_mode eq 'even' && $q =~ /[24680]$/) || - ($rnd_mode eq 'odd' && $q =~ /[13579]$/) )) ) { + ($rnd_mode eq 'even' && $q =~ /[13579]$/ ) || + ($rnd_mode eq 'odd' && $q =~ /[24680]$/ ) ) + ) + ) { $q; # round down } else { Math::BigInt::badd($q, ((substr($q,$[,1) eq '-') ? '-1' : '+1')); @@ -244,9 +248,17 @@ sub fcmp #(fnum_str, fnum_str) return cond_code if ($xm eq '+0' || $ym eq '+0') { return $xm <=> $ym; } - ord($y) <=> ord($x) - || ($xe <=> $ye) * (substr($x,$[,1).'1') - || Math::BigInt::cmp($xm,$ym); + if ( $xe < $ye ) # adjust the exponents to be equal + { + $ym .= '0' x ($ye - $xe); + $ye = $xe; + } + elsif ( $ye < $xe ) # same here + { + $xm .= '0' x ($xe - $ye); + $xe = $ye; + } + return Math::BigInt::cmp($xm,$ym); } } diff --git a/lib/Math/BigInt.pm b/lib/Math/BigInt.pm index a43969c..808e552 100644 --- a/lib/Math/BigInt.pm +++ b/lib/Math/BigInt.pm @@ -1,4 +1,5 @@ package Math::BigInt; +$VERSION='0.01'; use overload '+' => sub {new Math::BigInt &badd}, diff --git a/t/lib/bigfltpm.t b/t/lib/bigfltpm.t index 5d97f1b..971b4ee 100755 --- a/t/lib/bigfltpm.t +++ b/t/lib/bigfltpm.t @@ -164,15 +164,15 @@ $Math::BigFloat::rnd_mode = 'odd' -50123456789:5:-50123000000 +50123456789:9:50123456800 -50123456789:9:-50123456800 -+501234500:6:501235000 --501234500:6:-501235000 ++501234500:6:501234000 +-501234500:6:-501234000 $Math::BigFloat::rnd_mode = 'even' +60123456789:5:60123000000 -60123456789:5:-60123000000 +60123456789:9:60123456800 -60123456789:9:-60123456800 -+601234500:6:601234000 --601234500:6:-601234000 ++601234500:6:601235000 +-601234500:6:-601235000 &ffround $Math::BigFloat::rnd_mode = 'trunc' +1.23:-1:1.2 @@ -235,13 +235,13 @@ $Math::BigFloat::rnd_mode = 'odd' -5.23:-1:/-5.2(?:0{5}\d+)? +5.27:-1:/5.(?:3|29{5}\d+) -5.27:-1:/-5.(?:3|29{5}\d+) -+5.25:-1:/5.(?:3|29{5}\d+) --5.25:-1:/-5.(?:3|29{5}\d+) -+5.35:-1:/5.(?:3|29{5}\d+) --5.35:-1:/-5.(?:3|29{5}\d+) ++5.25:-1:/5.(?:2|29{5}\d+) +-5.25:-1:/-5.(?:2|29{5}\d+) ++5.35:-1:/5.(?:4|29{5}\d+) +-5.35:-1:/-5.(?:4|29{5}\d+) -0.0065:-1:0 -0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.007|-7e-03 +-0.0065:-3:/-0\.006|-6e-03 -0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 $Math::BigFloat::rnd_mode = 'even' @@ -249,13 +249,13 @@ $Math::BigFloat::rnd_mode = 'even' -6.23:-1:/-6.2(?:0{5}\d+)? +6.27:-1:/6.(?:3|29{5}\d+) -6.27:-1:/-6.(?:3|29{5}\d+) -+6.25:-1:/6.2(?:0{5}\d+)? --6.25:-1:/-6.2(?:0{5}\d+)? -+6.35:-1:/6.(?:4|39{5}\d+) --6.35:-1:/-6.(?:4|39{5}\d+) ++6.25:-1:/6.3(?:0{5}\d+)? +-6.25:-1:/-6.3(?:0{5}\d+)? ++6.35:-1:/6.(?:3|39{5}\d+) +-6.35:-1:/-6.(?:3|39{5}\d+) -0.0065:-1:0 -0.0065:-2:/-0\.01|-1e-02 --0.0065:-3:/-0\.006|-6e-03 +-0.0065:-3:/-0\.007|-7e-03 -0.0065:-4:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 -0.0065:-5:/-0\.006(?:5|49{5}\d+)|-6\.5e-03 &fcmp