Hacker News new | comments | show | ask | jobs | submit login

As many others have pointed out, this is not really a matter of language but of the data types used. In IEEE 754 floating point arithmetic, the two expressions are not equal.

You're looking for a different data type, and many choices would give you the desired equality: a decimal floating point representation, calculating with rational numbers and interval arithmetic come to mind (technically, IEEE floating point is a kind of interval arithmetic, but you have not much control over the size of the intervals used and comparison does not do what you might naively expect, i.e., checking whether the two intervals intersect).

I suppose you should look for a library like GMP or MPIR for whatever is your preferred language. Most computer algebra systems (e.g., Sage) will also provide with what you need in some way or another.

But the APL family (APL, J, K) does get this right despite using IEEE 754 floating point - it has comparison tolerance built in.

And you would probably look at what it does with disgust, given that it breaks transitivity of equality (that is, there are numbers such that a==b and b==c but a<>c). However, in practice it does work very well. You can still run into weird corners, but e.g.

always holds true, unlike with C comparison semantics.

This seems to be a very sensible default way to handle the comparison, assuming the programmer already knows about the problems with floating point math. A programmer who does not know about that will probably trip up sooner or later no matter how the language implements comparison.

So personally I would not see it as "right" (or "wrong"), simply because it is a choice between many different methods with their own advantages and disadvantages. I view this not as a problem a language can (or has to) solve, but as one a programmer has to be aware of and has to solve depending on his particular application.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact