J
Jonathan Fielder
Hi,
My program (below) casts a double (which is in range for a float) to a
float. As far as I know this should give me the nearest representable
float, which will loose some precision. I then test for a NAN by comparing
my float to itself (is this correct?), and I get different behaviour with
different compilers. Have I done something that is undefined, or is this a
compiler bug?
Thanks,
Jon.
GCC:
$ ./a.exe
*** NOT A NAN ***
y = 1.56723856925964355469
MS Visual C++:
$ ./single
*** NAN ***
y = 1.56723856925964360000
#include <stdio.h>
int main(void) {
/* set x to double that is in RANGE for single precision */
double x = 1.5672385729857;
/* cast double to float, loosing PRECISION */
float y = (float) x;
/* test for bad y - test for NAN */
if (y != y) {
printf("*** NAN ***\n\n");
printf("y = %10.20f\n", y);
}
else {
printf("*** NOT A NAN ***\n\n");
printf("y = %10.20f\n", y);
}
}
My program (below) casts a double (which is in range for a float) to a
float. As far as I know this should give me the nearest representable
float, which will loose some precision. I then test for a NAN by comparing
my float to itself (is this correct?), and I get different behaviour with
different compilers. Have I done something that is undefined, or is this a
compiler bug?
Thanks,
Jon.
GCC:
$ ./a.exe
*** NOT A NAN ***
y = 1.56723856925964355469
MS Visual C++:
$ ./single
*** NAN ***
y = 1.56723856925964360000
#include <stdio.h>
int main(void) {
/* set x to double that is in RANGE for single precision */
double x = 1.5672385729857;
/* cast double to float, loosing PRECISION */
float y = (float) x;
/* test for bad y - test for NAN */
if (y != y) {
printf("*** NAN ***\n\n");
printf("y = %10.20f\n", y);
}
else {
printf("*** NOT A NAN ***\n\n");
printf("y = %10.20f\n", y);
}
}