Python vs R/Matlab 复数指数的实现

Python vs R/Matlab implementation of exponential of a complex number

我在 Python 和 R

中实现这个简单的计算时遇到了不同的结果

e^(2*pi*1j) = cos(2*pi) + j*sin(2*pi) = 1

在 R 中,它给出了预期的结果

j   <- complex(real = 0, imaginary = 1)
exp(2*pi*j)
>>1 -0j

而在 Python

import math
import cmath

cmath.exp(2*math.pi*1j)
>>(1-2.4492935982947064e-16j)
## Also tried this
math.e ** (2*math.pi*1j)
>>(1-2.4492935982947064e-16j)

我在 Python 中实施它时做错了什么?

你没有做错任何事。虚部很小,这是不可避免的浮点不精确的结果。 R和Python的区别仅在于输出的表示; R(最初设计为交互式统计分析的平台)没有显示非常小的虚部,但它仍然存在:

j   <- complex(real = 0, imaginary = 1)
> exp(2*pi*j)
[1] 1-0i
> Im(exp(2*pi*j))
[1] -2.449294e-16
print(Im(exp(2*pi*j)),digits=22)
[1] -2.449293598294706414348e-16