Consider:
double f = foo();
double g = -f;
where foo()
can return anything that be assigned to f
.
is double g = -f;
safe in C and C++? For IEEE 754 types it obviously is but C and C++ do not restrict floating point implementation to that (unlike Java).
If it is safe, then would -g
always compare true
with foo()
?
(The above is not true for int
in 2's complement).
Floating types are defined by §5.2.4.2.2 of the C standard (draft N1570 at least):
So yes, that expression is somewhat safe, except in the case where
foo
returned one of the "other kinds of floating point numbers" not specified by the standard:and:
There are probably other caveats to this, but the standard goes in a fair bit of details about the characteristics of these types. You can read all about them in this publicly available draft.