November 12, 2009
In defense of slower calculations

…or 2^64 should be enough for everyone.

Floating points

    % python
    >>> 2.5 - 0.1
    2.39999999999999

The usual answer to complaints about such behaviour is mumbling about standards, traditional numerical representations and things like that. That’s a cop out. It’s about time it would be treated as a bug.

Nowadays computers are insanely fast and have huge amounts of memory. Still, it’s surprisingly hard to tell that by number “2,5” I really mean discrete 2,5. IEEE-754 is a premature optimization, and I feel that there is a need to search for new real number representations.

There are algorithms, CPU support and long history of working with traditional, highly inexact floating points, and thus the calculations are very fast. Yet, as soon as you’d like to sacrifice some speed for the correctness, it turns out that you’re on your own. Multiplying 123456789 with 987654321 is possible, but as soon as it’s 123456789.01, you’re welcome to the land of huge errors and approximations.

Platform-specific integer representations

Even google’s new language Go is short-sighted with having fixed-size integers, forcing you to do the computer’s job of cherry-picking how much memory you’ll allocate to each of your integers. Will it be 8 bits, 16 bits, or maybe 32 bits? Oh, and then there are platform-dependant integers, which will work nicely on my 64-bit machine, but will overflow on your 32-bit, and no tests will catch that. It’s 2009 already! Just use for the integers as much you need!

Integer overflows

As if it wouldn’t be bad enough to pick some integer size and stick with it, it’s worse when it silently overflows. Ocaml, for example, with its emphasys of speed and type correctness allows you shoot in the foot:

    % ocaml
    # 9876543210 * 9876543210;;
    - : int = -3910986626405429788

I would really have expected at least throwing an exception.

Bottom line

C, C++ and their ways of doing things won’t go anywhere. But for the new languages there’s a wondeful chance to question the stagnant traditions, not sacrificing the correctness of the results for some speed.

  1. bugpipe posted this
blog comments powered by Disqus