Re: Complex variable assignment

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux