[Python-ideas] Python Numbers as Human Concept Decimal System

Stefan Krah stefan at bytereef.org
Sun Mar 9 13:36:41 CET 2014


Steven D'Aprano <steve at pearwood.info> wrote:
> Note that this round-trips back to float correctly:
> 
> py> assert float(d) == 9017.010981286436

Many inexact values map to the same float.


> So I must admit, I'm not sure where the extra digits come from:
> 
> Decimal('9017.01098128643570817075669765472412109375')
> Decimal('9017.010981286435708170756694')

Without looking at your calculation, I guess it is rounding:

>>> 9017.010981286436.as_integer_ratio()
(2478577105427079, 274877906944)
>>> Decimal(2478577105427079) / 274877906944
Decimal('9017.010981286435708170756698')
>>> getcontext().prec = 100
>>> Decimal(2478577105427079) / 274877906944
Decimal('9017.01098128643570817075669765472412109375')



Stefan Krah




More information about the Python-ideas mailing list