gpt4 book ai didi

python - 为什么 NumPy.exp 显示的结果与 C 中的 exp 不同

转载 作者:行者123 更新时间:2023-11-30 18:49:55 28 4
gpt4 key购买 nike

我正在将一些 Python 代码转换为 C 代码。

下面的复数输出上的 Python NumPy exp (6.12323399574e-17-1j)k=1l=4

numpy.exp(-2.0*1j*np.pi*k/l)

我将其转换为 C 代码,如下所示。但输出是1.000000

#include <complex.h>
#define PI 3.1415926535897932384626434
exp(-2.0*I*PI*k/l)

我错过了什么?

最佳答案

您必须使用cimagcreal来打印数据。

C 版本:

#include <stdio.h>
#include <complex.h>
#include <tgmath.h>


int main(){
int k=1;
int l=4;
double PI = acos(-1);
double complex z = exp(-2.0*I*PI*k/l);
printf(" %.1f%+.1fj\n", creal(z), cimag(z));
return 0;
}

输出:

0.0-1.0j

Python 版本:

import numpy as np

k=1
l=4
z = np.exp(-2.0*1j*np.pi*k/l)
print(z)

输出:

6.12323399574e-17-1j

关于python - 为什么 NumPy.exp 显示的结果与 C 中的 exp 不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41479174/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com