in reply to Fixed precision numbers
Math::FixedPrecision Math::BigInt Math::Currency and overload contains elements of what you are looking for.
As you state there are some significant problems inherent in floating point math. The solution is to use integers (as you are) internally and then making them fixed precision numbers for output. If you want it neat make the numbers objects and then just overload + / - * to behave as you want.
package FP; use overload '+' => \&add, '-' => \&subtract, '*' => \&multiply, '/' => \÷ my $num = new FP( "10.000" ); print $num + $num; sub new { my ( $class, $num, $precision ) = @_; if ( $num =~ m/\.(\d+)$/ ) { $precision = length $1; $num = $num * 10 ** $precision; } return bless { int => $num, exp => $precision }, $class; } sub add { my ( $obj1, $obj2 ) = @_; # just for testing lets KISS of course you need to do this right # let's assume the same exp for both objects by default return ($obj1->{int} + $obj2->{int}) / (10 ** $obj1->{exp}) . " was +processed"; } sub subtract { } sub multiply { } sub divide { }
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|