Floating point comparisons with NaN values produce wrong results
Olli Saarela
olli.saarela@kcl.fi
Sun Sep 5 23:48:00 GMT 1999
Stephen L Moshier wrote:
>
> > static const char double_q_nan_bytes_be[8] = {0x7f, 0xf8, 0, 0, 0, 0,
> 0, 0};
>
> > #define double_q_nan (*(const double *)double_q_nan_bytes_be)
>
> This is not valid C language. You need to fix your test case.
A fixed version is included below. This doesn't affect the results, the
inequality comparisons (< > <= >=) still give wrong results.
#include <stdio.h>
#include <math.h>
union DoubleInit {
char bytes[8];
double f;
};
/* Quiet NaN : depends on byte order */
const union DoubleInit double_q_nan_bytes_le = {{0, 0, 0, 0, 0, 0, 0xf8,
0x7f}};
const union DoubleInit double_q_nan_bytes_be = {{0x7f, 0xf8, 0, 0, 0, 0,
0, 0}};
#define double_q_nan double_q_nan_bytes_be.f
void compare(double x, double y) {
printf("%10f < %10f %s\n", x, y, (x < y) ? "TRUE" : "FALSE");
printf("%10f <= %10f %s\n", x, y, (x <= y) ? "TRUE" : "FALSE");
printf("%10f > %10f %s\n", x, y, (x > y) ? "TRUE" : "FALSE");
printf("%10f >= %10f %s\n", x, y, (x >= y) ? "TRUE" : "FALSE");
printf("%10f == %10f %s\n", x, y, (x == y) ? "TRUE" : "FALSE");
printf("%10f != %10f %s\n", x, y, (x != y) ? "TRUE" : "FALSE");
printf("%10f == %10f %s\n", x, 0.0, (x == 0.0) ? "TRUE" : "FALSE");
printf("%10f != %10f %s\n", x, 0.0, (x != 0.0) ? "TRUE" : "FALSE");
printf("\n");
}
int main(int argc, char *argv[]) {
compare(1.0, double_q_nan);
compare(double_q_nan, 1.0);
compare(double_q_nan, double_q_nan);
return 0;
}
--
Olli Saarela, KCL - The Finnish Pulp and Paper Research Institute
Olli.Saarela@kcl.fi tel. +358-9-4371538 (office)
Tekniikantie 2, Espoo-Otaniemi fax. +358-9-464305
P.O. Box 70, FIN-02151 Espoo, Finland
More information about the Gcc-bugs
mailing list