简体   繁体   English

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

[英]Why does NumPy.exp show a different result than exp in C

I am converting some Python code to C code. 我正在将一些Python代码转换为C代码。

Below Python NumPy exp on complex number outputs (6.12323399574e-17-1j) for k=1 , l=4 . 在Python NumPy exp以下,复数输出(6.12323399574e-17-1j)对于k=1l=4

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

I convert it to C code like below. 我将其转换为C代码,如下所示。 But the output is 1.000000 . 但是输出是1.000000

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

What am I missing? 我想念什么?

You must use cimag and creal to print the data. 您必须使用cimagcreal来打印数据。

C version: 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;
}

Output: 输出:

0.0-1.0j

Python Version: Python版本:

import numpy as np

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

Output: 输出:

6.12323399574e-17-1j

Here's the right C code to print out your answer: 这是正确的C代码,可以打印出您的答案:

    x = cexp(-2.0*I*PI*0.25);
    printf("%f + i%f\n", creal(x), cimag(x));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM