Comparing float and decimal

Tim Roberts timr at probo.com
Thu Sep 25 09:55:52 CEST 2008


Marc 'BlackJack' Rintsch <bj_666 at gmx.net> wrote:
>
>On Tue, 23 Sep 2008 07:08:07 -0700, Michael Palmer wrote:
>
>>> > This seems to break the rule that if A is equal to B and B is equal
>>> > to C then A is equal to C.
>>>
>>> I don't see why transitivity should apply to Python objects in general.
>> 
>> Well, for numbers it surely would be a nice touch, wouldn't it. May be
>> the reason for Decimal to accept float arguments is that irrational
>> numbers or very long rational numbers cannot be converted to a Decimal
>> without rounding error, and Decimal doesn't want any part of it. Seems
>> pointless to me, though.
>
>Is 0.1 a very long number?  Would you expect ``0.1 == Decimal('0.1')`` to 
>be `True` or `False` given that 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
-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-list mailing list