vite apprentice
vite apprentice

Reputation: 41

Arithmetic operations of complex numbers in c

I'm interested in how I can perform arithmetic operations of complex numbers in the c language using a standard library the win32. For example:

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

int main(int argc, char **argv)
{
    _Dcomplex a;
    _Dcomplex b = 2. + 3. * _Complex_I;
    _Dcomplex c = a + b;

    return 0;
}

As far as I understand, the C99 standard is not supported by Microsoft. How do I bypass the restrictions? Thanks

Upvotes: 1

Views: 737

Answers (1)

gaurav
gaurav

Reputation: 136

Sadly, earlier Microsoft was not eager to provide C99 libraries support because of its comparatively smaller userbase and pain in porting mordern gcc based code to MSVC. It's a matter of priority.

But, on the demand of developers, they have implemented many C99 libraries in Visual Studio 2013 and above that are mentioned here :

https://blogs.msdn.microsoft.com/vcblog/2013/07/19/c99-library-support-in-visual-studio-2013/

So, the code in C can be written as :

#include <stdio.h>      /* Standard Library of Input and Output */
#include <complex.h>    /* Standard Library of Complex Numbers */

int main() {

double complex z1 = 1.0 + 3.0 * I;
double complex z2 = 1.0 - 4.0 * I;

printf("Initial values: Z1 = %.2f + %.2fi \nZ2 = %.2f %+.2fi\n", creal(z1), cimag(z1), creal(z2), cimag(z2));

double complex sum = z1 + z2;
printf("Sum: Z1 + Z2 = %.2f %+.2fi\n", creal(sum), cimag(sum));

double complex diff = z1 - z2;
printf("Diff: Z1 - Z2 = %.2f %+.2fi\n", creal(difference), cimag(difference));

double complex product = z1 * z2;
printf("Product: Z1 x Z2 = %.2f %+.2fi\n", creal(product), cimag(product));

double complex quotient = z1 / z2;
printf("Quotient: Z1 / Z2 = %.2f %+.2fi\n", creal(quotient), cimag(quotient));

double complex conjugate = conj(z1);
printf("Conjugate of Z1 = %.2f %+.2fi\n", creal(conjugate), cimag(conjugate));

return 0;

}

functions like cos(), exp() and sqrt() must be replaced with their complex forms, e.g. ccos(), cexp(), csqrt() and they work just fine.

Other workaround could be to completely scrape the MS compiler and use intel compiler(which is more enlightened) which works in Visual C.

A more sensible approach is to move over to Intel CC or gcc, and use Eclipse for your programming environment. Portability of code across Windows-Linux-Solaris-AIX-etc is usually important and that is not at all supported by MS tools, unfortunately.

Upvotes: 1

Related Questions