2**2**2**2**2 wrong? Bug?

David Jones david.l.jones at gmail.com
Wed Jul 11 13:04:06 EDT 2007


On Jul 10, 12:47 am, "Jim Langston" <tazmas... at rocketmail.com> wrote:
> "Paul Rubin" <http://phr...@NOSPAM.invalid> wrote in message
>
> news:7xbqek7ty1.fsf at ruckus.brouhaha.com...
>
> > "Jim Langston" <tazmas... at rocketmail.com> writes:
> >> In Python 2.5 on intel, the statement
> >> 2**2**2**2**2
> >> evaluates to
> >> >>> 2**2**2**2**2
>
> > I get the same number from hugs--why do you think it might be wrong?
>
> 2**2 = 4
> 4**2 = 16
> 16**2 = 256
> 256**2 = 65536
> 65536**2 = 4294967296
>
> In fact, if I put (2**2)**2**2**2
> it comes up with the correct answer, 4294967296

Actually, the "correct" answer (even by your own demonstration) is
65536. Assuming left-associativity, i.e., (((2**2)**2)**2)**2, python
returns 65536. The answer of 4294967296 is actually
((((2**2)**2)**2)**2)**2, which is one extra raise-to-the-power-of-two
instruction.

The statement (2**2)**2**2**2 is the same as 4**16, following right-
associativity rules, which just happens to be the same as
((((2**2)**2)**2)**2)**2.

David




More information about the Python-list mailing list