Confusing math problem

Peter Pearson ppearson at nowhere.invalid
Thu Feb 21 22:59:54 CET 2013


On Fri, 22 Feb 2013 08:23:27 +1100, Chris Angelico <rosuav at gmail.com> wrote:
> On Fri, Feb 22, 2013 at 7:49 AM, Dave Angel <davea at davea.name> wrote:
>> However, if I do:
>>
>> print 3**60, "\n", int(math.pow(3,60)), "\n", pow(3,60)
>>
>>
>> I get:
>>
>> 42391158275216203514294433201
>> 42391158275216203520420085760
>> 42391158275216203514294433201
>>
>>
>> and the middle one is the one that's wrong.
>
> In theory, a float should hold the nearest representable value to the
> exact result. Considering that only one operation is being performed,
> there should be no accumulation of error. The integer results show a
> small number (618) of collisions, eg 2**16 and 4**8; why should some
> of those NOT collide when done with floating point? My initial thought
> was "Oh, this is comparing floats for equality", but after one single
> operation, that should be not a problem.

Does this help explain it?

>>> print hex(int(math.pow(3,60))); print hex(3**60)
0x88f924eeceeda80000000000L
0x88f924eeceeda7fe92e1f5b1L


-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list