I am using gcc 4.6.1 with mingw in windows.
I recently learned about the %a
format specifier for representing floats exactly in floating-point hex notation (in C99, apparently).
For some reason I can print a float this way just fine, but I am having trouble scanning it.
If I replace both instances of %a
, with %f
my code handles printing and scanning the the floating point number just fine.
The code seems to work just fine when I ran it on a traditional Unix system (gcc 4.2.1).
I have tried every variation I could think of with floats and doubles. I tried adding the flag -std=c99
, which seemed to change whether it treated the numeric literal as a double or float, but it did not fix my problem.
I want to know 2 things, if anyone else can duplicate this problem, and what I might do to fix it.
#include <stdio.h>
int main(){
char buffer[1000];
sprintf(buffer, "%la", 21.3930);
double x = 0;
printf("\n");
printf(buffer);
if(!sscanf(buffer, "%la", &x))
printf("\nscan failed.");
printf("\n%la", x);
return 0;
}
The output is:
0x1.5649bap+4
scan failed.
0x0.000000p+0
Help would be greatly appreciated.