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

64 of 359 comments (clear)

  1. Analog Computers by Tisha_AH · · Score: 2, Informative

    I seems to me that this problem would pop up any time you worked with an irrational number.

    Back in the early days the analog computer was used for things like ballistic calculations. I would think that they would be less prone to this type of problem.

    Linearity may still be an issue (analog systems have their own set of problems).

    --
    Tisha Hayes
    1. Re:Analog Computers by maxume · · Score: 3, Insightful

      Precision isn't that big a deal (we aren't so good at making physical things that 7 decimal digits become problematic, even on something the scale of an aircraft carrier, 6 digits is enough to place things within ~ 1 millimeter).

      The bigger issue is how the errors combine when doing calculations, especially iterative calculations.

      --
      Nerd rage is the funniest rage.
    2. 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.

    3. Re:Analog Computers by cupantae · · Score: 2, Interesting

      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.

      --
      --
    4. Re:Analog Computers by moonbender · · Score: 2, Informative

      True, but irrational numbers are those that cannot be written down exactly in *any* base

      ... except the irrational number's own base. ;)

      --
      Switch back to Slashdot's D1 system.
    5. Re:Analog Computers by Chris+Mattern · · Score: 2, Insightful

      The problem is, if you're doing a long string of calculations--say a loop that repeats calculations thousands of times with the outcome of the last calculation becoming the input for the next (approximating integrals often does this) then the rounding errors can accumulate if you're not paying attention to how the floating point works.

    6. Re:Analog Computers by adonoman · · Score: 2, Funny

      Nobody would expect someone to write down 1/3

      I use base 3, so 0.1 is a perfectly easy number to express in floating point.

    7. Re:Analog Computers by RAMMS+EIN · · Score: 3, Interesting

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

      --
      Please correct me if I got my facts wrong.
    8. Re:Analog Computers by JWSmythe · · Score: 3, Insightful

          Well, it would depend on what you're doing the calculations for, and how you're doing them.

          Say it used diesel fired engines, and you were instructed to calculate the fuel consumption per engine revolution, and then apply that to a trip. I don't know the specifics on an aircraft carrier, so I'll just make up some numbers.

          At full speed, the ship travels at 12 nautical miles per hour (knots). The engines spin at 300rpm. It burns 1275 gallons of fuel per hour.

        That's 18,000 engine revolutions per hour, or 0.0708334 gallons per revolution.

          1,000 miles at 12 knots = 84.3333334 hours.

          If you are to travel 1,000 nautical miles, 18,000 * 83.3333334 = 1,500,000.0012 revolutino. At 0.0707334 gallons per revolution, that would be 106,100.100085 gallons.

          But knowing that it burns 1,275 gallons per hour at 12 knots, and you will be traveling for 83.3333334 hours, you will require 106,250.000085 gallons. Using the measure of gallons per revolution to try to come up with a very precise number to work with, you've actually fallen short by 150 gallons for the trip. I can imagine a slight embarrassment by having your aircraft carrier run out of fuel just 7 minutes from its destination.

          Using 7 decimal points of precision, when it's multiplied so many times, it can easily cause errors.

          I'd be pretty sure they aren't counting gallons per revolution, I only used that as an example of where errors could happen. If you're considering the full length of the ship, 0.1 inches is more than enough to believe you have a good number. :) I believe due to expansion of the metals, the total length of the ship may change more than that depending on if it's a hot or cold day. :)

      --
      Serious? Seriousness is well above my pay grade.
    9. Re:Analog Computers by maxume · · Score: 3, Insightful

      Sure, you can make it a problem, but it isn't particularly insidious.

      And the part where I say "The bigger issue is how the errors combine when doing calculations" is a pretty compact version of what you said.

      --
      Nerd rage is the funniest rage.
  2. 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.

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

    2. Re:.9999999984 Post by Splab · · Score: 2, Insightful

      Bullshit.

      1. The web was very much alive when the FDIV bug was discovered.
      2. I seriously doubt you as a teenager spent 2 weeks finding this bug, this guy (who happens to be the one who found it) spent some weeks digging through everything to prove it was the FPU that bit him:
      http://www.trnicely.net/pentbug/pentbug.html

      Oh, and even when the "computer is wrong" it's still a human error.

  4. 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
  5. strictfp by lenmaster · · Score: 4, Informative

    This article should mention strictfp in the section on Java.

    1. Re:strictfp by owlstead · · Score: 2, Insightful

      Not really. It might point to BigDecimal, but leave strictfp out of it. Remember, this is for starting programmers, not creators of advanced 3D or math libs.

  6. If you want accuracy... by Nutria · · Score: 2, Interesting

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

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

    --
    "I don't know, therefore Aliens" Wafflebox1
    1. 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?
    2. Re:If you want accuracy... by larry+bagina · · Score: 2, Insightful

      on paper, I'd express it as 1/3 and not truncate it.

      --
      Do you even lift?

      These aren't the 'roids you're looking for.

    3. Re:If you want accuracy... by TheRaven64 · · Score: 3, Interesting

      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.

      --
      I am TheRaven on Soylent News
    4. Re:If you want accuracy... by amorsen · · Score: 2, Insightful

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

      I want accurate math, not estimates.

      Math with 20 decimal digits in 64 bits is proper decimal arithmetic. It acts exactly like BCD does, it just doesn't waste tons of space and CPU power.

      --
      Finally! A year of moderation! Ready for 2019?
    5. Re:If you want accuracy... by poopdeville · · Score: 2, Informative

      Continued fractions are a straightforward way to implement exact computable real arithmetic. So yes, it's been used. And it's slow. But it is exact.

      --
      After all, I am strangely colored.
    6. Re:If you want accuracy... by JamesP · · Score: 2, Insightful

      You completely missed my point.

      I'm not comparing BCD to floating point, I'm comparing BCD with other ways of encoding decimal numbers in a computer

      --
      how long until /. fixes commenting on Chrome?
    7. Re:If you want accuracy... by TheRaven64 · · Score: 2, Informative
      In the 1620, cited in the Wikipedia article that you mention, it wasn't just a way of doing arithmetic, it was the only way. It was a variable-word-length machine with 6-bit bytes, each storing one decimal digit or some flags, or a single character from a 64 character set. One special value indicated the end of a word.

      It didn't, however, as you imply, have BCD hardware. In fact, it had no hardware at all for arithmetic. At the start of the core memory, you had two lookup tables, one for addition and one for multiplication. To add two values together, you iterated over each pair of (decimal) digits, looking up their values in the table and then storing the result somewhere. If one of the digits had the carry bit set, then you did the same thing with the result of adding the next two digits together to add the extra one.

      The 1620 was nicknamed CADET by its designers for this reason - Can't Add, Doesn't Even Try. It was one of IBM's first attempts to make a cheap computer (you could buy a minimal unit for around $100,000 (paper tape instead of punch cards, only 10,000 characters of memory). It was the first computer my university ever bought, and the subject of an incredibly scathing review by Dijkstra.

      --
      I am TheRaven on Soylent News
    8. Re:If you want accuracy... by AuMatar · · Score: 3, Insightful

      If you want accuracy, BCD is still a failure. It only does base 10 instead of base 2. A truly accurate math system would use 2 integers, one for numerator and one for denominator and thus get all rational numbers. If you need irrationals you get even more complicated. But don't pretend BCD is accurate, it fails miserably on common math problems like 1/3.

      --
      I still have more fans than freaks. WTF is wrong with you people?
  7. 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

  8. Another potential solution is Interval arithmetic by renoX · · Score: 3, Insightful

    Maybe in your list of solutions you could mention interval arithmetic, it's not very much used, but it gives "exact" solution.

  9. Re:#1 Floating Point Rule by lenmaster · · Score: 3, Informative

    If you think that every language except Java implements IEEE-754 to the letter, you are sadly mistakenly. That fact is Java can be used just fine for floating point work in most applications.

  10. Re:#1 Rule, Don't use Java by abigor · · Score: 2, Informative

    Actually, the linked article says exactly the opposite, and up above I posted a link to the JVM definition that verifies it. So you are 100% incorrect.

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

  12. 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 Rogerborg · · Score: 2, Insightful

      Depends how many "integers" you use. If you need accuracy - and "scientific" computing certainly does - then don't use floats. Decide how much accuracy you need, and implement that with as many bytes of data as it takes.

      Floats are for games and 3D rendering, not "science".

      --
      If you were blocking sigs, you wouldn't have to read this.
    3. 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

  13. No, base 10 arithmetic isn't "more accurate". by Animats · · Score: 3, Interesting

    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.

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

    1. Re:Stop with the educational articles by sohp · · Score: 2, Insightful

      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.

      True, except that HR people and hiring managers neither know nor care about doing things correctly, they just want cheap and fast. Just make sure you have all the right TLAs on your resume, you'll get a job. You can put "IEEE 754 expert" down though. They won't recognized the reference so maybe they'll be impressed by it.

    2. Re:Stop with the educational articles by jellomizer · · Score: 2, Funny

      Except for the fact that companies don't care about floating point they are looking for 3+ years on windows 7. 20 years of Linux. and 15 years of .NET.

      --
      If something is so important that you feel the need to post it on the internet... It probably isn't that important.
  15. 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
  16. Re:Three one-thirds is 1, riiiiight? by jjohnson · · Score: 2, Informative

    >>> (1.0/3)*3
    1.0

    --
    Anyone who loves or hates any language, platform, or manufacturer, doesn't know what they're talking about.
  17. Re:#1 Floating Point Rule by kestasjk · · Score: 3, Insightful

    I'm not sure whether that is factually true, but IEEE-754 isn't exactly perfect or without alternatives so I wouldn't base my language choice on it..

    That'd be like not using Java because it doesn't represent ints using ones complement; if your code relies on the specific internal implementation of data primitives you're probably doing something wrong.
    (Before I get replies: Of course sometimes these things really do matter, but not often enough to dismiss a multi-purpose langauge.)

    --
    // MD_Update(&m,buf,j);
  18. Re:#1 Floating Point Rule by sdiz · · Score: 5, Informative

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

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

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

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

  22. Re:Simple, effective and useful by Dumnezeu · · Score: 2, Informative

    And wrong. I don't know how to use Github and if he won't bother to post an email address, I won't bother to learn about Github just for this.
    The comparison page is wrong. Take nearlyEqual(0.0000001, 0) for example. As the author said, using Epsilon can be bad if you don't know what you are doing. The correct form of the function is:

    epsilon = 0.00001;
    function nearlyEqual(a,b)
    {
            return (Math.abs(b) < epsilon) ? (Math.abs(a) < epsilon) : (Math.abs((a-b)/b) < epsilon);
    }

    Also, parentheses don't hurt you and they help the reader.

    --
    Yes, it's sarcasm. Deal with it!
  23. Not equivalent to Spolsky's article. by John+Hasler · · Score: 2, Funny

    It's missing the irritating cutesy "humor".

    --
    Warning: this article may contain humor, sarcasm, parody, and perhaps even irony. Read at your own risk.
  24. Re:Simple, effective and useful by -brazil- · · Score: 2, Insightful

    Sure it can be: by starting with simple explanations fit for novices (who usually aren't actually doing serious numerical math and simply wonder how come 0.1 + 02 != 0.3) and getting into more details progressively.

    And I mention the alternatives to floating-point formats and when to use what.

    --

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

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

  26. Re:Hard to debug floating point when it goes wrong by Lord+Efnar · · Score: 2, Interesting
    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];
    w
    ]
    Plot[Evaluate[h[x]], {x, 0, 2}]

    The result: http://www.untruth.org/~josh/real-rounding.png

  27. Re:Simple, effective and useful by safetyinnumbers · · Score: 2, Funny

    The code on the site for

    function nearlyEqual(a,b)

    contains the line

    if (a==0.0){

    Clearly, this should be

    if (nearlyEqual(a, 0.0)){

    There. What could possibly go wrong now? :)

  28. Re:Simple, effective and useful by -brazil- · · Score: 2, Informative

    In case you weren't just being funny, that == is correct, as it's meant to prevent NaN or Infinity results from the division, which can only happen with the actual "zero" values.

    --

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

  29. Oops! by Lord+Efnar · · Score: 2, Informative

    Well, so much for my smug mathy reply! Amusingly, the reason it worked so well is that my "Evaluate" statement asked Mathematica to symbolically evaluate the function prior to graphing. So, the graph looked nice because Mathematica was just graphing the "Abs[x]" function!
    Without the symbolic evaluation or requesting a particular precision level, the graph you actually get is this:
    http://www.untruth.org/~josh/real-rounding-oops1.png
    You can get a more reasonable looking answer by messing with the calculation precision and accuracy...

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

  31. 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)

  32. Re:Simple, effective and useful by petermgreen · · Score: 3, Insightful

    I don't think you are correct about two numbers not being "nearly equal" when they are both close to zero, but with opposite signs. The function returns "true" in this case, no? Are you suggesting this is undesirable? I could see for some use cases that property might be undesirable, but if that's what you meant it wasn't clear. Certainly that property is desirable for some applications.
    IMO this sort of thing is a good reason NOT to write a nearlyequals(a,b) function. That will just lull you into a false sense of security that the same rules are appropriate in every case.

    You need to consider each case on it's own merits to decide what is meant by "nearly equals" in context.

    In some cases that may be best defined in terms of absolute error, in some cases that may be best defined in terms of error relative to the value and in yet other cases it may be best defined in terms of the error relative to the current precision which is related to the value for larger numbers but becomes fixed for smaller (subnormal) numbers.

    --
    note: i'm known as plugwash most places but i screwd up registering that here somehow in the past and now can't register
  33. Re:#1 Floating Point Rule by gnasher719 · · Score: 3, Interesting

    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.

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

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

  35. Re:Simple, effective and useful by JWSmythe · · Score: 2, Interesting

        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?

    --
    Serious? Seriousness is well above my pay grade.
  36. Re:#1 Floating Point Rule by Trepidity · · Score: 2, Insightful

    There are some decent points there, but a lot of them aren't really related to IEEE 754 compatibility. For example, bullet point #5 on their first-page list of five "gratuitous mistakes" is that Java doesn't support operator overloading. But by that standard, C sucks too, and yet is somehow used in lots of floating-point libraries.

  37. Re:Amazing how few programmers use real maths. by spitzak · · Score: 2, Insightful

    It is safe to compare to any small integer, not just zero, as long as you are checking if the the value came from an assignment. It is also safe to use small negative powers of two.

    One big problem I have is with programmers who religiously add these epsilon functions and screw up algorithms. In my experience, about 99% of the == statements with floating point are explicitly testing "did the assignment to the same value earlier get executed?" Comparing the bit patterns is exactly what is wanted, stop messing it up!

  38. and Ada by krischik · · Score: 2, Informative

    Correction: COBOL, PL/I and Ada. Ada has both fixed and floating point BCD arithmetic. And yes I too wonder why it is not in wider use. Perhaps it has to do with the ill conceived notion of "light weight languages" - most of which are not light weight at all any more once they are on the market for decade or so.

    Martin