

o  /^[+]\d+$/ 
o  /^[+]\d+\.\d*$/ 
o  /^[+]\d+E[+]?\d+$/ 
o  /^[+]\d*\.\d+E[+]?\d+$/ 
Empty strings as well as other illegal numbers results in ’NaN’.
bnorm() on a BigFloat object is now effectively a noop, since the numbers are always stored in normalized form. On a string, it creates a BigFloat object.
Output values are BigFloat objects (normalized), except for bstr() and bsstr().The string output will always have leading and trailing zeros stripped and drop a plus sign. bstr() will give you always the form with a decimal point, while bsstr() (s for scientific) gives you the scientific notation.
Input bstr() bsstr() 0 0 0E1 123 123 123 123123123 123123123E0 00.0123 0.0123 123E4 123.45E2 1.2345 12345E4 10E+3 10000 1E4Some routines (is_odd(), is_even(), is_zero(), is_one(), is_nan()) return true or false, while others (bcmp(), bacmp()) return either undef, <0, 0 or >0 and are suited for sort.
Actual math is done by using the class defined with with => Class; (which defaults to 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, and ’inf’ and ’inf’ are used to represent positive and negative infinity, respectively.
mantissa(), exponent() and parts()
mantissa() and exponent() return the said parts of the BigFloat as BigInts such that:
$m = $x>mantissa(); $e = $x>exponent(); $y = $m * ( 10 ** $e ); print "ok\n" if $x == $y;($m,$e) = $x>parts(); is just a shortcut giving you both of them.
Currently the mantissa is reduced as much as possible, favouring higher exponents over lower ones (e.g. returning 1e7 instead of 10e6 or 10000000e0). This might change in the future, so do not depend on it.
See also: Rounding.Math::BigFloat supports both precision (rounding to a certain place before or after the dot) and accuracy (rounding to a certain number of digits). For a full documentation, examples and tips on these topics please see the large section about rounding in Math::BigInt.
Since things like sqrt(2) or 1 / 3 must presented with a limited accuracy lest a operation consumes all resources, each operation produces no more than the requested number of digits.
If there is no global precision or accuracy set, <B>andB> the operation in question was not called with a requested precision or accuracy, <B>andB> the input $x has no accuracy or precision set, then a fallback parameter will be used. For historical reasons, it is called div_scale and can be accessed via:
$d = Math::BigFloat>div_scale(); # query Math::BigFloat>div_scale($n); # set to $n digitsThe default value for div_scale is 40.
In case the result of one operation has more digits than specified, it is rounded. The rounding mode taken is either the default mode, or the one supplied to the operation after the scale:
$x = Math::BigFloat>new(2); Math::BigFloat>accuracy(5); # 5 digits max $y = $x>copy()>bdiv(3); # will give 0.66667 $y = $x>copy()>bdiv(3,6); # will give 0.666667 $y = $x>copy()>bdiv(3,6,undef,odd); # will give 0.666667 Math::BigFloat>round_mode(zero); $y = $x>copy()>bdiv(3,6); # will also give 0.666667Note that Math::BigFloat>accuracy() and Math::BigFloat>precision() set the global variables, and thus <B>anyB> newly created number will be subject to the global rounding <B>immediatelyB>. This means that in the examples above, the 3 as argument to bdiv() will also get an accuracy of <B>5B>.
It is less confusing to either calculate the result fully, and afterwards round it explicitly, or use the additional parameters to the math functions like so:
use Math::BigFloat; $x = Math::BigFloat>new(2); $y = $x>copy()>bdiv(3); print $y>bround(5),"\n"; # will give 0.66667 or use Math::BigFloat; $x = Math::BigFloat>new(2); $y = $x>copy()>bdiv(3,5); # will give 0.66667 print "$y\n";
All rounding functions take as a second parameter a rounding mode from one of the following: ’even’, ’odd’, ’+inf’, ’inf’, ’zero’, ’trunc’ or ’common’.
bfround ( +$scale ) Rounds to the $scale’th place left from the ’.’, counting from the dot. The first digit is numbered 1. bfround ( $scale ) Rounds to the $scale’th place right from the ’.’, counting from the dot. bfround ( 0 ) Rounds to an integer. bround ( +$scale ) Preserves accuracy to $scale digits from the left (aka significant digits) and pads the rest with zeros. If the number is between 1 and 1, the significant digits count from the first nonzero after the ’.’ bround ( $scale ) and bround ( 0 ) These are effectively noops. The default rounding mode is ’even’. By using Math::BigFloat>round_mode($round_mode); you can get and set the default mode for subsequent rounding. The usage of $Math::BigFloat::$round_mode is no longer supported. The second parameter to the round functions then overrides the default temporarily.
The as_number() function returns a BigInt from a Math::BigFloat. It uses ’trunc’ as rounding mode to make it equivalent to:
$x = 2.5; $y = int($x) + 2;You can override this by passing the desired rounding mode as parameter to as_number():
$x = Math::BigFloat>new(2.5); $y = $x>as_number(odd); # $y = 3
Math::BigFloat supports all methods that Math::BigInt supports, except it calculates noninteger results when possible. Please see Math::BigInt for a full description of each method. Below are just the most important differences:
accuracy() $x>accuracy(5); # local for $x CLASS>accuracy(5); # global for all members of CLASS # Note: This also applies to new()! $A = $x>accuracy(); # read out accuracy that affects $x $A = CLASS>accuracy(); # read out global accuracySet or get the global or local accuracy, aka how many significant digits the results have. If you set a global accuracy, then this also applies to new()!
Warning! The accuracy sticks, e.g. once you created a number under the influence of CLASS>accuracy($A), all results from math operations with that number will also be rounded.
In most cases, you should probably round the results explicitly using one of round() in Math::BigInt, bround() in Math::BigInt or bfround() in Math::BigInt or by passing the desired accuracy to the math operation as additional parameter:
my $x = Math::BigInt>new(30000); my $y = Math::BigInt>new(7); print scalar $x>copy()>bdiv($y, 2); # print 4300 print scalar $x>copy()>bdiv($y)>bround(2); # print 4300precision() $x>precision(2); # local for $x, round at the second # digit right of the dot $x>precision(2); # ditto, round at the second digit # left of the dot CLASS>precision(5); # Global for all members of CLASS # This also applies to new()! CLASS>precision(5); # ditto $P = CLASS>precision(); # read out global precision $P = $x>precision(); # read out precision that affects $xNote: You probably want to use accuracy() instead. With accuracy() you set the number of digits each result should have, with precision() you set the place where to round!
bdiv() $q = $x>bdiv($y); ($q, $r) = $x>bdiv($y);In scalar context, divides $x by $y and returns the result to the given or default accuracy/precision. In list context, does floored division (Fdivision), returning an integer $q and a remainder $r so that $x = $q * $y + $r. The remainer (modulo) is equal to what is returned by $xbmod($y)>.
bmod() $x>bmod($y);Returns $x modulo $y. When $x is finite, and $y is finite and nonzero, the result is identical to the remainder after floored division (Fdivision). If, in addition, both $x and $y are integers, the result is identical to the result from Perl’s % operator.
bexp() $x>bexp($accuracy); # calculate e ** XCalculates the expression e ** $x where e is Euler’s number.
This method was added in v1.82 of Math::BigInt (April 2007).
bnok() $x>bnok($y); # x over y (binomial coefficient n over k)Calculates the binomial coefficient n over k, also called the choose function. The result is equivalent to:
( n ) n!    =  ( k ) k!(nk)!This method was added in v1.84 of Math::BigInt (April 2007).
bpi() print Math::BigFloat>bpi(100), "\n";Calculate PI to N digits (including the 3 before the dot). The result is rounded according to the current rounding mode, which defaults to even.
This method was added in v1.87 of Math::BigInt (June 2007).
bcos() my $x = Math::BigFloat>new(1); print $x>bcos(100), "\n";Calculate the cosinus of $x, modifying $x in place.
This method was added in v1.87 of Math::BigInt (June 2007).
bsin() my $x = Math::BigFloat>new(1); print $x>bsin(100), "\n";Calculate the sinus of $x, modifying $x in place.
This method was added in v1.87 of Math::BigInt (June 2007).
batan2() my $y = Math::BigFloat>new(2); my $x = Math::BigFloat>new(3); print $y>batan2($x), "\n";Calculate the arcus tanges of $y divided by $x, modifying $y in place. See also batan().
This method was added in v1.87 of Math::BigInt (June 2007).
batan() my $x = Math::BigFloat>new(1); print $x>batan(100), "\n";Calculate the arcus tanges of $x, modifying $x in place. See also batan2().
This method was added in v1.87 of Math::BigInt (June 2007).
bmuladd() $x>bmuladd($y,$z);Multiply $x by $y, and then add $z to the result.
This method was added in v1.87 of Math::BigInt (June 2007).
as_float() This method is called when Math::BigFloat encounters an object it doesn’t know how to handle. For instance, assume $x is a Math::BigFloat, or subclass thereof, and $y is defined, but not a Math::BigFloat, or subclass thereof. If you do
$x > badd($y);$y needs to be converted into an object that $x can deal with. This is done by first checking if $y is something that $x might be upgraded to. If that is the case, no further attempts are made. The next is to see if $y supports the method as_float(). The method as_float() is expected to return either an object that has the same class as $x, a subclass thereof, or a string that ref($x)>new() can parse to create an object.
In Math::BigFloat, as_float() has the same effect as copy().
from_hex() $x > from_hex("0x1.921fb54442d18p+1"); $x = Math::BigFloat > from_hex("0x1.921fb54442d18p+1");Interpret input as a hexadecimal string.A prefix (0x, x, ignoring case) is optional. A single underscore character (_) may be placed between any two digits. If the input is invalid, a NaN is returned. The exponent is in base 2 using decimal digits.
If called as an instance method, the value is assigned to the invocand.
from_bin() $x > from_bin("0b1.1001p4"); $x = Math::BigFloat > from_bin("0b1.1001p4");Interpret input as a hexadecimal string. A prefix (0b or b, ignoring case) is optional. A single underscore character (_) may be placed between any two digits. If the input is invalid, a NaN is returned. The exponent is in base 2 using decimal digits.
If called as an instance method, the value is assigned to the invocand.
from_oct() $x > from_oct("1.3267p4"); $x = Math::BigFloat > from_oct("1.3267p4");Interpret input as an octal string. A single underscore character (_) may be placed between any two digits. If the input is invalid, a NaN is returned. The exponent is in base 2 using decimal digits.
If called as an instance method, the value is assigned to the invocand.
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 2E100,"\n"prints the value of 2E100. Note that without conversion of constants the expression 2E100 will be calculated as normal floating point number.
Please note that ’:constant’ does not affect integer constants, nor binary nor hexadecimal constants. Use bignum or Math::BigInt to get this to work.
Math with the numbers is done (by default) by a module called Math::BigInt::Calc. This is equivalent to saying:
use Math::BigFloat lib => Calc;You can change this by using:
use Math::BigFloat lib => GMP;<B>NoteB>: General purpose packages should not be explicit about the library to use; let the script author decide which is best.
Note: The keyword ’lib’ will warn when the requested library could not be loaded. To suppress the warning use ’try’ instead:
use Math::BigFloat try => GMP;If your script works with huge numbers and Calc is too slow for them, you can also for the loading of one of these libraries and if none of them can be used, the code will die:
use Math::BigFloat only => GMP,Pari;The following would first try to find Math::BigInt::Foo, then Math::BigInt::Bar, and when this also fails, revert to Math::BigInt::Calc:
use Math::BigFloat lib => Foo,Math::BigInt::Bar;See the respective lowlevel library documentation for further details.
Please note that Math::BigFloat does <B>notB> use the denoted library itself, but it merely passes the lib argument to Math::BigInt. So, instead of the need to do:
use Math::BigInt lib => GMP; use Math::BigFloat;you can roll it all into one line:
use Math::BigFloat lib => GMP;It is also possible to just require Math::BigFloat:
require Math::BigFloat;This will load the necessary things (like BigInt) when they are needed, and automatically.
See Math::BigInt for more details than you ever wanted to know about using a different lowlevel library.
For backwards compatibility reasons it is still possible to request a different storage class for use with Math::BigFloat:
use Math::BigFloat with => Math::BigInt::Lite;However, this request is ignored, as the current code now uses the lowlevel math library for directly storing the number parts.
Math::BigFloat exports nothing by default, but can export the bpi() method:
use Math::BigFloat qw/bpi/; print bpi(10), "\n";
Do not try to be clever to insert some operations in between switching libraries:
require Math::BigFloat; my $matter = Math::BigFloat>bone() + 4; # load BigInt and Calc Math::BigFloat>import( lib => Pari ); # load Pari, too my $anti_matter = Math::BigFloat>bone()+4; # now use PariThis will create objects with numbers stored in two different backend libraries, and <B>VERY BAD THINGSB> will happen when you use these together:
my $flash_and_bang = $matter + $anti_matter; # Dont do this!
stringify, bstr() Both stringify and bstr() now drop the leading ’+’. The old code would return ’+1.23’, the new returns ’1.23’. See the documentation in Math::BigInt for reasoning and details. bdiv() The following will probably not print what you expect:
print $c>bdiv(123.456),"\n";It prints both quotient and remainder since print works in list context. Also, bdiv() will modify $c, so be careful. You probably want to use
print $c / 123.456,"\n"; # or if you want to modify $c: print scalar $c>bdiv(123.456),"\n";instead.
brsft() The following will probably not print what you expect:
my $c = Math::BigFloat>new(3.14159); print $c>brsft(3,10),"\n"; # prints 0.00314153.1415It prints both quotient and remainder, since print calls brsft() in list context. Also, $c>brsft() will modify $c, so be careful. You probably want to use
print scalar $c>copy()>brsft(3,10),"\n"; # or if you really want to modify $c print scalar $c>brsft(3,10),"\n";instead.
Modifying and = Beware of:
$x = Math::BigFloat>new(5); $y = $x;It will not do what you think, e.g. making a copy of $x. Instead it just makes a second reference to the <B>sameB> object and stores it in $y. Thus anything that modifies $x will modify $y (except overloaded math operators), and vice versa. See Math::BigInt for details and how to avoid that.
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 with badd() etc. The first will modify $x, the second one won’t:
print bpow($x,$i),"\n"; # modify $x print $x>bpow($i),"\n"; # ditto print $x ** $i,"\n"; # leave $x aloneprecision() vs. accuracy() A common pitfall is to use precision() when you want to round a result to a certain number of digits:
use Math::BigFloat; Math::BigFloat>precision(4); # does not do what you # think it does my $x = Math::BigFloat>new(12345); # rounds $x to "12000"! print "$x\n"; # print "12000" my $y = Math::BigFloat>new(3); # rounds $y to "0"! print "$y\n"; # print "0" $z = $x / $y; # 12000 / 0 => NaN! print "$z\n"; print $z>precision(),"\n"; # 4Replacing precision() with accuracy() is probably not what you want, either:
use Math::BigFloat; Math::BigFloat>accuracy(4); # enables global rounding: my $x = Math::BigFloat>new(123456); # rounded immediately # to "12350" print "$x\n"; # print "123500" my $y = Math::BigFloat>new(3); # rounded to "3 print "$y\n"; # print "3" print $z = $x>copy()>bdiv($y),"\n"; # 41170 print $z>accuracy(),"\n"; # 4What you want to use instead is:
use Math::BigFloat; my $x = Math::BigFloat>new(123456); # no rounding print "$x\n"; # print "123456" my $y = Math::BigFloat>new(3); # no rounding print "$y\n"; # print "3" print $z = $x>copy()>bdiv($y,4),"\n"; # 41150 print $z>accuracy(),"\n"; # undefIn addition to computing what you expected, the last example also does <B>notB> taint the result with an accuracy or precision setting, which would influence any further operation.
Please report any bugs or feature requests to bugmathbigint at rt.cpan.org, or through the web interface at <https://rt.cpan.org/Ticket/Create.html?Queue=MathBigInt> (requires login). We will be notified, and then you’ll automatically be notified of progress on your bug as I make changes.
You can find documentation for this module with the perldoc command.
perldoc Math::BigFloatYou can also look for information at:
o RT: CPAN’s request tracker <https://rt.cpan.org/Public/Dist/Display.html?Name=MathBigInt>
o AnnoCPAN: Annotated CPAN documentation o CPAN Ratings o Search CPAN o CPAN Testers Matrix <http://matrix.cpantesters.org/?dist=MathBigInt>
o The Bignum mailing list
o Post to mailing list bignum at lists.scsys.co.uk
o View mailing list o Subscribe/Unsubscribe
This program is free software; you may redistribute it and/or modify it under the same terms as Perl itself.
Math::BigFloat and Math::BigInt as well as the backends Math::BigInt::FastCalc, Math::BigInt::GMP, and Math::BigInt::Pari.The pragmas bignum, bigint and bigrat also might be of interest because they solve the autoupgrading/downgrading issue, at least partly.
o Mark Biggar, overloaded interface by Ilya Zakharevich, 19962001. o Completely rewritten by Tels <http://bloodgate.com> in 20012008. o Florian Ragwitz flora@cpan.org, 2010. o Peter John Acklam, pjacklam@online.no, 2011.
perl v5.20.3  MATH::BIGFLOAT (3)  20160105 
Visit the GSP FreeBSD Man Page Interface.
Output converted with manServer 1.07.