Name

HUGE_VAL macro — Range error value

Synopsis

double HUGE_VAL
               
image with no caption

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

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;
}

See Also

<cerrno>

Get C++ In a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.