I read this in a popular book about computer graphics,
There are many numeric computations that become much simpler if the programmer takes advantage of the IEEE rules. For example, consider the expression:
a = 1 / (1/b + 1/c)
Such expressions arise with resistors and lenses. If divide-by-zero resulted in a program crash (as was true in many systems before IEEE floating-point), then two if statements would be required to check for small or zero values of b or c. Instead, with IEEE floating-point, if b or c is zero, we will get a zero value for a as desired.
But what about the case where b=+0
and c=-0
? Then a=1/inf-inf=nan
. Is the book wrong about this optimizations, or have I misunderstood something? It seems like we'll still need at least one check for the signs of b & c rather than no checks at all.
Edit One suggestion in the comments was just to do a post-check for NaN. Is that the idiomatic way to "take advantage" of these number type?
bool is_nan(float x) { return x != x; }
float optic_thing(float b, float c) {
float result = 1.0f / (1.0f/b + 1.0f/c);
if (is_nan(result)) result = 0;
return result;
}