0

I've written an application using Xcode (in C), which relies heavily on the use of complex numbers. i.e. :

#include <complex.h>
...

float a, b;
complex float z;
z = a + I * b;

... etc

Now, this notation for complex numbers works great in Xcode, and was the perfect solution when building OS X compatible versions of the application. However, now I have a need to build a Windows version, so I have imported the same code into Visual Studio 2015. But unfortunately, this notation does not work, as VS's implementation of complex numbers is very different. And after researching my issue, I have failed to find any helpful information regarding how to handle complex numbers in such a manner, as to compile successfully on both IDEs.

This is perhaps due to my poor understanding of this C99 standard or maybe it is simply not possible. But, while I feel I could rewrite the C code using VS's own way of handling complex numbers, this would not be ideal, as I now plan to develop the application further for both OSs. And it would take too much time to write two versions of the same code in parallel.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • 1
    So you use an Oldtimer and complain it does not drive as fast as a modern sports car. MSVC is not standard compliant. Use a compliant compiler. – too honest for this site Jul 08 '16 at 12:30
  • Possible duplicate of [C99 complex support with visual studio](http://stackoverflow.com/questions/1063406/c99-complex-support-with-visual-studio). Tl,dr the best way I could think of is to replace all operators and datatypes with preprocessor defines/macors, and then implement you own handling for VS. – vgru Jul 08 '16 at 12:37
  • Can you use C++? I believe that Visual Studio's C++ compiler is more compliant then their C compiler (if I recall correctly VS C compiler is still frozen at C90). I don't know how hard it would be to port the Mac version to C++ (and XCode support for C++, but I suspecting that it is good). Failing that you could attempt to install gcc on windows (see http://mingw-w64.org/doku.php) and us that instead of a non-standard complaint visual studion. – thurizas Jul 08 '16 at 13:35
  • 1
    @thurizas: The question is clearly tagged C. It is useless to ask if he can use Python, Assembler, Brainfuck or any other language. – too honest for this site Jul 08 '16 at 17:45
  • @olaf: that is why I asked if he could use C++. If he is required to use C and also required to use MS visual studio he is out of luck. If he is required to use C, on Windows and use C99 features he is going to have to move away from Microsoft (to either Clang or gcc). Also, moving to C++ might be an option he has not considered yet, especially given the poor support for C99 and beyond with Microsoft compiler. – thurizas Jul 10 '16 at 01:52
  • @thurizas: It is useless just to support some rubbish, ancient compiler to rewrite code in a new language which otherwise works perfectly. So the only way would be either to obfuscate it with macros or - the right way - use a modern (i.e. less than ca. 15 years old language standard) tool chain. – too honest for this site Jul 10 '16 at 15:11

1 Answers1

1

as shown in: 'Compiling C code in Visual Studio 2013 with complex.h library'

_Dcomplex dc1 = {3.0, 2.0};

for the variable declaration.

From looking inside VS2013's "complex.h" header, it seems that Microsoft decided on their own implementation for C complex numbers. You'll have to implement your own arithmetical operators using the real() and imag() functions, i.e.:

double real_part = real(dc1) + real(dc2);
double imag_part = imag(dc1) + imag(dc2);
_Dcomplex result = {real_part, imag_part};

I doubt that microsoft has corrected the complex.h header, so you should examine the complex.h header file for the details for the float types

Community
  • 1
  • 1
user3629249
  • 16,402
  • 1
  • 16
  • 17
  • 1
    "their own implementation for C complex numbers" - If they did it their own way, they are not "C complex numbers". The C standard is clear about how to use complex numbers, any other way is not compliant. The correct way would be to trash MSVC. – too honest for this site Jul 10 '16 at 15:12