Floating point arithmetics in 19 programming languages

Update: It seems that there has been some confusions about the results. I’m reminding that you can click on each programming language name to edit the source and see the running results for yourself! But since there is only two type of results - false & true until Pascal and false & false for others, then I wouldn’t bother much.

Update #2: There has been mentioned in the comments one very useful and good article by Bruce Dawson called “Comparing floating point numbers”. Thank you for the suggestion!

I got an e-mail from a co-worker about one spec which should have not been failing. At least he thought so. The problem was simple - he was extracing two floating point values from the web page and added them together and expected to see that calculated value on the web page also. The value appeared to be correct, but the test failed nevertheless. I’m pretty sure that most of you know the problem already - it is just a “minor” problem of converting floating-point numbers from base 10 to base 2 and back. You can read more about the problem at here. After Ruby got flamed by not being adequate enough to deal with these problems in the background, i started to look how some of the other languages behave hoping that there is some programming language which works as we are expecting it to. For this reason I found ideone being extremely helpful. I took two “magic” floating point numbers like 1.2 and 1.0 and made two calculations with them:

  • 1.2 - 1.0 and checked that it equals to 0.2 exactly (well, duh).
  • 1.2 * 10 - 1.0 * 10 and checked that it equals to 0.2 * 10 exactly for trying to rule out syntax errors hopefully.

The results with some comments will follow plus you can click on the language name to see the actual results on ideone and change the source code to play around. Also, please be aware of the fact that I haven’t written single line of code in some of the languages below thus creating the possibility that my solution might look a bit weird.

Ruby

Okay, this is the language which all started the flamewar and here is the code which shows the problem:

So, what do you think is the output of this code? Of course it would be great if the output would be “true” and “true”. Sadly, it’s “false” and “true”. Let’s see how other languages behave, shall we?

Java

This is of course the language of the mainstream. At least according to the TIOBE index. This language probably doesn’t have this flaw because otherwise all big companies would burn to the ashes due to the cataclysmic event caused by the multi-million-dollar-software-project, right? Code looks like this:

Oh, and the result? False & true - damn. I guess that we’re all doomed.

PHP

Here’s another popular language. Hopefully it “performs” better.

And the result? Same as all above…

JavaScript

Well, it’s JavaScript.

Results are still the same…

Python

What about snakes…

Nope. Still not good.

Perl

I guess it won’t survive either.

Yup.

C

Let’s take a look at C family.

I guess not.

C++

Another C-family member representative.

Oh gosh - 0 and 1 is the output of this.

C#

And yet another C-family “member”.

Still not good…

Smalltalk

Okay, let’s see what this older language thinks about the problem.

Doesn’t seem to be thinking much of it either.

Common Lisp

What about Lisp-family-languages?

Woops… NIL and T.

Clojure

What about this one?

Nope :(

Scheme

And this?

At least the output is different - #f and #t :)

Erlang

Uhoh, maybe we can calculate correctly by doing it concurrently?

Still no luck.

Prolog

Okay, let’s try logical programming language - i mean, this calculation seems to be quite logical to me.

I guess it’s not logical enough.

Scala

What about some newer languages?

Damn!

Pascal

Let’s see what this does.

False and false?! Any ideas, how come?

Haskell

Okay, this is a so called language for academics and mathematicians. They ought to make calculations correctly…

Same as Pascal? Holy ****? This was a total surprise for me.

Go

So, what about this new programming language from year 2009 by Google? Maybe there has been some thinking regarding solving this problem…

Again, same as 2 above.

Working example in Ruby

Okay, i’m going to also show how to perform these calculations correctly in Ruby. You just have to use more precise type like BigDecimal instead of Float in Ruby:

And the results? True and true of course!

“Fix” for Ruby

But what if i don’t want to spend my time thinking about these problems? Well, in Ruby and many other languages above you can modify (built-in) classes/methods at run-time, except Java and few others (Javassist and similar tools doesn’t count). We just need to overwrite methods for addition, subtraction, division and multiplication in Float class. Now we can perform calculations without having to worry about the problems of floating-point arithmetics. Here it is - a simple solution to our everyday problems:

Conclusion

So, what to think about the results? Honestly - i don’t know. I mean, if all the languages behave similarly then maybe it’s OK? On the other hand it can’t be OK if someone spends hours of debugging the code because of this problem. But why isn’t there any language which deals with this problem “correctly”? Or maybe i just wasn’t lucky enough to stumble upon this language? Is there maybe a performance or memory usage in mind?

This is it. Do you know some language which handles this situation “automagically”? Feel free to tell me and everyone else about it in the comments!