summaryrefslogtreecommitdiff
path: root/lib/Math/BigFloat.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Math/BigFloat.pm')
-rw-r--r--lib/Math/BigFloat.pm36
1 files changed, 24 insertions, 12 deletions
diff --git a/lib/Math/BigFloat.pm b/lib/Math/BigFloat.pm
index 32f0a21c40..dfd722c836 100644
--- a/lib/Math/BigFloat.pm
+++ b/lib/Math/BigFloat.pm
@@ -11,7 +11,7 @@
package Math::BigFloat;
-$VERSION = '1.20';
+$VERSION = '1.21';
require 5.005;
use Exporter;
use Math::BigInt qw/objectify/;
@@ -33,10 +33,9 @@ use vars qw/$AUTOLOAD $accuracy $precision $div_scale $rnd_mode/;
my $class = "Math::BigFloat";
use overload
-'<=>' => sub {
- $_[2] ?
- $class->bcmp($_[1],$_[0]) :
- $class->bcmp($_[0],$_[1])},
+'<=>' => sub { $_[2] ?
+ ref($_[0])->bcmp($_[1],$_[0]) :
+ ref($_[0])->bcmp($_[0],$_[1])},
'int' => sub { $_[0]->as_number() }, # 'trunc' to bigint
;
@@ -345,18 +344,31 @@ sub bcmp
return 1 if $yz && $x->{sign} eq '+'; # +x <=> 0
# adjust so that exponents are equal
- my $lx = $x->{_m}->length() + $x->{_e};
- my $ly = $y->{_m}->length() + $y->{_e};
+ my $lxm = $x->{_m}->length();
+ my $lym = $y->{_m}->length();
+ my $lx = $lxm + $x->{_e};
+ my $ly = $lym + $y->{_e};
# print "x $x y $y lx $lx ly $ly\n";
my $l = $lx - $ly; $l = -$l if $x->{sign} eq '-';
# print "$l $x->{sign}\n";
- return $l if $l != 0;
+ return $l <=> 0 if $l != 0;
- # lengths are equal, so compare mantissa, if equal, compare exponents
- # this assumes normalized numbers (no trailing zeros etc!)
- my $rc = $x->{_m} <=> $y->{_m} || $x->{_e} <=> $y->{_e};
+ # lengths (corrected by exponent) are equal
+ # so make mantissa euqal length by padding with zero (shift left)
+ my $diff = $lxm - $lym;
+ my $xm = $x->{_m}; # not yet copy it
+ my $ym = $y->{_m};
+ if ($diff > 0)
+ {
+ $ym = $y->{_m}->copy()->blsft($diff,10);
+ }
+ elsif ($diff < 0)
+ {
+ $xm = $x->{_m}->copy()->blsft(-$diff,10);
+ }
+ my $rc = $xm->bcmp($ym);
$rc = -$rc if $x->{sign} eq '-'; # -124 < -123
- return $rc;
+ return $rc <=> 0;
}
sub bacmp