This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Complex variable assignment
Thank you. I wish the documentation was a bit more clear on this.
-Amin Al-Juffali
ajuffali@mac.com
On Feb 28, 2012, at 1:03 AM, Marc Glisse wrote:
> 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