Math Argument Out of Domain
Production Risk
Silent NaN propagation from an unvalidated EDOM is a data integrity risk in numerical code.
EDOM (errno 33) is set by math library functions when an argument is outside the domain of the function — for example, passing a negative value to sqrt(), or log(0).
- 1sqrt() of a negative number
- 2log() or log2() of zero or a negative number
- 3asin() or acos() of a value outside [-1, 1]
- 4pow(0, negative) — mathematically undefined
Taking the square root of a negative number.
#include <math.h> #include <errno.h> errno = 0; double r = sqrt(-1.0); // r = NaN, errno = EDOM
expected output
sqrt(-1) returns NaN, errno = EDOM
Fix
Validate inputs before calling math functions
WHEN Always — check domain preconditions before calling C math functions
if (x < 0) {
fprintf(stderr, "sqrt: negative input\n");
return -1;
}
double r = sqrt(x);Why this works
Math functions set errno=EDOM and return NaN for out-of-domain inputs. Validate inputs first to detect logic bugs early rather than propagating NaN.
✕ Ignore NaN propagation
NaN values spread silently through calculations. A single EDOM-causing call can corrupt downstream results without any obvious error.
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev