cacosf (Complex arc cos) function in C returns indefinite

I have an algorithm coded in MATLAB, which contains complex arc cos of some value (computation requires arccos of 15, which is approximately 3.4i). I want to code C or C++ counterpart of this code running on my Windows 7 PC. Actually, I want to produce it as a mex function compiled with Visual Studio C++.

I included “complex.h” and used cacosf function (complex arccos returning float _Complex) but I could not compile it as a mex function because Visual C++ compiler does not have “complex.h” support. However, mex file can take libraries as input, so I can compile my c code with another compiler that MATLAB does support (for example mingw, I integrated it to matlab with gnumex utility.) I downloaded Bloodshed C++ IDE which uses mingw at backend, I can compile my c++ code. The following C++ code represents a similar operation to my goal:

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

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

The output should be:

Complex number result of acos(5) is : 0.000000 + -2.292432i

However I get

Complex number result of acos(5) is : -1.#IND00, -0.000000

When I compile my C++ code with Linux GCC on Ubuntu 14.04 computer with Eclipse CDT Luna I get

The output should be:

Complex number result of acos(5) is : 0.000000 + -2.292432i
Where can I be wrong? Why can’t I compile this code in Windows + mingw setup?

Note: I can compute cacosf(0) as 1.570796 + -0.000000 when I use mingw.


Source: gcc

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.