HUGE_VAL macro — Range error value
double HUGE_VAL
When an overflow occurs, most functions set errno
to ERANGE
and return HUGE_VAL
with the correct sign of the
result. The exact value of HUGE_VAL
is implementation-defined and is
not necessarily a compile-time constant. It might even be a value
that can be returned as a valid result from the function. In that
case, the only way to discover whether an overflow occurred is to
test errno
, as shown in Example 13-5.
Example 13-5. Computing a logarithm to any base
// Return the logarithm of x to the base n. template<typename T> T logn(T x, T n) { errno = 0; T logx = log(x); if (errno == ERANGE) return logx; // Should be HUGE_VAL else if (errno != 0) return logx; // Implementation defined T logn = log(n); if (errno == ERANGE) return logn; // Should be HUGE_VAL else if (errno != 0) return logn; // Implementation defined if (logn == 0) { errno = EDOM; return 0; } return logx / logn; }