NAME

Math::FixedPrecision - Decimal Math without Floating Point Errors

SYNOPSIS

use Math::FixedPrecision; $height = Math::FixedPrecision->new(12.362); # 3 decimal places $width = Math::FixedPrecision->new(9.65); # 2 decimal places $area = $height * $width; # area is now 119.29 not 119.2933 $length = Math::FixedPrecision->new("100.00"); # 2 decimal places $section = $length / 9; # section is now 11.11 not 11.1111111...

DESCRIPTION

There are numerous instances where floating point math is unsuitable, yet the data does not consist solely of integers. This module is designed to completely overload all standard math functions. The module takes care of all conversion and rounding automatically. Rounding is handled using the IEEE 754 standard even mode. This is a complete rewrite to use Math::BigFloat, rather than Math::BigInt to handle the underlying math operations.

This module is not a replacement for Math::BigFloat; rather it serves a similar but slightly different purpose. By strictly limiting precision automatically, this module operates slightly more natually than Math::BigFloat when dealing with floating point numbers of limited accuracy. Math::BigFloat can unintentially inflate the apparent accuracy of a calculation.

Please examine assumptions you are operating under before deciding between this module and Math::BigFloat. With this module the assumption is that your data is not very accurate and you do not want to overstate any resulting values; with Math::BigFloat, you can completely avoid the rounding problems associated with floating point notation.

new(number[,precision])

The constructor accepts either a number or a string that looks like a number. But if you want to enforce a specific precision, you either need to pass an exact string or include the second term. In other words, all of the following variables have different precisions:

$var1 = Math::FixedPrecision->new(10);
        # 10 to infinite decimals
$var2 = Math::FixedPrecision->new(10,2);
        # 10.00 to 2 decimals
$var3 = Math::FixedPrecision->new("10.000");
        # 10.000 to 3 decimals

All calculations will return a value rounded to the level of precision of the least precise datum. A number which looks like an integer (like $var1 above) has infinite precision (no decimal places). This is important to note since Perl will happily truncate all trailing zeros from a number like 10.000 and the code will get 10 no matter how many zeros you typed. If you need to assert a specific precision, you need to either explicitly state that like $var2 above, or quote the number like $var3. For example:

$var4 = $var3 * 2; # 20.000 to 3 decimals
$var5 = Math::FixedPrecision->new("2.00");
        # 2.00 to 2 decimals
$var6 = $var3 * $var 5;
        # 20.00 to 2 decimals, not 3

EXPORT None by default.

AUTHOR

John Peacock <jpeacock@rowman.com>

SEE ALSO

Math::BigFloat