[Python-Dev] return type of complex (original) (raw)

Larry Hastings larry at hastings.org
Mon Oct 22 13:34:26 CEST 2012


On 10/21/2012 09:35 PM, Steven D'Aprano wrote:

so as far as I can tell, the only way you could accidentally get a complex number without expecting one is by exponentiation, either by ** or the builtin pow. This includes square roots. Exponentiation isn't as common as the basic four arithmetic operators, but it is hardly exotic.

Any time you have a non-integer power and a negative base, you will get a complex number.

No, only via the builtin pow.

% python3
Python 3.3.0 (default, Sep 29 2012, 22:42:55)
[GCC 4.6.3] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> import math
 >>> pow(-1, 0.5)
(6.123031769111886e-17+1j)
 >>> -1 ** 0.5
-1.0
 >>> math.pow(-1, 0.5)
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ValueError: math domain error
 >>>

//arry/

-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://mail.python.org/pipermail/python-dev/attachments/20121022/e220509c/attachment.html>



More information about the Python-Dev mailing list