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

mensanator at aol.com mensanator at aol.com
Fri Jul 13 17:27:13 EDT 2007


On Jul 13, 1:20 pm, Wayne Brehaut <wbreh... at mcsnet.ca> wrote:
> On Mon, 09 Jul 2007 23:51:25 -0700, "mensana... at aol.com"
>
>
>
>
>
> <mensana... at aol.com> wrote:
> >On Jul 9, 11:42?pm, Paul McGuire <p... at austin.rr.com> wrote:
> >> On Jul 9, 11:21 pm, "Jim Langston" <tazmas... at rocketmail.com> wrote:> In Python 2.5 on intel, the statement
> >> > 2**2**2**2**2
> >> > evaluates to>>> 2**2**2**2**2
>
> >> > 200352993040684646497907235156025575044782547556975141926501697371089405955  63114
> >> > 530895061308809333481010382343429072631818229493821188126688695063647615470  29165
> >> > 041871916351587966347219442930927982084309104855990570159318959639524863372  36720
>
> >> <snip>
>
> >> Exponentiation is right associative, so this is the same as:
>
> >> 2**(2**(2**(2**2)))
> >> 2**2**2**4
> >> 2**2**16
> >> 2**65536
>
> >> 2=10**0.3010, so 2**65536 is approx 10**19726
>
> >> There are 19730 digits in your answer,
>
> >>>> import gmpy
> >>>> n = 2**2**2**2**2
> >>>> gmpy.numdigits(n)
> >19729
>
> >Did you count the 'L'?
>
> numdigits(n)?

>>> import gmpy
>>> help(gmpy.numdigits)
Help on built-in function numdigits in module gmpy:

numdigits(...)
    numdigits(x[,base]): returns length of string representing x in
    the given base (2 to 36, default 10 if omitted or 0); the value
    returned may sometimes be 1 more than necessary; no provision
    for any 'sign' characte, nor leading '0' or '0x' decoration,
    is made in the returned length.  x must be an mpz, or else gets
    coerced into one.

>
> What?  'L' is a digit in Python?  

No, but it's a character. Don't you know the difference
between str() and repr()?

>>> n = 2**2**2**2**2
>>> m = -n
>>> len(str(n))
19729
>>> len(repr(n))
19730
>>> len(str(m))
19730
>>> len(repr(m))
19731

So why would you ever do it the stupid way, when it's
just as easy to get these things right?

>>> gmpy.numdigits(n)
19729
>>> gmpy.numdigits(m)
19729

> I'm going back to Fortran!

Sounds like you never left.

>
> wwwayne
>
> >>so this seems to be at least in
> >> the ball park.
>
> >> -- Paul




More information about the Python-list mailing list