Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

When 0.99 isn't 0.99

by Thelonius (Priest)
on Sep 11, 2003 at 19:16 UTC ( [id://290788]=note: print w/replies, xml ) Need Help??


in reply to When < isn't less than

If you add these two lines:
my $x = $final_data{$key}; printf "x = %.20f high= %.20f\n", $x, $high;
you will get this output:
x = 0.98999999999999999000 high= 0.99000000000000010000 x = 0.97999999999999998000 high= 0.98000000000000009000 x = 0.96999999999999997000 high= 0.97000000000000008000 x = 0.95999999999999996000 high= 0.96000000000000008000 x = 0.94999999999999996000 high= 0.95000000000000007000 x = 0.93999999999999995000 high= 0.94000000000000006000 x = 0.93000000000000005000 high= 0.94000000000000006000 x = 0.92000000000000004000 high= 0.93000000000000005000 x = 0.91000000000000003000 high= 0.92000000000000004000 x = 0.90000000000000002000 high= 0.91000000000000003000
As you can see, the 0.99 that you get by assigning 0.99 is not the same as the 0.99 you get when you start with 0.90 and add 0.01 nine times. That's the way it is with binary floating point numbers. They're not exactly what you expect. There are several things you could do:
  1. Don't worry about it. If these are measurements, e.g., then ones that fall right on the boundaries of the bins are inherently ambiguous.
  2. Use an arbitrary-precision math package. There are ones for Perl, but I haven't used them, so I can't comment more.
  3. Scale everything so that they are all integers.

Log In?
Username:
Password:

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

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

    No recent polls found