Slashdot Mirror


Floating Point Programming, Today?

An anonymous reader asks: "I'm rather new with programming and stumbled across these twe articles: The Perils of Floating Point from 1996 and What Every Computer Scientist Should Know About Floating-Point Arithmetic from 1991. I tried some of the examples in these articles with Intel's Fortran Compiler and g77 and noted that some of those issue reported no longer seem valid whereas quite a few still very much are around. Could someone, please, give me a pointer to some newer thoughts and/or new facts surrounding floating point programming. What has been improved since those articles were written? What is still the same? How is the future, especially with the new platforms IA64 and AMD64? I am most interested in the x86 and x86-64 architectures. Thank you for your kind help."

24 of 111 comments (clear)

  1. The articles are quite up-to-date by Mik!tAAt · · Score: 5, Informative

    Both articles are still valid today, mostly because current processors use the same IEEE floating point format than the ones available in 96 (or 91).

    --
    This is the place where you write something that will make you seem like a complete idiot.
  2. Don't worry by Hard_Code · · Score: 5, Funny

    ...those articles are only 99.99999891 percent true

    --

    It's 10 PM. Do you know if you're un-American?
  3. Unsolvable problem by Anonymous Coward · · Score: 5, Informative

    Floating point stuff hasn't really changed much since then. Basic rule of thumb, if you want it to be accurate don't use floating point.

    Much the same problem as you have with decimals. Many fractions cannot be evaluated evenly in certain bases. It will always cause you headaches if you don't realize this.

    Try writing a bunch of numbers in hex but then do all of your calculations in decimal. you'll have the same problem.

    1. Re:Unsolvable problem by john_many_jars · · Score: 4, Informative

      The use of floating point numbers isn't all bad. Those of use who use them are often solving problems with condition numbers that render the answer we get less accurate than the number of digits of accuracy provided.

      Think about tan(89.99) versus tan(89.991) (which is very ill-conditioned around 90). Both numbers are not terribly truncated by floating point, but the results are different by about 1,000. Try it and you'll see floating point error isn't as dangerous as things like cancellation, ill-conditioning and the like.

    2. Re:Unsolvable problem by Daleks · · Score: 4, Insightful

      Basic rule of thumb, if you want it to be accurate don't use floating point.

      Basic rule of thumb, determine what accuracy you need, then pick your number representation.

    3. Re:Unsolvable problem by Phronesis · · Score: 3, Informative
      Think about tan(89.99) versus tan(89.991) (which is very ill-conditioned around 90). Both numbers are not terribly truncated by floating point, but the results are different by about 1,000. Try it and you'll see floating point error isn't as dangerous as things like cancellation, ill-conditioning and the like.

      tan(89.990) = -2.0460
      tan(89.991) = -2.0408

      perhaps you're thinking of

      tan(1.571) = -4909.8
      and
      tan(1.578) = -138.8

    4. Re:Unsolvable problem by looseBits · · Score: 3, Funny

      Wouldn't it be simpler if humans only had 2 fingers instead of 10. Hell, that's how many I type with anyway.

      --
      Lord, bless my users that they may stop being such fucking idiots!!
    5. Re:Unsolvable problem by Admiral+Burrito · · Score: 3, Informative
      Try writing a bunch of numbers in hex but then do all of your calculations in decimal. you'll have the same problem.

      Actually, you won't. You would the other way though.

      The problem occurs when you try to represent a (properly reduced) fraction whos denominator has one or more prime factors not in common with your number base.

      You can represent one tenth in base 10 because all the prime factors in the denominator (10: 5,2) are found within the factorization of the base (also 10: 5,2). You can not represent one sixth in base 10 because one of the factors of 6 is not found in the factorization of 10 (3). Likewise, you cannot represent one tenth in base 2, because the denominator (10) is a multiple of 5, which is a prime not found in the factorization of the base (2).

      Because the factorization of 16 contains only primes that are in the factorization of 10 (2) all fractions that can be represented in hexadecimal can be represented in decimal. The reverse is not true, because 10 is the product of a prime (5) that is not found in the factorization of 16. So there is no way to get the "fifths" aspect of a decimal number into a hexadecimal number.

  4. Platform and all by Stary · · Score: 5, Informative

    It all depends on what platform you program on and so on. Newer x86 processors do their floating point in an 80-bit format and only truncate when copying back to your original 32 or 64 bit floats. That saves you some precision but not that much. As others have said, there are probably situations where almost all of the material in those articles is valid.

    --
    Tomorrow will be cancelled due to lack of interest
    1. Re:Platform and all by norwoodites · · Score: 4, Insightful

      It only truncates when saving to memory, that is why you can get different results when optimizing than not optimizing with gcc (you can force gcc to truncate all the time by using -ffloat-store).
      With gcc you can force the floating point calculations in the sse registers by -mfpmath=sse.

  5. Common mistake by PD · · Score: 5, Informative

    Don't count money as floating point. You'll just have rounding errors. Using long doubles instead of floats won't help you at all.

    The solution is to count pennies instead, or if you need values bigger than 22 million dollars, use a BCD library. BCD is Binary Coded Decimal.

    1. Re:Common mistake by PD · · Score: 3, Informative

      That's not the error I was addressing. Here's some definitions of a subtotal:

      float subtotal; // wrong way to represent money
      long subtotal_pennies; // right way to represent money

      And, if you're at a gas station, you need to represent money like this:

      long subtotal_mils; // gas per gallon has a 9/10 of a cent on the end - $1.34 9/10

      The calculations that you perform on the money are a completely different story. There's no point in worrying about 4 decimal places of percentages if you don't start from the right place.

    2. Re:Common mistake by AvantLegion · · Score: 4, Funny
      Don't count money as floating point. You'll just have rounding errors.

      But that's the point! And you transfer those fractions of cents (that just get rounded off anyway) into an account you control!

      "back up in your ass with the resurrection...."

  6. Here's an important one. by Apuleius · · Score: 4, Informative
  7. If you need more precision... by cfallin · · Score: 4, Informative

    Hardware floating point is only so accurate - if you need more floating point (or integer) precision, use GNU MP - a library for C with bindings for many other languages too. It came in quite handy when I wrote some cryptography code with very large numbers.

  8. Floating point operations are not that bad. by stj · · Score: 5, Insightful
    Well, I have a lot of experience with that since I've been doing numerical computations for last 7 years. First of all, it's not all that bad. With 64 bit 'double' in C, you get around 15 decimal digits of accuracy (theoretically 18, but in practice don't count on the end). You have to understand that numbers are stored in logarithmic format: mantissa and a factor to multiply it (in computers exponent of 2). If there is no overlap between two numbers in addition (that is for example one number is 1.234*2^64, and the other is 1.234*2^-15), the smaller one is always lost. The are two ways to get around it:

    extend mantissa so there is enough overlap - usually involves some kind of multiple precision libraries like mentioned in other post GNU MP and many others. I've implemented one for my own use, too. Generally means lots of overhead since there will be less than 5% of operations actually benefitting from greater precision.

    postpone such operations until there is overlap - store such numbers together and do operations on them together, too. Sometimes additions in loops will add up small parts so actually there will be overlap with big part and additions can be done with enough precision.
    On a side, interesting thing is that in computers multiplications and divisions are better (that is more accurate) than additions and subtractions because of logarithmic format.
    I know that Sun was working on a variable precision floating-point CPU. I'm not sure how that project is going and what the end effect is, but I remember it being an interesting idea.

    Multiple precision libraries are usually decent with only one problem, they are always slower by a couple orders of magnitude than regular CPU operations, so using them is just such a pain.

    --
    iThink iHate iMod
  9. Python-specific, but contains useful info for all. by tdelaney · · Score: 3, Informative
  10. Intervall Analysis by mvw · · Score: 3, Informative
    Ok, known issues with floating point routines that can be fixed (unintentional pun :-) should be fixed.

    On the other hand it is clear that a finite representation of real numbers has tradeoffs. But only few seem to care about the cumulated errors.

    My experience in engineering (simulation of casted turbine blades) was that people know that bad things can occur during complex floating point calculations but the matter was too complicated to be investigated.

    Example: if during finite element simulation a timestep did not end up with a valid solution (the iterative/approximative solver of the large linear systems did not converge or even crash) just some control parameters were varied (time step, perhaps material curves) until the calculation seemed to produce some valid looking result. Needless to say, that that only obvious errors can be spotted that way.

    The strange thing about all that is, that in the last years the mathematical discipline of interval analyis has been developed. Here every number is represented with its interval of known error bounds. These error intervall are kept and updated during calculations. Thus at the end of a large complex calculation, you know the error. That is a very valuable property.

    More, in fact what one does so in many cases is not only a standard calculation but rather machine proof of error bounds.

    This offers some unique properties, e.g. for rigorous global searches.

    So we have far better technology available. Why is this stuff not used more widely?

    As far as I know, only SUN puts interval analysis enabled data types in its FORTRAN and C/C++ compilers. But I have not seen that stuff in gcc, which would have a big impact.

    Very strange.

    To whom is interested, here is a homepage of the intervals community.

    Regards,
    Marc

  11. The world is not all float by Jouni · · Score: 3, Insightful
    Most desktop architectures have gone all the way to push wide bands of parallel processed float and double calculations through the pipes, but the mobile world is a whole different story.

    PDA level mobile FPUs are very rare indeed. In practice, devices using the ARM family processors have no hardware float support. It's thus very important for developers to understand floating point intimately, so that they won't be left at the mercy of awful compiler-emulated floating point code. Of course, in those cases most code tends to orient itself for fixed point arithmetic. Fixed point calculations are much better suited for the integer crunching power of, say, the Intel XScale.

    There are also good tradeoffs developers can make between floats and fixed point, for example by using block floating point (BFP) formats, where a whole block of values shares the same common exponent.

    Now that 3D is really coming to mobile devices, plenty of people will get first-hand experience of emulating floating point for the first time since the 80's. :-)

    Jouni

    --
    Jouni Mannonen | Game Designer, Consultant
  12. Any collegel level engineering numerical methods by alyandon · · Score: 3, Insightful

    Any college level engineering numerical methods course will teach you all the pitfalls involved with using floating point calculations on modern processors and how to minimize the impact of rounding errors (cumulative and otherwise) on your calculations.

    Hell, any decent numerical methods book should cover stuff like that as well.

  13. Huh? by joto · · Score: 4, Insightful
    I tried some of the examples in these articles with Intel's Fortran Compiler and g77 and noted that some of those issue reported no longer seem valid whereas quite a few still very much are around.

    Would you mind tell us what those "issues" where. Because the articles hardly deal with "issues" at all. What they deal with is the theoretic limitations that must exist in floating point, due to the fact that we have finite hardware, while real analysis assumes infinite precision. This should not have changed between 1991 and now (especially, since we have all standardized on IEEE floating point formats, but even if the article was from 1960, you should easily be able to "translate" it to your favourite floating point format (which is probably IEEE)).

    Could someone, please, give me a pointer to some newer thoughts and/or new facts surrounding floating point programming.

    There are very few new thoughts with regards to floating point programming, just as there are very few new thoughts on the use of "if-then-else"-branches or "while"-loops. Floating point programming is basically a solved problem. The only problem with it is that it sometimes flies in the face of intuition, and most programmers are ignorant about it. This has not changed since 1991 either.

    The articles you mentioned are very good articles for understanding issues surrounding floating point. Just make sure you read them with your brain, instead of just feeding your favourite compiler with any examples you see.

    What has been improved since those articles were written?

    Speed. Computers have become faster. (It's possible that there also have been some minor software improvements such as an ISO C addendum clarifying tricky areas with rounding modes, or something like that.)

    What is still the same?

    Essentially, nothing have changed.

    How is the future, especially with the new platforms IA64 and AMD64?

    Very predictable. Nothing will change there either. Non-IEEE floating point vector instructions, or "multimedia" instruction sets will probably continue to be unstandardized and platform-dependent.

    I am most interested in the x86 and x86-64 architectures

    There is nothing special about those architectures with respect to floating point (well, the x86 reuses its floating point registers for MMX instructions, but you shouldn't need to know that unless you use assembler).

  14. Nooooooooo! by Anonymous+Brave+Guy · · Score: 3, Funny

    It's past 1am and some **** is throwing inexact representations and fuzzy logic at me.... This must be a nightmare... Must... wake... up... Aaaaaargh!

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  15. Real world and catastrophic failures by Anonymous+Brave+Guy · · Score: 4, Interesting
    I would really like to know, if there are real world engineering examples, where simulations produced dangerous products, because the simulation was inadequate because of numerical errors. Perhaps in aerodynamics, who knows how they perform their flight simulations.

    I've worked on a couple of projects where this is very important. One was writing control software for metrology equipment, industrial strength QA kit that measured manufactured parts down to fractions of a micron or even nanometres to make sure they were in spec. Another was a geometric modelling tool used in CAD applications and the like.

    In neither case am I aware of any physical real world failure caused by a problem with the floating point calculations. You do have to be really careful with manipulating the numbers, though.

    For example, the loss of significance when you subtract can be horrible if you've got two position vectors close together, and you're trying to calculate a translation vector from one to the other. The error in that translation vector can be enormous if the points you started with were very close: you might get only one or two significant figures, when the rest of your values have 15 or more. If you're interested in the direction of the vector, that can give you errors of +/- several degrees!

    Inevitably, there are always going to be bugs in complex mathematical software, and I've seen plenty of wrong answers from programs like the above. Fortunately, it's normally possible to have checks and balances that at least identify and highlight inconsistencies so, in the worst case, at least nobody relies on them. You can also use ruthless automated testing procedures, which run zillions of calculations every night and flag the smallest changes in the results, so no-one accidentally breaks a verified algorithm with a change later. The combination makes it reasonably unlikely that any algorithm would fail catastrophically with the sort of consequences you're talking about.

    The possibility is always there, of course, because programming is subject to human error. However, FWIW, I've worked on software that's used to design cars, and software that controls the QA machinery to make sure they're put together right, and I still drive one. :-)

    --
    If you disagree, post your argument. (-1, Overrated) isn't your personal censorship tool for views you don't like.
  16. News Flash: Go To considered harmful by bellings · · Score: 5, Funny
    I'm assuming that sometime in the next week one of the slashdot editors will be trolled with an article like:
    I'm rather new with programming and stumbled across the article Go To Statement Considered Harmful from 1968. I tried some of the examples in this article, and noted that some of those issue reported no longer seem valid whereas quite a few still very much are around. What has been improved since the article was written? Will the new 64-bit architectures finally fix all the problems with Go To Statements, or is this something that the hardware designers still need to work on?
    --
    Slashdot is jumping the shark. I'm just driving the boat.