git.net

# Truncation error

```On 2020-10-10, Peter J. Holzer <hjp-python at hjp.at> wrote:
> 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:
>     [...]

>     >>> from decimal import *
>     >>> a = Decimal(3)
>     >>> a
>     Decimal('3')
>     >>> b = Decimal(1E50)
>     >>> b
>     Decimal('100000000000000007629769841091887003294964970946560')
>     [...]

There are two problems with your code:

1. You meant Decimal('1e50').  What you typed creates a Decimal value
from the IEEE 64-bit floating point value closest to 1e50.

2. You need to increase the context precision.  It defaults to 28,
and you're example needs it to be at least 51:

>>> getcontext().prec = 100
>>> a = Decimal(3)
>>> b = Decimal('1e50')
>>> c = Decimal(2)
>>> a + b - c - b
Decimal('1')
>>> b - b + a - c
Decimal('1')
>>> a + (b - b) - c
Decimal('1')
>>> a + b - b - c
Decimal('1')

Like other floating point systems, you still need to know what you're
doing if you want to get the "right" results.

--
Grant

```