Skip to content
Tutorials Center
My learning

Topics

C Reference ยท ctype.h, math.h and others

C math constants: INFINITY, NAN, HUGE_VAL and M_PI

Reference for the constants in math.h: INFINITY, NAN, HUGE_VAL, the nonstandard M_PI family, math_errhandling and how to define pi portably.

<math.h> defines a few constants for special floating-point values. INFINITY is positive infinity as a float constant expression, NAN is a quiet NaN, and HUGE_VAL is what many math functions return when a result is too large to represent. They are ISO C and work everywhere that supports IEEE 754.

Mathematical constants such as M_PI and M_E are different: they come from POSIX, not from the C standard. With a strict -std=c23 build they are hidden unless you ask for them with a feature-test macro, and portable code defines its own fallback.

C
#include <math.h>

double inf = INFINITY;
double not_a_number = NAN;
double big = HUGE_VAL;

Try it

C INFINITY, NAN and HUGE_VAL

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C Pi and friends, portably

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Reference

Constant Standard Meaning
INFINITY C99 Positive infinity (type float). If the platform cannot represent infinity, it is a constant that overflows at translation time.
NAN C99 A quiet NaN (type float). Defined only if the platform supports quiet NaNs.
HUGE_VAL, HUGE_VALF, HUGE_VALL C89, C99 A large positive value of type double, float, long double; positive infinity on IEEE 754 systems. Returned on overflow.
FP_NAN, FP_INFINITE, FP_ZERO, FP_SUBNORMAL, FP_NORMAL C99 Results of fpclassify().
FP_ILOGB0, FP_ILOGBNAN C99 What ilogb() returns for 0 and for NaN.
MATH_ERRNO, MATH_ERREXCEPT C99 Bits in math_errhandling telling whether math functions report errors through errno, exceptions, or both.
math_errhandling C99 An int expression combining the two bits above.
FP_FAST_FMA C99 Defined when fma() is as fast as a multiply and an add.
FLT_EVAL_METHOD C99 How wide intermediate results are evaluated (see float.h).
M_PI, M_PI_2, M_PI_4, M_E, M_SQRT2, M_LN2, M_LN10, M_LOG2E, M_LOG10E, M_1_PI, M_2_PI, M_SQRT1_2 POSIX, not ISO C Mathematical constants as double.

Usage notes

  • M_PI is not standard C. Under -std=c23 (or any strict mode) glibc hides it unless you define _DEFAULT_SOURCE, _XOPEN_SOURCE (500 or higher) or _GNU_SOURCE before the first #include. MSVC needs _USE_MATH_DEFINES. The #ifndef M_PI fallback makes the code portable.
  • Computing pi at run time with acos(-1.0) or 4 * atan(1.0) is exact to the last digit on common platforms, and avoids a typo in a long literal. Write a literal once and give it a name; do not repeat magic digits.
  • C23 constexpr lets you define such a constant as a real compile-time constant instead of a macro, with a type and a scope.
  • INFINITY is a float, but it converts to any wider floating type without loss. -INFINITY is negative infinity.
  • Do not print NAN. Whether it comes out as nan or -nan depends on the platform. Test with isnan().
  • HUGE_VAL is not the largest finite number. That is DBL_MAX from <float.h>. Compare against HUGE_VAL (or use isinf()) to detect overflow.
  • Check math_errhandling before relying on errno. On glibc it contains MATH_ERRNO, so errno is set; on some platforms (macOS) it is not. See errno.
  • Use the f and l variants (HUGE_VALF, HUGE_VALL) only when you need that precision; the plain constants are double.