Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: My floating point comparison does not work. Why ?

by PhilHibbs (Hermit)
on Sep 23, 2003 at 14:16 UTC ( [id://293501]=note: print w/replies, xml ) Need Help??


in reply to My floating point comparison does not work. Why ?

The problem with comparing two floats with a tolerance is that the comparison is not transitive. That is, even though a==b and b==c, it may be that a!=c.

This might become important in such situations as passing a sorting or searching function. I am more familiar with this situation in C++ and Java. In Java, there is such a thing as the "equals contract", which states that the equals function must be reflexive (a==a), symmetric (a==b, b==a) and transitive (a==b, b==c, a==c). This is important for building HashSet and HashMap objects, and there are similar rules in C++ for the STL containers and algorithms.

Being something of a perl noob, I don't know if there are similar constraints in Perl libraries, but it's a good rule to bear in mind anyway.

If a comparison function is not transitive, you are just pushing the surprising behaviour into a darker corner.

  • Comment on Re: My floating point comparison does not work. Why ?

Log In?
Username:
Password:

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

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

    No recent polls found