Ocaml stubbed my toe

In ocaml, the expression 0.0 == 0.0 +. 0.0 is false. Gah! That is so stupid. I miss haskell.

Posted on September 17, 2006 07:29 PM
More programming articles

Comments

I'm still trying to wrap my brain around Lisp and the fact that (eq 2 2) may be false.

Posted by: Sean Conner at September 17, 2006 10:05 PM

And this is by design, not a bug?

Posted by: Sjoerd Visscher at September 18, 2006 05:45 AM

Apparently yes, it is by design. == is used for pointer equality while = is used for structural equality. I'm guessing that since floats are 8 bytes, they can't be stored as a primitive value. Therefore == compares pointers instead of values. But I'm probably wrong about that.

Posted by: Kim at September 18, 2006 09:59 AM

This actually makes a lot of sense. If you're comparing values, 0.0 = 0.0 +. 0.0 is true, whereas == simply checks to see if each value references the same object.

Posted by: Eric Radman at October 3, 2006 07:12 PM

Eric, I disagree. If you're adding ints, then == works fine. It's only if you're adding doubles that it breaks. In my world, any language that treats ints as non-reference-types also treats doubles as non-reference types. Therefore this is confusing and unexpected.

Posted by: Kim at October 3, 2006 07:21 PM

You should be using = instead of ==. Only use == when you know exactly what it does, e.g. when you're writing code to traverse cyclic data structures.

Posted by: Jon Harrop at November 12, 2006 01:46 AM
Post a comment









Remember info?




Prove you're human. Type "human":