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.
#include <math.h> double inf = INFINITY; double not_a_number = NAN; double big = HUGE_VAL;
Try it
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.
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_PIis 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_SOURCEbefore the first#include. MSVC needs_USE_MATH_DEFINES. The#ifndef M_PIfallback makes the code portable.- Computing pi at run time with
acos(-1.0)or4 * 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
constexprlets you define such a constant as a real compile-time constant instead of a macro, with a type and a scope. INFINITYis afloat, but it converts to any wider floating type without loss.-INFINITYis negative infinity.- Do not print
NAN. Whether it comes out asnanor-nandepends on the platform. Test withisnan(). HUGE_VALis not the largest finite number. That isDBL_MAXfrom<float.h>. Compare againstHUGE_VAL(or useisinf()) to detect overflow.- Check
math_errhandlingbefore relying onerrno. On glibc it containsMATH_ERRNO, soerrnois set; on some platforms (macOS) it is not. See errno. - Use the
fandlvariants (HUGE_VALF,HUGE_VALL) only when you need that precision; the plain constants aredouble.