A structured lesson workspace with readable content, hands-on examples, and a clean path to completion.
IEEE 754 floating-point has five categories: normal, subnormal, zero, infinite, and NaN. C99 provides macros to classify a value: isnan, isinf, isfinite, isnormal, and the general fpclassify. These are essential for numerical code. A NaN comparison always returns false, including x == x. An infinity propagates through arithmetic. A subnormal value indicates you are near the underflow boundary. If you do not check for these special values, your numerical code will produce silent garbage.
#include <stdio.h>#include <math.h>void classify(const char *label, double val) {printf("%-20s -> ", label);switch (fpclassify(val)) {case FP_NAN: printf("NaN"); break;case FP_INFINITE: printf("Infinite"); break;case FP_ZERO: printf("Zero"); break;case FP_SUBNORMAL: printf("Subnormal"); break;case FP_NORMAL: printf("Normal"); break;default: printf("Unknown"); break;}printf(" isnan=%d isinf=%d isfinite=%d\n",isnan(val), isinf(val), isfinite(val));}int main(void) {classify("3.14", 3.14);classify("0.0", 0.0);classify("1.0/0.0", 1.0 / 0.0);classify("0.0/0.0", 0.0 / 0.0);classify("1e-320", 1e-320);classify("-INFINITY", -INFINITY);return 0;}
fpclassify gives you the full category. The individual macros answer specific questions. isfinite is the most commonly needed check: it returns true for normal, subnormal, and zero, and false for infinity and NaN. Use it as a guard before passing values to functions that assume finite input.
#include <stdio.h>#include <math.h>int main(void) {double nan_val = NAN;printf("nan_val == nan_val: %d\n", nan_val == nan_val);printf("nan_val != nan_val: %d\n", nan_val != nan_val);printf("isnan(nan_val): %d\n", isnan(nan_val));return 0;}
NaN is not equal to itself. This is specified by IEEE 754. The only reliable way to detect NaN is the isnan macro. Do not write x != x as a NaN check. It works, but it is obscure and some compilers optimize it away under -ffast-math. Use isnan. It is clear, portable, and correct.
Floating-point classification is defensive numerical programming. Check your inputs. Check your outputs. Do not let NaN or infinity propagate silently. The macros cost nothing and save you from debugging a calculation chain that went wrong twenty operations ago. Next: tolower, the character transformation function.
Finish the lesson once you have worked through the material. This awards ★ 30 XP.