Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: (s)printf and rounding woes

by ysth (Canon)
on Nov 04, 2003 at 23:18 UTC ( [id://304570]=note: print w/replies, xml ) Need Help??


in reply to (s)printf and rounding woes

Any integer + .005 is not going to be exactly representable in floating point. Sometimes you are getting a repesentable value above the true one and sometimes one below. Try this:
for my $num ( 0 .. 101 ) { $num += 0.005; printf "%.2f\t%.20g\n", $num, $num; }
to see the actual values you are rounding and it should make more sense. (The output starts:)
0.01 0.0050000000000000001041 1.00 1.0049999999999998934 2.00 2.0049999999999998934 3.00 3.0049999999999998934 4.00 4.0049999999999998934 5.00 5.0049999999999998934 6.00 6.0049999999999998934 7.00 7.0049999999999998934 8.01 8.0050000000000007816 9.01 9.0050000000000007816 10.01 10.005000000000000782 11.01 11.005000000000000782 12.01 12.005000000000000782 13.01 13.005000000000000782 14.01 14.005000000000000782 15.01 15.005000000000000782 16.00 16.004999999999999005

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-04-25 11:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found