power TypeErrors

Carl Banks imbosol at vt.edu
Tue Nov 5 16:15:09 EST 2002


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. 
> 
> 
> 
> I'm running code such as: 
> 
> 1 ** -2
> -1.2 ** 0.5
> 
> and there is no type error.
> 
> How do I generate a type error?


(-1.2) ** 0.5

It seems strange at first, but the ** operator is stronger than the
negative sign: Python sees "-1.2 ** 0.5" as "-(1.2**0.5)".  Of course,
that's the convention in mathematical notation.

1 ** -2 does give me a type error.


-- 
CARL BANKS



More information about the Python-list mailing list