Catch up on stories from the past week (and beyond) at the Slashdot story archive


Forgot your password?
DEAL: For $25 - Add A Second Phone Number To Your Smartphone for life! Use promo code SLASHDOT25. Also, Slashdot's Facebook page has a chat bot now. Message it for stories and more. Check out the new SourceForge HTML5 internet speed test! ×
Programming Math

What Every Programmer Should Know About Floating-Point Arithmetic 359

-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."
This discussion has been archived. No new comments can be posted.

What Every Programmer Should Know About Floating-Point Arithmetic

Comments Filter:
  • by ameline ( 771895 ) <`moc.liamg' `ta' `enilema.nai'> on Sunday May 02, 2010 @11:45AM (#32064214) Homepage Journal

    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.

  • by Nutria ( 679911 ) on Sunday May 02, 2010 @11:57AM (#32064276)

    use BCD math. With h/w support it's fast enough...

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

  • I'd just avoid it (Score:5, Interesting)

    by Chemisor ( 97276 ) on Sunday May 02, 2010 @12:13PM (#32064396)

    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.

  • by Animats ( 122034 ) on Sunday May 02, 2010 @12:19PM (#32064454) Homepage

    The article gives the impression that base 10 arithmetic is somehow "more accurate". It's not. You still get errors for, say, 1/3 + 1/3 + 1/3. It's just that the errors are different.

    Rational arithmetic, where you carry along a numerator and denominator, is accurate for addition, subtraction, multiplication, and division. But the numerator and denominator tend to get very large, even if you use GCD to remove common factors from both.

    It's worth noting that, while IEEE floating point has an 80-bit format, PowerPCs, IBM mainframes, Cell processors, and VAXen do not. All machines compliant with the IEEE floating point standard should get the same answers. The others won't. This is a big enough issue that, when the Macintosh went from Motorola 68xxx CPUs to PowerPC CPUs, most of the engineering applications were not converted. Getting a different answer from the old version was unacceptable.

  • by TheRaven64 ( 641858 ) on Sunday May 02, 2010 @12:30PM (#32064544) Journal

    also it would absolutely be very slow

    Depends on the architecture. IBM's most recent POWER and System-Z chips have hardware for BCD arithmetics.

  • Re:Analog Computers (Score:2, Interesting)

    by cupantae ( 1304123 ) <<moc.liamg> <ta> <llienoram>> on Sunday May 02, 2010 @12:48PM (#32064678)

    Irrational numbers are not such a problem as rational numbers which can't be represented in the base used.

    Lets say our computer has 6-digit-decimal precision. If you add two irrational numbers, say pi and e, you'll get 5.85987. It's imprecise, but imprecision is necessary, since it can't be represented in any base.

    But if you add 3/7 and 2/3 you get 1.90524 which is imprecise even though a precise answer does exist.

  • by Cliff Stoll ( 242915 ) on Sunday May 02, 2010 @01:19PM (#32064930) Homepage

    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: []

  • by Lord Efnar ( 30962 ) on Sunday May 02, 2010 @02:46PM (#32065604) Homepage
    That is neat, but some (math oriented) languages do just fine:
    Some Mathematica code:

    h[x_] := Module[{y, w, k},
    y = Abs[x];
    For[k = 1, k <= 128, k++, y = Sqrt[y]];
    w = y;
    For[k = 1, k <= 128, k++, w = w^2];
    Plot[Evaluate[h[x]], {x, 0, 2}]

    The result: []

  • by Anonymous Coward on Sunday May 02, 2010 @03:33PM (#32065928)

    And I've seen the addition of two money columns defined in Access get magical values. I'm sure somebody here can explain the situation here better than I can, but I've seen $1.00+.50 become $1.49. But in MS Access's defense, a float is a poor way to define money especially in MS Access. I was just hired to bandaid the broken solution.

  • Re:Analog Computers (Score:4, Interesting)

    by RAMMS+EIN ( 578166 ) on Sunday May 02, 2010 @03:55PM (#32066068) Homepage Journal

    ``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.''

    A problem which is exacerbated by the fact that many popular programming languages use (base 10) decimal syntax for (base 2) floating point literals. Which, first of all, puts people on the wrong foot (you would think that if "0.2" is a valid float literal, it could be represented accurately as a float), and, secondly, makes it impossible to write literals for certain values that _could_ actually be represented exactly as a float.

  • Thanks to Sun (Score:5, Interesting)

    by khb ( 266593 ) on Sunday May 02, 2010 @04:04PM (#32066118)

    Note that the cited paper location is; 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). [] 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)

  • by gnasher719 ( 869701 ) on Sunday May 02, 2010 @05:11PM (#32066478)

    Repeatability. If your code and language are standard-compliant, then you'll get the same floating-point math results as someone using another compliant language on any other platform. Not crucial for some tasks, but it certainly is for others, such as scientific work.

    Wouldn't it be great if you could change a switch in your computer to change all double precision fp from 53 bit mantissa to 52 bit, and if your results are suddenly radically different then you know your first set of results couldn't be trusted?

    Repeatability is highly overrated. It's no good if you get the wrong results, and a different computer system gets you identical wrong results.

  •     That's what I was thinking too. But hey, what do I know, I just work computers, I'm not a mathematician. :)

        The way some folks do it,

    0.1 + 02 = 0 + 2
    0 + 2 = 2

        There was a thread on here a few weeks ago, where I explained it in the calculation of payroll. If you're calculating fractional hours, then those decimals come in handy.

        1 minute = 0.0166666666666667 hours.
        Depending on how many decimal points you make it, it can really mess with your pay.

        0.01 * 60 = 0.6
        0.02 * 60 = 1.2
        0.0166 * 60 = 0.996
        0.0167 * 60 = 1.002

        For hourly folks, check your paychecks. I'd bet the company is using the most advantageous rounding for their profit rather than for accuracy.

        I was recently told on a something that one interval = 0.0083333 (1/120), that it should always be simply cut off (not rounded) at 1 decimal point. I tried to explain, that would make the numbers totally wrong.

    1 = 0.0
    10 = 0.0

        10 instances of 10 would then be 0.0, rather than 0.8. They wanted "absolute" accuracy over thousands of instances, but still insisted chopping it off to one decimal place is the way they wanted it. *sigh*

        I do understand why floating point numbers can induce errors, but is it necessary to make it worse by adding in sloppy math?

A year spent in artificial intelligence is enough to make one believe in God.