Comparing float and decimal

Mark Dickinson dickinsm at gmail.com
Thu Sep 25 06:05:13 EDT 2008


On Sep 25, 8:55 am, Tim Roberts <t... at probo.com> wrote:
> Marc 'BlackJack' Rintsch <bj_... at gmx.net> wrote:
> >0.1 actually is
>
> >In [98]: '%.50f' % 0.1
> >Out[98]: '0.10000000000000000555111512312578270211815834045410'
> >?
>
> Actually, it's not.  Your C run-time library is generating random digits
> after it runs out of useful information (which is the first 16 or 17
> digits).  0.1 in an IEEE 784 double is this:
>
>      0.100000000000000088817841970012523233890533447265625

I get (using Python 2.6):

>>> n, d = 0.1.as_integer_ratio()
>>> from decimal import Decimal, getcontext
>>> getcontext().prec = 100
>>> Decimal(n)/Decimal(d)
Decimal('0.1000000000000000055511151231257827021181583404541015625')

which is a lot closer to Marc's answer.  Looks like your float
approximation to 0.1 is 6 ulps out.  :-)

Mark



More information about the Python-list mailing list