Floating point comparisons with NaN values produce wrong results

Olli Saarela olli.saarela@kcl.fi
Thu Sep 30 19:57:00 GMT 1999


> Isn't it true that a NaN is *defined* to compare not equal to *any*
> valid FP number?

Yes indeed, and the equality (== !=) comparisons work as they should in
the test case. The inequality (< > <= >=) comparisons are defined to
evaluate to false, if either or both operands are NaN. In the test case,
these inequality comparisons evaluated to true, which is what I reported
as a bug.

A further test I ran:
  gcc nantest.c     ---> inequality comparisons incorrect
  gcc -O nantest.c  ---> inequality comparisons correct

> I think this has nothing to do with the compiler

Except that, e.g., an optimizing compiler mustn't treat floating-point
comparisons
    x > y   and  !(x <= y)
as equivalent.

Further information: "What Every Computer Scientist Should Know About
Floating Point Arithmetic" by David Goldberg.
http://www.lpthe.jussieu.fr/DOC_HTML/WorkShop/common-tools/numerical_comp_guide/goldberg1.ps

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