我在使用Python计算复数的平方根时遇到了一些混乱的行为。运行以下代码:
I have run across some confusing behaviour with square roots of complex numbers in python. Running this code:
from cmath import sqrt
a = 0.2
b = 0.2 + 0j
print(sqrt(a / (a - 1)))
print(sqrt(b / (b - 1)))
输出结果
0.5j
-0.5j
类似的事情也会发生在
print(sqrt(-1 * b))
print(sqrt(-b))
看起来这些语句应该给出相同的答案吗?
0.5i
,而第二对(链接 和链接) 都应该是0.447214...i
。cmath.sqrt()
的源代码在这里。 - Jens