Question 14.9

How do I test for IEEE NaN and other special values?


Many systems with high-quality IEEE floating-point implementations provide facilities (e.g. predefined constants, and functions like isnan(), either as nonstandard extensions in <math.h> or perhaps in <ieee.h> or <nan.h>) to deal with these values cleanly, and work is being done to formally standardize such facilities. A crude but usually effective test for NaN is exemplified by

	#define isnan(x) ((x) != (x))
although non-IEEE-aware compilers may optimize the test away.

Another possibility is to format the value in question using sprintf: on many systems it generates strings like "NaN" and "Inf" which you could compare for in a pinch.

See also question 19.39.


Read sequentially: prev next up top


This page by Steve Summit // Copyright 1995 // mail feedback