Why does 1**2**3**4**5 raise a MemoryError?

Dave Angel davea at davea.name
Sun Mar 31 09:48:02 CEST 2013


On 03/31/2013 03:33 AM, Steven D'Aprano wrote:
> On Sat, 30 Mar 2013 23:56:46 -0700, morphex wrote:
>
>> Hi.
>>
>> I was just doodling around with the python interpreter today, and here
>> is the dump from the terminal:
>>
>> morphex at laptop:~$ python
>> Python 2.7.3 (default, Sep 26 2012, 21:53:58) [GCC 4.7.2] on linux2
>> Type "help", "copyright", "credits" or "license" for more information.
>>>>> 1**2
>> 1
>>>>> 1**2**3
>> 1
>>>>> 1**2**3**4
>> 1L
>>>>> 1**2**3**4**5
>> Traceback (most recent call last):
>>    File "<stdin>", line 1, in <module>
>> MemoryError
>>>>>
>>>>>
>> Does anyone know why this raises a MemoryError?  Doesn't make sense to
>> me.
>
> Because exponentiation is right-associative, not left.
>
> 1**2**3**4**5 is calculated like this:
>
> 1**2**3**4**5
> => 1**2**3**1024
> => 1**2**373...481  #  489-digit number

Oops, you're right, it's 489.  I figured 488 but was wrong.




-- 
DaveA



More information about the Python-list mailing list