NAME
Math::BigFloat - Arbitrary size floating point math package
SYNOPSIS
use Math::BigFloat;
# not ready yet
# Number creation
$x = Math::BigInt->new($str); # defaults to 0
$nan = Math::BigInt->bnan(); # create a NotANumber
$zero = Math::BigInt->bzero();# create a "+0"
# Testing
$x->is_zero(); # return wether arg is zero or not
$x->is_one(); # return true if arg is +1
$x->is_one('-'); # return true if arg is -1
$x->is_odd(); # return true if odd, false for even
$x->is_even(); # return true if even, false for odd
$x->bcmp($y); # compare numbers (undef,<0,=0,>0)
$x->bacmp($y); # compare absolutely (undef,<0,=0,>0)
$x->sign(); # return the sign, either +,- or NaN
# The following all modify their first argument:
# set
$x->bzero(); # set $i to 0
$x->bnan(); # set $i to NaN
$x->bneg(); # negation
$x->babs(); # absolute value
$x->bnorm(); # normalize (no-op)
$x->bnot(); # two's complement (bit wise not)
$x->binc(); # increment x by 1
$x->bdec(); # decrement x by 1
$x->badd($y); # addition (add $y to $x)
$x->bsub($y); # subtraction (subtract $y from $x)
$x->bmul($y); # multiplication (multiply $x by $y)
$x->bdiv($y); # divide, set $i to quotient
# return (quo,rem) or quo if scalar
$x->bmod($y); # modulus
$x->bpow($y); # power of arguments (a**b)
$x->blsft($y); # left shift
$x->brsft($y); # right shift
# return (quo,rem) or quo if scalar
$x->band($y); # bit-wise and
$x->bior($y); # bit-wise inclusive or
$x->bxor($y); # bit-wise exclusive or
$x->bnot(); # bit-wise not (two's complement)
# The following do not modify their arguments:
bgcd(@values); # greatest common divisor
blcm(@values); # lowest common multiplicator
$x->bstr(); # return string
$x->bsstr(); # return string in scientific notation
$x->exponent(); # return exponent as BigInt
$x->mantissa(); # return mantissa as BigInt
DESCRIPTION
All operators (inlcuding basic math operations) are overloaded if you declare your big integers as
$i = new Math::BigFloat '123.456789123456789E-2';
Operations with overloaded operators preserve the arguments which is exactly what you expect.
- Canonical notation
-
not ready yet. Big integer values are strings of the form
/^[+-]\d+$/
with leading zeros suppressed.'-0' canonical value '-0', normalized '0' ' -123 123 123' canonical value '-123123123' '1 23 456 7890' canonical value '1234567890'
- Input
-
Input values to these routines may be either Math::BigFloat objects or strings of a relaxed canonical form (e.g. leading and trailin zeros are ok).
'' as well as other illegal numbers results in 'NaN'.
bnorm() on a BigFloat object is now effectively a no-op, since the numbers are always stored in normalized form. On a string, it creates a BigFloat object.
- Output
-
Output values are BigFloat objects (normalized), except for bstr(), which returns a string in normalized form. Some routines (
is_odd()
,is_even()
,is_zero()
,is_one()
) return true or false, while others (bcmp()
,bacmp()
) return either undef, <0, 0 or >0 and are suited for sort.bstr()
will always return a string with a decimal point as delimiter, whilebsstr()
will return a string in scientific notation, aka something like 1E-1.
Actual math is done by using BigInts to represent the mantissa and exponent. The sign /^[+-]$/
is stored separately. The string 'NaN' is used to represent the result when input arguments are not numbers, as well as the result of dividing by zero.
EXAMPLES
use Math::BigFloat qw(bstr bint);
# not ready yet
$x = bstr("1234") # string "1234"
$x = "$x"; # same as bstr()
$x = bneg("1234") # BigFloat "-1234"
$x = Math::BigFloat->bneg("1234"); # BigFloat "1234"
$x = Math::BigFloat->babs("-12345"); # BigFloat "12345"
$x = Math::BigFloat->bnorm("-0 00"); # BigFloat "0"
$x = bint(1) + bint(2); # BigFloat "3"
$x = bint(1) + "2"; # dito (auto-BigFloatify of "2")
$x = bint(1); # BigFloat "1"
$x = $x + 5 / 2; # BigFloat "3"
$x = $x ** 3; # BigFloat "27"
$x *= 2; # BigFloat "54"
$x = new Math::BigFloat; # BigFloat "0"
$x--; # BigFloat "-1"
Autocreating constants
After use Math::BigFloat ':constant'
all the floating point constants in the given scope are converted to Math::BigFloat
. This conversion happens at compile time.
In particular
perl -MMath::BigFloat=:constant -e 'print 2E-100,"\n"'
prints the value of 2E-100
. Note that without conversion of constants the expression 2E-100 will be calculated as normal floating point number.
PERFORMANCE
Greatly enhanced ;o) SectionNotReadyYet.
BUGS
None known yet.
CAVEAT
- stringify, bstr()
-
Not ready yet.
Both stringify and bstr() now drop the leading '+'. The old code would return '+1.23', the new returns '1.23'. This is to be consistent with Perl and to make cmp (especially with overloading) to work as you espect. It also solves problems with Test.pm, it's ok() uses 'eq' internally.
Mark said, when asked about to drop the '+' altogether, or make only cmp work:
I agree (with the first alternative), don't add the '+' on positive numbers. It's not as important anymore with the new internal form for numbers. It made doing things like abs and neg easier, but those have to be done differently now anyway.
So, the following examples now work all:
use Test; BEGIN { plan tests => 1 } use Math::BigFloat; my $x = new Math::BigFloat 3.12; my $y = new Math::BigFloat 3.12; ok ($x,3.12); print "$x eq 3.12" if $x eq $y; print "$x eq 3.12" if $x eq '3.12'; print "$x eq 3.12" if $x eq 3.12;
Additionally, the following still works:
print "$x == 3.12" if $x == $y; print "$x == 3.12" if $x == 3.12; print "$x == 3.12" if $x == 3.12;
- bdiv
-
The following will probably not do what you expect:
print $c->bdiv(123.456),"\n";
It prints both quotient and reminder since print works in list context. Also, bdiv() will modify $c, so be carefull. You probably want to use
print $c / 123.456,"\n"; print scalar $c->bdiv(123.456),"\n"; # or if you want to modify $c
instead.
- bpow
-
bpow()
now modifies the first argument, unlike the old code which left it alone and only returned the result. This is to be consistent withbadd()
etc. The first will modify $x, the second one won't:print bpow($x,$i),"\n"; # modify $x print $x->bpow($i),"\n"; # dito print $x ** $i,"\n"; # leave $x alone
AUTHORS
Mark Biggar, overloaded interface by Ilya Zakharevich. Completely rewritten by Tels http://bloodgate.com in 2001.