This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Complex variable assignment


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 Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]