In C++, do I have a guarantee that, for any given float a
and float b
, one and only one of a < b
, a == b
and a > b
is true?
If this differs between compilers and platforms, I am interested in Visual C++ on x86.
In C++, do I have a guarantee that, for any given float a
and float b
, one and only one of a < b
, a == b
and a > b
is true?
If this differs between compilers and platforms, I am interested in Visual C++ on x86.
No.
It's enough for either
a
orb
to beNaN
for each ofa < b
,a == b
anda > b
to be false.If both
a
andb
are non-NaN then exactly one ofa < b
,a == b
ora > b
has to be true.In complement, this answer tells you how you can get a NaN value in C++ (there are several NaN values, that can be distinguished by inspecting their representations; they are all different from each other because NaN is never equal to anything,) and how you can test whether a value is a NaN (an idiomatic test to see if a variable
x
is a NaN isx != x
, and indeedstd::isnan()
is often implemented this way, but some programmers who will have to read your code may be confused by it).And then, if
a
andb
are the results of previous computations, there is the problem of excess precision. See this article for a discussion in C. The C99 standard solved the problem by making rules explicit for where excess precision could and could not occur, but despite C++ more or less inheriting these rules by deferring to the C standard for the definition ofFLT_EVAL_METHOD
incfloat
, in practice C compilers take the rules more seriously than C++ compilers. For instance GCC implements the rules for C when compiling with-std=c99
, and in this context you can rely on the property to hold, but as of this writing GCC does not implement these rules when used as a C++ compiler.