Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^3: sprintf rounding convention

by kennethk (Abbot)
on Oct 30, 2009 at 22:24 UTC ( #804228=note: print w/ replies, xml ) Need Help??


in reply to Re^2: sprintf rounding convention
in thread sprintf rounding convention

Now I'm confused:

$ perl -v This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi $ perl -e 'printf "%.2f\n%.2f\n", 0.335, 1.335;' 0.34 1.33

and

C:\>perl -v This is perl, v5.8.9 built for MSWin32-x86-multi-thread (with 9 registered patches, see perl -V for more detail) C:\>perl -e "printf qq{%.2f\n%.2f\n}, 0.335, 1.335;" 0.34 1.34

Am I missing something (as usual)?

Update:

Following ikegami's suggestion, I tried increasing the precision to 40 digits, and got:

$ perl -v This is perl, v5.8.8 built for x86_64-linux-gnu-thread-multi $ perl -e 'printf "%.40f\n%.40f\n", 0.335, 1.335;' 0.3350000000000000199840144432528177276254 1.3349999999999999644728632119949907064438 C:\>perl -v This is perl, v5.8.9 built for MSWin32-x86-multi-thread (with 9 registered patches, see perl -V for more detail) C:\>perl -e "printf qq{%.40f\n%.40f\n}, 0.335, 1.335;" 0.3350000000000000200000000000000000000000 1.3350000000000000000000000000000000000000

It looks like this variation is just an artifact of 64-bit vs. 32-bit, emphasizing how you should never rely on precise results from rounding.


Comment on Re^3: sprintf rounding convention
Select or Download Code
Replies are listed 'Best First'.
Re^4: sprintf rounding convention
by Joost (Canon) on Oct 30, 2009 at 23:14 UTC
Re^4: sprintf rounding convention
by ikegami (Pope) on Oct 31, 2009 at 01:48 UTC

    I bet perl -e'printf "%.40e\n", 0.335' gives 0.334999... on the system that rounded to 1.33. Let me know if not.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (19)
As of 2015-07-29 17:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (266 votes), past polls