Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: simple arithmetic creates rounding errors

by reasonablekeith (Deacon)
on Jan 13, 2006 at 10:14 UTC ( [id://522941]=note: print w/replies, xml ) Need Help??


in reply to simple arithmetic creates rounding errors

I don't think you can get rid of them, you just have to manage it. If you swap in the following line you'll get the results as you expect.
my $frac = sprintf("%.9g", ($num - $int) );
This is simply rounding to an arbitrary 9 decimal points. Obviously you need to look at the formulas you're working with but this should serve in a lot of cases.

UPDATE: I didn't notice you actually gave a sprintf example. So, Yes, that is the easiest way to get rid of this problem. Infact the O'Reilly Perl CookBook gives examples just like this.
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^2: simple arithmetic creates rounding errors
by Anonymous Monk on Jan 13, 2006 at 11:33 UTC
    Thanks reasonablekeith

    The "%.9g" fixed it. Unlike "%.3f" which produces .000 onto everything.

    Excellent!!!!

    I must look better through the cookbook next time :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://522941]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (7)
As of 2024-04-23 10:07 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found