python计算对数很慢_Python3:计算复杂的指数和对数

math.exp() doesn't work for complex numbers:

>>> math.exp (math.pi*1j)

Traceback (most recent call last):

File "", line 1, in

TypeError: can't convert complex to float

This doesn't hurt me much, as ** works as intended:

>>> math.e ** (math.pi*1j)

(-1+1.2246467991473532e-16j)

Now the problem is with logarithms. math.log doesn't work for negative numbers:

>>> math.log(-1)

Traceback (most recent call last):

File "", line 1, in

ValueError: math domain error

(Expected result: (0+3.141592653589793j))

How can I calculate a logarithm in python whose result is complex?

(Preferably without implementing it myself)

解决方案

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 stands for Complex Math.

cmath has most of the same interface as math, so for your example you could just do the following:

import cmath

cmath.log(-1)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值