Floating point comparisons with NaN values produce wrong results

Olli Saarela olli.saarela@kcl.fi
Fri Sep 3 07:41:00 GMT 1999


Hello,

Floating point comparisons (< <= > >=) with NaN values produce wrong
results in
  GNU CPP version 2.95 19990718 (prerelease)
  hppa2.0-hp-hpux10.20

The following example should produce FALSE for all other comparisons
except !=.

Best regards,
	Olli

#include <stdio.h>
#include <math.h>

/* Quiet NaN : depends on byte order */

static const char double_q_nan_bytes_le[8] = {0, 0, 0, 0, 0, 0, 0xf8,
0x7f};
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)

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");
}

void main(int argc, char *argv[]) {
  compare(1.0, double_q_nan);
  compare(double_q_nan, 1.0);
  compare(double_q_nan, double_q_nan);
}

jauhin 17:32 tmp> gcc -v nantest.c -lm
Reading specs from
/usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/specs
gcc version 2.95 19990718 (prerelease)
 /usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/cpp -lang-c -v
-D__GNUC__=2 -D__GNUC_MINOR__=95 -Dhppa -Dhp9000s800 -D__hp9000s800
-Dhp9k8 -DPWB -Dhpux -Dunix -D__hppa__ -D__hp9000s800__ -D__hp9000s800
-D__hp9k8__ -D__PWB__ -D__hpux__ -D__unix__ -D__hppa -D__hp9000s800
-D__hp9k8 -D__PWB -D__hpux -D__unix -Asystem(unix) -Asystem(hpux)
-Acpu(hppa) -Amachine(hppa) -D__hp9000s700 -D_PA_RISC1_1 -D_HPUX_SOURCE
-D_HIUX_SOURCE nankoe.c /var/tmp/cc30L2Eq.i
GNU CPP version 2.95 19990718 (prerelease) (hppa)
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include

/usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/../../../../hppa2.0-hp-hpux10.20/include
 /usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/include
 /usr/include
End of search list.
The following default directories have been omitted from the search
path:

/usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/../../../../include/g++-3
End of omitted list.
 /usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/cc1
/var/tmp/cc30L2Eq.i -quiet -dumpbase nankoe.c -version -o
/var/tmp/cc0VkTza.s
GNU C version 2.95 19990718 (prerelease) (hppa2.0-hp-hpux10.20) compiled
by GNU C version 2.95 19990718 (prerelease).
nankoe.c: In function `main':
nankoe.c:23: warning: return type of `main' is not `int'
 /usr/ccs/bin/as -o /var/tmp/ccsMaWXR.o /var/tmp/cc0VkTza.s
 /usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95/collect2
-L/lib/pa1.1 -L/usr/lib/pa1.1 -z -u main /usr/ccs/lib/crt0.o
-L/usr/local/egcs/lib/gcc-lib/hppa2.0-hp-hpux10.20/2.95 -L/usr/ccs/bin
-L/usr/ccs/lib -L/usr/local/egcs/lib /var/tmp/ccsMaWXR.o -lm -lgcc -lc
-lgcc

jauhin 17:32 tmp> a.out
  1.000000 <    ?.000000 TRUE
  1.000000 <=   ?.000000 TRUE
  1.000000 >    ?.000000 TRUE
  1.000000 >=   ?.000000 TRUE
  1.000000 ==   ?.000000 FALSE
  1.000000 !=   ?.000000 TRUE
  1.000000 ==   0.000000 FALSE
  1.000000 !=   0.000000 TRUE

  ?.000000 <    1.000000 TRUE
  ?.000000 <=   1.000000 TRUE
  ?.000000 >    1.000000 TRUE
  ?.000000 >=   1.000000 TRUE
  ?.000000 ==   1.000000 FALSE
  ?.000000 !=   1.000000 TRUE
  ?.000000 ==   0.000000 FALSE
  ?.000000 !=   0.000000 TRUE

  ?.000000 <    ?.000000 TRUE
  ?.000000 <=   ?.000000 TRUE
  ?.000000 >    ?.000000 TRUE
  ?.000000 >=   ?.000000 TRUE
  ?.000000 ==   ?.000000 FALSE
  ?.000000 !=   ?.000000 TRUE
  ?.000000 ==   0.000000 FALSE
  ?.000000 !=   0.000000 TRUE



-- 
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