Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #42363

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

Newsgroups comp.lang.python
Date 2013-03-31 05:07 -0700
References <8e43bc87-e822-4bb3-b9ef-ccd489da8bf3@googlegroups.com> <5157e6cc$0$29974$c3e8da3$5496439d@news.astraweb.com>
Message-ID <8276eff6-9e5c-4060-b9e8-94fab606206f@googlegroups.com> (permalink)
Subject Re: Why does 1**2**3**4**5 raise a MemoryError?
From morphex <morphex@gmail.com>

Show all headers | View raw


Aha, OK.  Thought I found a bug but yeah that makes sense ;)

While we're on the subject, wouldn't it be nice to have some cap there so that it isn't possible to more or less block the system with large exponentiation?

On Sunday, March 31, 2013 9:33:32 AM UTC+2, 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@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
> 
> => 1**(something absolutely humongous)
> 
> => 1
> 
> 
> 
> except of course you get a MemoryError in calculating the intermediate 
> 
> values.
> 
> 
> 
> In other words, unlike you or me, Python is not smart enough to realise 
> 
> that 1**(...) is automatically 1, it tries to calculate the humongous 
> 
> intermediate result, and that's what fails.
> 
> 
> 
> For what it's worth, that last intermediate result (two to the power of 
> 
> the 489-digit number) has approximately a billion trillion trillion 
> 
> trillion trillion trillion trillion trillion trillion trillion trillion 
> 
> trillion trillion trillion trillion trillion trillion trillion trillion 
> 
> trillion trillion trillion trillion trillion trillion trillion trillion 
> 
> trillion trillion trillion trillion trillion trillion trillion trillion 
> 
> trillion trillion trillion trillion trillion trillion digits.
> 
> 
> 
> (American billion and trillion, 10**9 and 10**12 respectively.)
> 
> 
> 
> 
> 
> 
> 
> -- 
> 
> Steven

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Why does 1**2**3**4**5 raise a MemoryError? morphex <morphex@gmail.com> - 2013-03-30 23:56 -0700
  Re: Why does 1**2**3**4**5 raise a MemoryError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-03-31 07:33 +0000
    Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <davea@davea.name> - 2013-03-31 03:48 -0400
    Re: Why does 1**2**3**4**5 raise a MemoryError? morphex <morphex@gmail.com> - 2013-03-31 05:07 -0700
      Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <d@davea.name> - 2013-03-31 08:43 -0400
        Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-03-31 09:15 -0400
          Re: Why does 1**2**3**4**5 raise a MemoryError? Jason Swails <jason.swails@gmail.com> - 2013-03-31 10:03 -0400
      Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-03-31 09:04 -0400
      Re: Why does 1**2**3**4**5 raise a MemoryError? Tim Roberts <timr@probo.com> - 2013-04-01 21:45 -0700
        Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-02 17:16 +1100
        Re: Why does 1**2**3**4**5 raise a MemoryError? Dan Sommers <dan@tombstonezero.net> - 2013-04-02 12:56 +0000
    Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-03-31 08:59 -0400
  Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <davea@davea.name> - 2013-03-31 03:34 -0400
    Re: Why does 1**2**3**4**5 raise a MemoryError? "Alex" <foo@email.invalid> - 2013-03-31 22:06 +0000
      Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-01 09:28 +1100
        Re: Why does 1**2**3**4**5 raise a MemoryError? "Alex" <foo@email.invalid> - 2013-04-01 00:39 +0000
          Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-01 11:58 +1100
          Re: Why does 1**2**3**4**5 raise a MemoryError? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-04-01 04:16 +0000
            Re: Why does 1**2**3**4**5 raise a MemoryError? Roy Smith <roy@panix.com> - 2013-04-01 07:48 -0400
          Re: Why does 1**2**3**4**5 raise a MemoryError? Nobody <nobody@nowhere.com> - 2013-04-02 08:40 +0100
      Re: Why does 1**2**3**4**5 raise a MemoryError? Chris Angelico <rosuav@gmail.com> - 2013-04-01 09:32 +1100
      Re: Why does 1**2**3**4**5 raise a MemoryError? Dave Angel <davea@davea.name> - 2013-03-31 18:34 -0400

csiph-web