Confusing math problem

Dave Angel davea at
Thu Feb 21 23:33:59 CET 2013

On 02/21/2013 05:11 PM, Chris Angelico wrote:
>>  <snip>
> Note how, in each case, calculating three powers that have the same
> real-number result gives a one-element set. Three to the sixtieth
> power can't be perfectly rendered with a 53-bit mantissa, but it's
> rendered the same way whichever route is used to calculate it.

But you don't know how the floating point math library (note, it's the 
machine's C-library, not Python's that used) actually calculates that.

For example, if they were to calculate 2**64 by squaring the number 6 
times, that's likely to give a different answer than multiplying by 2 63 
times.  And you don't know how the library does it.  For any integer 
power up to 128, you can do a combination of square and multiply so that 
the total operations are never more than 13, more or less.  But if you 
then figure a = a*a  and b = b/2, and do the same optimization, you 
might not do them exactly in the same order, and therefore might not get 
exactly the same answer.

Even if it's being done in the coprocessor inside the Pentium, we don't 
have a documented algorithm for it.  Professor Kahn helped with the 
8087, but I know they've tweaked their algorithms over the years (as 
well as repairing bugs).  So it might not be a difference between Python 
versions, nor between OS's, but between processor chips.


More information about the Python-list mailing list