Python指数运算符和复数 [英] Python exponent operator and complex numbers
问题描述
为什么python为什么 print(type(-1 ** 0.5))
返回 float
而不是 complex
?
Why does python does print(type(-1**0.5))
return float
instead of complex
?
在数学上,始终将float的负整数的平方根视为复数.python指数运算符如何支持获取 complex
数字?
Getting the square root of negative integer of float always mathematically consider as complex numbers. How does python exponent operator support to get complex
number?
print(type(-1**0.5))
<type 'float'>
推荐答案
在运算的数学顺序中,指数运算先于乘法运算,一元负数作为乘法运算(乘以-1).因此您的表达式与-(1 ** 0.5)
相同,其中不包含任何虚数.
In the mathematical order of operations, exponentation comes before multiplication and unary minus counts as multiplication (by -1). So your expression is the same as -(1**0.5)
, which doesn't involve any imaginary numbers.
如果执行(-1)** 0.5
,则会在Python 2中出现错误,因为答案不是实数.如果需要复杂的答案,则需要通过执行(-1 + 0j)** 0.5
使用复杂的输入.(在Python 3中,(-1)** 0.5
将返回复杂的结果.)
If you do (-1)**0.5
you'll get an error in Python 2 because the answer isn't a real number. If you want a complex answer, you need to use a complex input by doing (-1+0j)**0.5
. (In Python 3, (-1)**0.5
will return a complex result.)
这篇关于Python指数运算符和复数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!