[Detroit-pm] Strange problem with simple math in perl

Sean Millichamp sean at bruenor.org
Thu Nov 12 07:41:56 PST 2009


I found that the best and safest way to handle money-related math is to
convert and store everything as cents, then you are doing integer math
which will be right every time.

I know that doesn't directly answer your question, but I did some coding
a while back in Perl that manipulated and tracked money amounts and I
never found a perfect and predictable solution with floating point
numbers.

Hope that helps.

Sean

On Thu, 2009-11-12 at 10:34 -0500, Jim McQuillan wrote:
> Hey guys,
> 
> I'm having an issue with doing simple arithmetic in perl.
> 
> I suspect it's a floating-point issue.
> 
> here's the code:
> 
> ==================================================
> 
> #!/usr/bin/perl -w
> 
> use strict;
> 
> my $new_bal = 123.00
>              - 69.63
>              - 53.37;
> 
> print("new_bal: $new_bal\n");
> 
> ==================================================
> 
> The result I get is:
> 
>     new_bal: 7.105427357601e-15
> 
> That's awfully close to the right answer, but I'm depending on perl to 
> be useful for calculating account balances.
> 
> Any ideas?
> 
> Thanks,
> Jim McQuillan
> jam at Avairis.com
> _______________________________________________
> Detroit-pm mailing list
> Detroit-pm at pm.org
> http://mail.pm.org/mailman/listinfo/detroit-pm
> 



More information about the Detroit-pm mailing list