Floating-Point

new topic     » topic index » view thread      » older message » newer message

1. floating-point calculations are not always exact, and a tiny
    error in a calculation can be magnified into a huge error.
    floor(0.99999) is 0, as is floor(0.9999999999... trillion places)
    It is your responsibility as a programmer to properly handle
    these situations.

2. The number 0.1 and many others like it, can't be represented
     exactly in binary floating-point. You may get something like
     0.09999999999999999 which leads to surprises when
     floor(10*x) is applied. The answer is not to seek out a
     binary f.p. software library that will give you trillions
     of digits of accuracy. That will just give you trillions of 9's.
     The answer is to apply a bit of rounding before you use floor(),
     or switch to Cobol. Even Cobol uses binary floating-point, but
     it also has something called "packed decimal" which has a few
     problems of its own.

3. When you subtract numbers of approximately equal magnitude,
     you are likely to lose a lot of accuracy. Subtracting two numbers
     that are accurate to 16 digits can easily leave you with a result
     that is only accurate to 14, 13, ... whatever number of digits.

4. All of the above applies to just about *any* programming
    language. It is a basic fact of life when binary floating-point
    is used. Intel and most other CPU's provide IEEE binary
    floating-point in the hardware.

5. By default, Euphoria prints numbers rounded to 10
    significant digits. This is an arbitrary cutoff point that was
    chosen so as to not cause undue eyestrain or clutter.
    If you want to see more digits, use printf(1, "%.16e", x).

6. Euphoria is not doing anything mysterious. It is using IEEE
    binary floating point for all floating-point calculations.
    Careful line by line study of the Euphoria source code
    would tell you nothing about these basic issues of
    floating-point accuracy. Recoding your calculation in C,
    or some other language will likely give you the same results.

7. "But my pocket calculator never gives me these problems!"
    Calculators usually retain a few extra "guard" digits that they
    don't display. They show rounded results. They also typically
    use decimal floating-point.

Regards,
   Rob Craig
   Rapid Deployment Software
   http://www.RapidEuphoria.com

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu