Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Printf/Sprintf Behavior Change

by kennethk (Monsignor)
on Sep 16, 2013 at 16:38 UTC ( #1054315=note: print w/ replies, xml ) Need Help??


in reply to Printf/Sprintf Behavior Change

What Every Computer Scientist Should Know About Floating-Point Arithmetic

In this case, the code

printf "%0.018f\n", 0.345; printf "%0.018f\n", (3.49 / 2) - 1.4;
outputs (on my computer, it's platform dependent)
0.344999999999999970 0.345000000000000200

You are getting different numbers because you are dealing with different numbers. If you need absolute precision, there are ways to handle it, but they are generally not worth the effort it takes to implement them.


#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.


Comment on Re: Printf/Sprintf Behavior Change
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2014-09-20 01:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls