Slashdot Mirror


What Every Programmer Should Know About Floating-Point Arithmetic

-brazil- writes "Every programmer forum gets a steady stream of novice questions about numbers not 'adding up.' Apart from repetitive explanations, SOP is to link to a paper by David Goldberg which, while very thorough, is not very accessible for novices. To alleviate this, I wrote The Floating-Point Guide, as a floating-point equivalent to Joel Spolsky's excellent introduction to Unicode. In doing so, I learned quite a few things about the intricacies of the IEEE 754 standard, and just how difficult it is to compare floating-point numbers using an epsilon. If you find any errors or omissions, you can suggest corrections."

5 of 359 comments (clear)

  1. Interval arithmetic by SolusSD · · Score: 4, Insightful

    Floating point math should be properly verified using interval arithmetic: http://en.wikipedia.org/wiki/Interval_arithmetic

    1. Re:Interval arithmetic by harshaw · · Score: 4, Insightful

      Gah. Yet another unintelligible wikipedia mathematics article. For once I did like to see an article that does a great job *teaching* about a subject. Perhaps wikipedia isn't the right home for this sort of content, but my general feeling whenever reading something is wikipedia is that the content was drafted by a bunch of overly precise wankers focusing on the absolute right terminology without focusing on helping the reader understand the content.

  2. Re:#1 Floating Point Rule by abigor · · Score: 5, Insightful

    "The floating-point types are float and double, which are conceptually associated with the 32-bit single-precision and 64-bit double-precision format IEEE 754 values and operations as specified in IEEE Standard for Binary Floating-Point Arithmetic , ANSI/IEEE Std. 754-1985 (IEEE, New York)."

    http://java.sun.com/docs/books/jvms/second_edition/html/Overview.doc.html

  3. Re:If you want accuracy... by JamesP · · Score: 4, Insightful

    Maybe because BCD is the worse possible way to do 'proper' decimal arithmetic, also it would absolutely be very slow.

    BCD = 2 decimal digits per 8 bits (4 bits per dd). Working 'inside' the byte sucks

    Instead you can put 20 decimal digits in 64bits (3.2 bits per db) and do math much more faster

    Why don't any languages except COBOL and PL/I use it?

    Exactly

    --
    how long until /. fixes commenting on Chrome?
  4. Re:I'd just avoid it by -brazil- · · Score: 4, Insightful

    You've never done any scientific computing, it seems. While it's a very broad term, and floats certainly not the best tool for *all* computing done by science, anyone with even the most basic understanding knows that IEEE 754 floats *are* the best tool most of the time and exactly the result of deciding how much accuracy you need and implementing that with as many bytes of data as it takes. Hardly anything in the natural sciences needs more accuracy than a 64 bit float can provide.

    --

    The illegal we do immediately. The unconstitutional takes a little longer.
    --Henry Kissinger