我有一个用MATLAB编码的算法,其中包含某个值的复杂弧余弦(计算所需的arccos为15,大约为3.4i)。我想编写在Windows 7 PC上运行的C或C ++对应代码。实际上,我想将其生成为使用Visual Studio C ++编译的mex函数。

我包含了“ complex.h”并使用了cacosf函数(复杂的arccos返回float _Complex),但由于Visual C ++编译器不支持“ complex.h”,因此无法将其编译为mex函数。但是,mex文件可以将库作为输入,因此我可以使用MATLAB确实支持的另一个编译器来编译c代码(例如mingw,我将其与gnumex实用程序集成到matlab中。)我下载了Bloodshed C ++ IDE,该后端在后台使用mingw,我可以编译我的C ++代码。以下C ++代码代表与我的目标相似的操作:

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

int main() {
    float _Complex myComplex;
    myComplex = cacosf(5);
    printf("Complex number result of acos(5) is : %f + %fi \r\n",crealf(myComplex),cimagf(myComplex));
    return 0;
}


输出应为:


  
    acos(5)的复数结果是:0.000000 + -2.292432i
  


但是我明白了


  
    acos(5)的复数结果是:-1。#IND00,-0.000000
  


当我使用Eclipse CDT Luna在Ubuntu 14.04计算机上用Linux GCC编译C ++代码时,我得到了

输出应为:


  
    acos(5)的复数结果是:0.000000 + -2.292432i
    我哪里错了?为什么我不能在Windows + mingw安装程序中编译此代码?
  


注意:使用mingw时,我可以将cacosf(0)计算为1.570796 + -0.000000。

最佳答案

您正在使用什么版本的mingwrt?使用mingwrt-3.21.1,以下对我有用(在Linux主机上交叉编译,并在wine下运行):

$ cat foo.c
#include <stdio.h>
#include <complex.h>

int main()
{
  double _Complex Z = cacos(5.0);
  printf( "arcos(5) = (%g, %gi)\n", __real__ Z, __imag__ Z );
  return 0;
}

$ mingw32-gcc -o foo.exe foo.c

$ ./foo.exe
arcos(5) = (0, -2.29243i)


这似乎与您的预期结果一致。但是,如果使用早于mingwrt的任何mingwrt-3.21版本(并且对mingwrt-4.x的破译越少越好),则由于任意认为任何纯正的cacos()参数值都会导致一个已知的错误。大于(1.0, 0.0i)会超出有效域(例如acos()的实际部分),这将产生您报告的结果。

关于c - C中的cacosf(复杂弧余弦)函数返回不确定,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31335827/

10-10 09:23