power TypeErrors

anton wilson anton.wilson at camotion.com
Tue Nov 5 16:39:44 EST 2002


On Tuesday 05 November 2002 04:10 pm, Brad Hards wrote:
> On Wed, 6 Nov 2002 08:02, anton wilson wrote:
> > I'm trying to understand the possible type errors for **
> >
> > The language reference states that
> >
> >
> > The power operator has the same semantics as the built-in pow() function,
> > when called with two arguments: it yields its left argument raised to the
> > power of its right argument. The numeric arguments are first converted to
> > a common type. The result type is that of the arguments after coercion;
> > if the result is not expressible in that type (as in raising an integer
> > to a negative power, or a negative floating point number to a broken
> > power), a TypeError exception is raised.
>
> Looks like the docs are outdated. I'm not sure about the "broken power"
> bit.
>


It actually causes an error from a source file, only if i use a variable 
argument to **.

so this fails:

x = -1
x ** 0.5 

but 

-1 ** -0.5 doesn't. 


> [bradh at localhost USB-guide-head]$ python
> Python 1.5.2 (#1, Jul  5 2001, 03:02:19)  [GCC 2.96 20000731 (Red Hat Linux
> 7.1 2 on linux-i386
> Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>
> >>> 1**-2
>
> Traceback (innermost last):
>   File "<stdin>", line 1, in ?
> ValueError: integer to the negative power
>
> >>> -1.2**0.5
>
> -1.09544511501
>
> bradh at squirt python $ python
> Python 2.2.1 (#1, Oct  4 2002, 09:50:49)
> [GCC 2.95.3 20010315 (release)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>
> >>> 1**-2
>
> 1.0
>
> >>> -1.2 ** 0.5
>
> -1.0954451150103321
>
> Brad




More information about the Python-list mailing list