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."

23 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. .9999999984 Post by tonywestonuk · · Score: 5, Funny

    Damn...Missed it! lol

    1. Re:.9999999984 Post by Yvan256 · · Score: 4, Funny

      I see you're still using that Pentium CPU.

  3. Only scratching the surface by ameline · · Score: 4, Interesting

    You really need to talk about associativity (and the lack of it). ie a+b+c != c+b+a, and the problems this can cause when vectorizing or otherwise parallelizing code with fp.

    And any talk about fp is incomplete without touching on catastrophic cancellation.

    --
    Ian Ameline
  4. strictfp by lenmaster · · Score: 4, Informative

    This article should mention strictfp in the section on Java.

  5. 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

  6. Re:Analog Computers by Anonymous Coward · · Score: 5, Informative

    No, irrationality has nothing to do with it. It's a matter of numeric systems, i.e. binary vs. decimal. For example, 0.2 is a rational number. Express it in binary floating point and you'll see the problem: 2/10 is 1/5 is 1/101 in binary. Let's calculate the mantissa: 1/101=110011001100... (long division: 1/5->2/5->4/5->8/5=1,r3->6/5=1,r1->2/5->4/5->8/5...)

    All numeric systems have this problem. It keeps tripping up programmers because of the conversion between them. Nobody would expect someone to write down 1/3 as a decimal number, but because people keep forgetting that computers use binary floating point numbers, they do expect them not to make rounding errors with numbers like 0.2.

  7. Before we get by toxygen01 · · Score: 4, Informative

    to floating point, please, everyone should've read Everything you ever wanted to know about C types and part 2 (which explains fp too).

    this will save a lot of time & questions to most beginning (and maybe mediocre) programmers.

  8. I'd just avoid it by Chemisor · · Score: 4, Interesting

    Given the great complexity of dealing with floating point numbers properly, my first instinct, and my advice to anybody not already an expert on the subject, is to avoid them at all cost. Many algorithms can be redone in integers, similarly to Bresenham, and work without rounding errors at all. It's true that with SSE, floating point can sometimes be faster, but anyone who doesn't know what he's doing is vastly better off without it. At the very least, find a more experienced coworker and have him explain it to you before you shoot your foot off.

    1. Re:I'd just avoid it by -brazil- · · Score: 4, Informative

      The non-trivial problems with floating-point really only turn up in the kind of calculations where *any* format would have the same or worse problems (most scientific computing simply *cannot* be done in integers, as they overflow too easily).

      Floating-point is an excellent tool, you just have to know what it can and cannot do.

      --

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

    2. 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

  9. 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?
  10. Stop with the educational articles by sunderland56 · · Score: 4, Funny

    Look, times are tough for programmers already. Knowing how to do things correctly - like proper floating point math - is one of the ways to separate the true CS professional from the wannabe new graduates. Articles like this just make everyone smarter, and make finding a job that much harder.

  11. Not sure it belongs in an intro explanation, but by dr2chase · · Score: 4, Informative
    Other issues that might be worth mentioning:
    • Catastrophic cancellation in complex arithmetic.
    • Single vs double rounding, in fused vs cascaded multiply-add operations.
    • Range reduction in trig functions (Intel hardware only uses a 68-bit PI, this causes problems sometimes).
    • Double-rounding when converting FP precision (e.g., 64-bit mantissa to 53, or 53 with extended exponent to 53 with regular exponent when the mantissa goes denorm).
    • Conversion to/from string representation.
    • Issues with "constructive reals" (a=b? is not necessarily an answerable question -- you might need to look at "all" the digits in order to answer "yes").
    • Distributive law DOES NOT HOLD -- a * (b+c) != a*b + a*c
  12. Re:#1 Floating Point Rule by sdiz · · Score: 5, Informative

    Java have a strictfp keyword for strict IEEE-754 arithmetic.

  13. Re:float are over by sco08y · · Score: 4, Funny

    Really, the best answer is to store all numbers on the cloud, and just use a 256-bit GUID to look them up when needed.

  14. Please look here by ctrl-alt-canc · · Score: 4, Informative

    People interested into floating point math will find some very interesting materials and horror stories in the documents collected at the home page of professor William Kahan, the man behind IEEE754 standard.
    According to my personal experience the paper by David Goldberg cited in the post isn't that difficult after all. Plenty of interesting materials can also be found in the Oppenheim & Shafer textbook about digital signal processing.

  15. Hard to debug floating point when it goes wrong! by Cliff+Stoll · · Score: 4, Interesting

    Over at Evans Hall at UC/Berkeley, stroll down the 8th floor hallway. On the wall, you'll find an envelope filled with flyers titled, "Why is Floating-Point Computation so Hard to Debug whe it Goes Wrong?"

    It's Prof. Kahan's challenge to the passerby - figure out what's wrong with a trivial program. His program is just 8 lines long, has no adds, subtracts, or divisions. There's no cancellation or giant intermediate results.

    But Kahan's malignant code computes the absolute value of a number incorrectly on almost every computer with less than 39 significant digits.

    Between seminars, I picked up a copy, and had a fascinating time working through his example. (Hint: Watch for radioactive roundoff errors near singularities!)

    Moral: When things go wrong with floating point computation, it's surprisingly difficult to figure out what happened. And assigning error-bars and roundoff estimates is really challenging!

    Try it yourself at:
    http://www.cs.berkeley.edu/~wkahan/WrongR.pdf

  16. Re:#1 Floating Point Rule by Eharley · · Score: 4, Informative

    I think the original poster was referring to this piece by the father of floating point, William Kahan, and Joe Darcy

    "How Java's Floating-Point Hurts Everyone Everywhere"

    http://www.eecs.berkeley.edu/~wkahan/JAVAhurt.pdf

  17. Re:Another potential solution is Interval arithmet by OSPolicy · · Score: 4, Informative

    Internal arithmetic always includes the exact solution, but only the rarest circumstances does it actually give the exact solution. For example, an acceptable interval answer for 1/3 would be [0.33,0.34]. That interval includes the exact answer, but does not express it.

  18. Thanks to Sun by khb · · Score: 4, Interesting

    Note that the cited paper location is docs.sun.com; this version of the article has corrections and improvements from the original ACM paper. Sun has provided this to interested parties for 20odd years (I have no idea what they paid ACM for rights to distribute).

    http://www.netlib.org/fdlibm/ is the Sun provided freely distributable libm that follows (in a roundabout way) from the paper.

    I don't recall if K.C. Ng's terrific "infinite pi" code is included (it was in Sun's libm) which takes care of intel hw by doing the range reduction with enough bits for the particular argument to be nearly equivalent to infinite arithmetic.

    Sun's floating point group did much to advance the state of the art in deployed and deployable computer arithmetic.

    Kudos to the group (one hopes that Oracle will treat them with the respect they deserve)

  19. Re:Simple, effective and useful by Dog-Cow · · Score: 5, Funny

    That would be because 0.1 + 02 is 2.1. :-)