Complex variable assignment

Marc Glisse marc.glisse@inria.fr
Mon Feb 27 22:03:00 GMT 2012


On Tue, 28 Feb 2012, Amin Al-Juffali wrote:

> I have wrote a simple program that assigns a complex number a number in the form of a + b*I
> and when I print it the numbers I noticed loss of precision. It does not seem it is recognizing it as a double.
>
> #include <stdio.h>
> #include <math.h>
> #include <complex.h>
>
> int main (int argc, const char * argv[]) {
>
> 	complex double  y[1][1];
>
> 	y[0][0] = (double) -0.107692307692308 + (double) 0.061538461538462*I;

No need for the "(double)" here.

>
> 	printf("\n------------------- Complex Array Assignment ---------------------------------------------------\n");
>        printf("\n(%+10.15f,%+10.15f)", crealf(y[0][0]), cimagf(y[0][0]));

See the 'f' at the end of crealf and cimagf? It is 'f' as in "float". Use 
creal and cimag instead.

> 	printf("\n");
> 	printf("\n\n----------------------------------------------------------------------------------------------\n");
>
>
>    return 0;
> }
>
> This is what you will get:
>
> ------------------- Complex Array Assignment ---------------------------------------------------
>
> (-0.107692308723927,+0.061538461595774)
>
>
> ----------------------------------------------------------------------------------------------

-- 
Marc Glisse



More information about the Gcc-help mailing list