[Detroit-pm] Strange problem with simple math in perl
Steve Ferguson
sfergus1 at gmail.com
Thu Nov 12 07:41:55 PST 2009
If you know you'll always be dealing in pennies (and no fractions
thereof), I'd suggest you treat that as your unit (e.g. multiply the
dollar amounts by 100) and stick to integer math. Alternately you could
round all of your calculations, but that may or may not suit your need
for accuracy.
Steve
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
--
Steve Ferguson <sfergus1 at gmail.com>
The effort of using machines to mimic the human mind has always struck
me as rather silly: I'd rather use them to mimic something better.
-- E. W. Dijkstra
More information about the Detroit-pm
mailing list