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

Re^2: When is a 2 not a 2? (eq)

by halley (Prior)
on Jan 31, 2008 at 13:43 UTC ( #665343=note: print w/ replies, xml ) Need Help??


in reply to Re: When is a 2 not a 2? (eq)
in thread When is a 2 not a 2?

tye, you may already know of this, but I'll reiterate that anyone who is interested in implementing a "comparing floating point numbers" feature should read this article. http://www.cygnus-software.com/papers/comparingfloats/comparingfloats.htm

In the olden days, people would compare using a chosen EPSILON. if (abs($a - $b) < $EPSILON) { ... } If you know about the uneven resolution of floats, you learn that EPSILON must be chosen carefully for each comparison. Better to know something about the format of IEEE floats (the most common implementation on modern computers) and some fast and flexible ways to make a suitable AlmostEqual() function that lets you use a tolerance that is tied to the resolution, not the decimal position of the error.

Comparing floats is a huge gotcha for newcomers or writers of quick ad-hoc code, and easy to do wrong. I would rather that high-level programming languages melt such comparison features into the language, say, with an A =~ B floating point operator. (Whether regex or float, that can be read as "does A bind with B.") Then, the default tolerance can be set to a suitable "about one decimal place" and overridden through a pragma or language variable. But I digress.

--
[ e d @ h a l l e y . c c ]


Comment on Re^2: When is a 2 not a 2? (eq)
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (7)
As of 2014-12-25 16:08 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls