You are viewing the version of this documentation from Perl 5.6.1. View the latest version



Math::BigFloat - Arbitrary length float math package


use Math::BigFloat;
$f = Math::BigFloat->new($string);

$f->fadd(NSTR) return NSTR            addition
$f->fsub(NSTR) return NSTR            subtraction
$f->fmul(NSTR) return NSTR            multiplication
$f->fdiv(NSTR[,SCALE]) returns NSTR   division to SCALE places
$f->fmod(NSTR) returns NSTR           modular remainder
$f->fneg() return NSTR                negation
$f->fabs() return NSTR                absolute value
$f->fcmp(NSTR) return CODE            compare undef,<0,=0,>0
$f->fround(SCALE) return NSTR         round to SCALE digits
$f->ffround(SCALE) return NSTR        round at SCALEth place
$f->fnorm() return (NSTR)             normalize
$f->fsqrt([SCALE]) return NSTR        sqrt to SCALE places


All basic math operations are overloaded if you declare your big floats as

$float = new Math::BigFloat "2.123123123123123123123123123123123";
number format

canonical strings have the form /[+-]\d+E[+-]\d+/ . Input values can have embedded whitespace.

Error returns 'NaN'

An input parameter was "Not a Number" or divide by zero or sqrt of negative number.

Division is computed to

max($Math::BigFloat::div_scale,length(dividend)+length(divisor)) digits by default. Also used for default sqrt scale.

Rounding is performed

according to the value of $Math::BigFloat::rnd_mode:

trunc     truncate the value
zero      round towards 0
+inf      round towards +infinity (round up)
-inf      round towards -infinity (round down)
even      round to the nearest, .5 to the even digit
odd       round to the nearest, .5 to the odd digit

The default is even rounding.


The current version of this module is a preliminary version of the real thing that is currently (as of perl5.002) under development.

The printf subroutine does not use the value of $Math::BigFloat::rnd_mode when rounding values for printing. Consequently, the way to print rounded values is to specify the number of digits both as an argument to ffround and in the %f printf string, as follows:

printf "%.3f\n", $bigfloat->ffround(-3);


Mark Biggar Patches by John Peacock Apr 2001