本文介绍了Python3:计算复杂的指数和对数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
math.exp()
不适用于复数:
>>> math.exp (math.pi*1j)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: can't convert complex to float
因为 **
可以按预期工作:
>>> math.e ** (math.pi*1j)
(-1+1.2246467991473532e-16j)
现在问题出在对数上。 math.log
不适用于负数:
Now the problem is with logarithms. math.log
doesn't work for negative numbers:
>>> math.log(-1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: math domain error
(预期结果:(0 + 3.141592653589793j)
)
如何在结果复杂的python中计算对数?
(最好不要自己实现)
How can I calculate a logarithm in python whose result is complex?(Preferably without implementing it myself)
推荐答案
数学文档明确指出它不支持复数。如果您想要使用python编写的库,则应该改用。
The math documentation explicitly says that it does not support complex numbers. If you want a library in python that does, you should use cmath instead.
Cmath代表复杂数学。
Cmath stands for Complex Math.
cmath具有与数学大部分相同的接口,因此在您的示例中您可以执行以下操作:
cmath has most of the same interface as math, so for your example you could just do the following:
import cmath
cmath.log(-1)
这篇关于Python3:计算复杂的指数和对数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!