[Python-ideas] complex number and fractional exponent

Terry Reedy tjreedy at udel.edu
Sat Jan 26 20:09:44 CET 2013


On 1/26/2013 7:01 AM, Vito De Tullio wrote:

> There is some ideas about extending the pow() / ** operator to return
> complex number when necessary?

As other have noted, it was. But the analogy with division is not exact. 
The new // operator was added for when one wants floor(a/b).

For sqrt, one also has a choice and always has since complexes were added.
 >>> import math, cmath
 >>> math.sqrt(-1)
Traceback (most recent call last):
   File "<pyshell#1>", line 1, in <module>
     math.sqrt(-1)
ValueError: math domain error
 >>> cmath.sqrt(-1)
1j

For many purposes, such as computing standard deviation in statistics, 
one wants the exception, as negative variance indicates a calculation error.

-- 
Terry Jan Reedy




More information about the Python-ideas mailing list