python 3.44 float addition bug?

Ben Finney ben at benfinney.id.au
Wed Jun 25 23:39:23 EDT 2014


Steven D'Aprano <steve at pearwood.info> writes:

> On Wed, 25 Jun 2014 14:12:31 -0700, Maciej Dziardziel wrote:
>
> > Floating points values use finite amount of memory, and cannot
> > accurately represent infinite amount of numbers, they are only
> > approximations. This is limitation of float type and applies to any
> > languages that uses types supported directly by cpu. To deal with it
> > you can either use decimal.Decimal type that operates using decimal
> > system and saves you from such surprises
>
> That's a myth. decimal.Decimal *is* a floating point value

That's misleading: Decimal uses *a* floating-point representation, but
not the one commonly referred to. That is, Decimal does not use IEEE-754
floating point.

> and is subject to *exactly* the same surprises as binary floats,

Since those “surprises” are the ones inherent to *decimal*, not binary,
floating point, I'd say it's also misleading to refer to them as
“exactly the same surprises”. They're barely surprises at all, to
someone raised on decimal notation.

This makes the Decimal functionality starkly different from the built-in
‘float’ type, and it *does* save you from the rather-more-surprising
behaviour of the ‘float’ type. This is not mythical.

-- 
 \     “Guaranteed to work throughout its useful life.” —packaging for |
  `\                                          clockwork toy, Hong Kong |
_o__)                                                                  |
Ben Finney




More information about the Python-list mailing list