Maths error

Dan Bishop danb_83 at yahoo.com
Mon Jan 8 19:19:05 EST 2007


On Jan 8, 3:30 pm, Rory Campbell-Lange <r... at campbell-lange.net> wrote:
> >>> (1.0/10.0)  + (2.0/10.0) + (3.0/10.0)
> 0.60000000000000009
> >>> 6.0/10.0
> 0.59999999999999998
>
> Is using the decimal module the best way around this? (I'm expecting the first
> sum to match the second).

Probably not.  Decimal arithmetic is NOT a cure-all for floating-point
arithmetic errors.

>>> Decimal(1) / Decimal(3) * Decimal(3)
Decimal("0.9999999999999999999999999999")
>>> Decimal(2).sqrt() ** 2
Decimal("1.999999999999999999999999999")

> It seem anachronistic that decimal takes strings as
> input, though.

How else would you distinguish Decimal('0.1') from
Decimal('0.1000000000000000055511151231257827021181583404541015625')?




More information about the Python-list mailing list