On 02/21/2013 02:33 PM, Schizoid Man wrote:
Hi there,

I run the following code in Python 3.3.0 (on a Windows 7 machine) and
Python 2.7.3 on a Mac and I get two different results:

result1 = []
result2 = []
for a in range(2,101):
    for b in range(2,101):
        result1.append(math.pow(a,b))
        result2.append(a**b)
result1 = list(set(result1))
result2 = list(set(result2))
print (len(result1))
print (len(result2))

On the Windows box, I get 9183 for on both lines. However, on the Mac I
get 9220 and 9183. Why this difference? Is there some sort of precision
subtlety I'm missing between ** and math.pow()?

Thank you.

I assumed this was some difference between Python 2.x and 3.x. However, on my 2.7.3 on Linux, I also get 9183 both times.

However, there is an important inaccuracy in math.pow, because it uses floats to do the work. If you have very large integers, that means some of them won't be correct. The following are some examples for 2.7.3 on Linux:

 a  b  math.pow(a,b)       a**b
 3 34 1.66771816997e+16 16677181699666569
 3 35 5.0031545099e+16 50031545098999707
...
 5 23 1.19209289551e+16 11920928955078125

The built-in pow, on the other hand, seems to get identical answers for all these cases. So use pow() instead of math.pow()

One other test:

diff = set(map(int, result1)).symmetric_difference(set(result2))
if diff:
    print diff
    print len(diff)

shows me a diff set of 15656 members.  One such member:

13552527156068805425093160010874271392822265625000000000000000000000000000000000000000000000000000000000000000000L

Notice how using floats truncated lots of the digits in the value?
--
DaveA
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to