[英]Accurate roots of negative numbers in Python
I'm looking for a way to accurately compute roots of negative numbers in Python. 我正在寻找一种在Python中精确计算负数根的方法。 I believe that the inaccuracies I see are related to floating point , but it seems like there should be a way to get the right answer for something as simple as this:
我相信我看到的错误与浮点数有关 ,但似乎应该有一种方法可以为诸如此类的简单问题找到正确的答案:
>>(-1+0j)**0.1
(0.9510565162951535+0.3090169943749474j)
The answer I expect in this case is (0+1j)
. 我期望在这种情况下的答案是
(0+1j)
。 Though raising Python's response to the tenth does come close to -1, I am looking for an answer obtained from a more precise method, ie, one that when raised to the tenth equals exactly -1, not a number really close to -1. 尽管将Python的响应提高到十分之一确实接近-1,但我仍在寻找一种从更精确的方法获得的答案,即,当将其提高到十分之一时恰好等于-1,而不是真正接近-1的数字。
Is there a way to do this correctly with either a native Python library, or sympy/numpy/scipy etc? 有没有办法使用本机Python库或sympy / numpy / scipy等正确执行此操作?
-1 has not one but 10 complex tenth roots. -1没有十个复数的十个根。 You got only one of them.
您只有其中之一。 If
a
is your returned root, a ** 5
is also a root: 如果
a
是您返回的根,则a ** 5
也是根:
(a ** 5) ** 10 = (a ** 10) ** 5 = (-1) ** 5 = -1
(a ** 5)** 10 =(a ** 10)** 5 =(-1)** 5 = -1
But if you run: 但是,如果您运行:
a = (-1 + 0j) ** 0.1
print(a)
print(a ** 5)
you'll get: 你会得到:
(0.951056516295+0.309016994375j)
(1.11022302463e-16+1j)
You see a ** 5
is very close to 1j
. 您会看到
a ** 5
非常接近1j
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.