[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Truncation error

On 2020-10-07 07:53:55 +0200, Marco Sulla wrote:
> If you want to avoid float problems, you can use Decimal:

Decimal doesn't avoid floating point problems, because it is a floating
point format. For example:

    Python 3.8.5 (default, Jul 28 2020, 12:59:40)
    [GCC 9.3.0] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> from decimal import *
    >>> a = Decimal(3)
    >>> a
    >>> b = Decimal(1E50)
    >>> b
    >>> c = Decimal(2)
    >>> a + b - c - b
    >>> b - b + a - c
    >>> a + (b - b) - c
    >>> a + b - b - c

Mathematically, all four expressions should have the result 1, but with
floating point numbers they don't because intermediate results are

For comparison, here are the results with float:

    >>> a + b - c - b
    >>> b - b + a - c
    >>> a + (b - b) - c
    >>> a + b - b - c

One could argue that these are at least closer to the truth, although I
think that's just luck,

Decimal does have two advantages over float: 

a) It's precision is configurable and even by default higher. So on
   average, the error is smaller (but still not zero).

b) It uses decimal numbers like we learned in school. So it will make
   the same errors as we make when we use pencil and paper, which is
   less confusing to laypersons than the seemingly arbitrary errors from
   converting from decimal to binary and back.

The disadvantages are of course higher memory consumption and lower
speed. Also, I'm very confident that the engineers at Intel and AMD knew
what they were doing when they designed the FP units of their
processors. I'm slightly less confident about the authors of the Decimal
module. And I'm much less confident that the average Python programmer
can implement a matrix multiplication with Decimal which is as
numerically stable as what Matlab or Pandas provide using IEEE-754


PS: I recently read an interesting article on the Android calculator.
    That goes to extreme lengths to avoid unexpected rounding errors.
    It is also very slow, but still faster than a human can look, so it
    doesn't matter.

   _  | Peter J. Holzer    | Story must make more sense than reality.
|_|_) |                    |
| |   | hjp at         |    -- Charles Stross, "Creative writing
__/   | |       challenge!"
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <>