NaN
is something like “error” or “unknown”. Every operation with NaN
results in NaN
. Infinity
and -Infinity
are introduced to handle overflow in a well-defined way. When overflow happens, you cannot be sure about how much it happened. Therefore two infinite values can be in fact different. Therefore Infinity - Infinity
does not have any well-defined value, which is handled by NaN
.
C example:
#include <stdio.h>
#include <float.h>
int main() {
double e, f;
/* floats are always implicitly converted to doubles for computation */
/* but intermediate values can be larger */
e = DBL_MAX * 2.0;
f = DBL_MAX * 2.5;
printf("%-12s%e\n", "DBL_MAX", DBL_MAX);
printf("%-12s%e\n", "e", e);
printf("%-12s%e\n", "f", f);
printf("%-12s%e\n", "f - e", f - e);
printf("%-12s%e\n", "f - e expr", DBL_MAX * 2.5 - DBL_MAX * 2.0);
return 0;
}
Output (GCC 4.3.2, Linux 2.6.26-2-686):
DBL_MAX 1.79769e+308
e inf
f inf
f - e nan
f - e expr 8.98847e+307
The last expression is a tricky one. Why this happens is described elsewhere. Basically the intermediate calculation is done in a larger type.