diff options
author | Ilya Zakharevich <ilya@math.berkeley.edu> | 2001-01-24 14:06:57 -0500 |
---|---|---|
committer | Jarkko Hietaniemi <jhi@iki.fi> | 2001-01-25 04:16:32 +0000 |
commit | f216259dc50e3a06164781e025bbb486cdc1dbaa (patch) | |
tree | e8ed6fb1558ef7ad342bdb1e96cef33c01c92f07 /lib/Math/BigFloat.pm | |
parent | f2766b05f6136cc9e8c8812afdbe7a31657a110d (diff) | |
download | perl-f216259dc50e3a06164781e025bbb486cdc1dbaa.tar.gz |
overload int()
Message-ID: <20010124190657.A8512@math.ohio-state.edu>
p4raw-id: //depot/perl@8545
Diffstat (limited to 'lib/Math/BigFloat.pm')
-rw-r--r-- | lib/Math/BigFloat.pm | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lib/Math/BigFloat.pm b/lib/Math/BigFloat.pm index 74a023e0d8..4c520fdd49 100644 --- a/lib/Math/BigFloat.pm +++ b/lib/Math/BigFloat.pm @@ -18,6 +18,7 @@ use overload scalar fdiv(${$_[0]},$_[1])}, 'neg' => sub {new Math::BigFloat &fneg}, 'abs' => sub {new Math::BigFloat &fabs}, +'int' => sub {new Math::BigInt &f2int}, qw( "" stringify @@ -58,6 +59,13 @@ sub stringify { return $n; } +sub import { + shift; + return unless @_; + die "unknown import: @_" unless @_ == 1 and $_[0] eq ':constant'; + overload::constant float => sub {Math::BigFloat->new(shift)}; +} + $div_scale = 40; # Rounding modes one of 'even', 'odd', '+inf', '-inf', 'zero' or 'trunc'. @@ -235,6 +243,26 @@ sub ffround { #(fnum_str, scale) return fnum_str } } } + +# Calculate the integer part of $x +sub f2int { #(fnum_str) return inum_str + local($x) = ${$_[$[]}; + if ($x eq 'NaN') { + die "Attempt to take int(NaN)"; + } else { + local($xm,$xe) = split('E',$x); + if ($xe >= 0) { + $xm . '0' x $xe; + } else { + $xe = length($xm)+$xe; + if ($xe <= 1) { + '+0'; + } else { + substr($xm,$[,$xe); + } + } + } +} # compare 2 values returns one of undef, <0, =0, >0 # returns undef if either or both input value are not numbers |