Confusing math problem

Ian Kelly ian.g.kelly at gmail.com
Thu Feb 21 15:42:49 EST 2013


On Thu, Feb 21, 2013 at 12:33 PM, Schizoid Man
<schiz_man at 21stcentury.com> 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()?

math.pow is basically a wrapper for the C standard pow function, which
operates on doubles.  The difference you're seeing is probably a
difference in implementation in the platform's C library.  The **
operator on the other hand is implemented separately as a Python
built-in and operates on any numeric data type.



More information about the Python-list mailing list