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

Jim McQuillan jam at McQuil.com
Thu Nov 12 14:18:01 PST 2009


Thanks to everyone that replied about the issues with floating point 
arithmetic.  I pretty much figured that's where the problem was, and I 
know I can play games with "%.2f" to get the number of digits after the 
decimal point the way I want it.

If all i was doing was outputting the result, that'd be fine.  But, I 
also need to do things like test the balance for zero:

    if( $new_bal == 0 ){
       ...
    }

of course, that falls apart when the number is 0.0000000000000710542... 
  cuz that's NOT zero.

I know it's not perl's fault.  In fact, I tried the same thing in python:

   #!/usr/bin/python

   new_bal = 123.00 - 69.63 - 53.37;

   print new_bal;

Not surprisingly, I get the same result as the perl example I showed 
earlier.

I used to do alot of C programming and back then, we all just knew about 
the issues with floating point and avoided it for business applications.

I just assumed that the world of programming languages had grown past 
that by now.

I've been doing perl programming for many years and not been bitten (at 
least not that I knew of) until today.

What's really funny, is Duane (Also works here at Avairis) had a very 
similar problem.  He did a select on a database table that resulted in a 
value of 33.05.  he tried to convert that to pennies and got 3304.
when we displayed the original number as floating point, with '%.15f', 
it came out '33.049999999997'.  Multiply that by 100 and turn it into an 
integer, and you get 3304.

I'm thinking of having the database do the conversion to pennies by 
doing something like this:

     SELECT (bal * 100)::int AS chg_bal_cents
       FROM chg

It's a postgresql database and the 'bal' column is defined as 
'numeric(10,2)'

This way, my perl code only ever has to deal with integers.

In the one case where I tried it today, it gave the results I needed, 
but I'm not ready to just jump into that for everything yet.  I figured 
I'd throw that idea out to you guys for feedback.

Thanks again for the awesome quick responses.

Jim McQuillan
jam at Avairis.com





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