math.h

The math.h header file contains function declarations for all the floating-point math functions:

No feature test macro required.

Notes:
  1. nan(), nanf(), and nanl() functions are supported under IEEE only.
  2. For the C99 math functions, it is required to define the feature test macro _ISO_C99_SOURCE or requires a compiler that is designed to support C99 to expose the functionality.
Special Behavior for C++: For C++ applications, each of the base functions in the following list is also overloaded for float, double, and long double. For example:
  • float sqrt(float)
  • double sqrt(double)
  • long double sqrt(long double)
_XOPEN_SOURCE
_XOPEN_SOURCE_EXTENDED 1
__STDC_WANT_DEC_FP__
For C++ applications, the following functions are overloaded for _Decimal32, _Decimal64, and _Decimal128:
For example:
  • _Decimal32 ceil(_Decimal32)
  • _Decimal64 ceil(_Decimal64)
  • _Decimal128 ceil(_Decimal128)
_TR1_C99: The following functions provide additional overloads.
  • If any argument corresponding to a double parameter has type long double, all arguments corresponding to double parameters are effectively cast to long double. The return type will be the same return type as if the ‘C’ long double version of the function was called.
  • Otherwise, if any argument corresponding to a double parameter has type double or an integer type, all arguments corresponding to double parameters are effectively cast to double. The return type will be the same return type as if the ‘C’ double version of the function was called.
  • Otherwise, all arguments corresponding to double parameters are effectively cast to float. The return type will be the same return type as if the ‘C’ float version of the function was called.

Object-like macros: Additional object-like macros provided by the math.h header are described.

Note: The floating point macros and the macros INFINITY and NAN are supported under IEEE only.
DEC_INFINITY
A constant expression of type _Decimal32 representing infinity.
DEC_NAN
A quiet decimal floating NaN for the type _Decimal32.
HUGE_VAL_D32
A constant expression of type _Decimal32 representing +infinity.
HUGE_VAL_D64
A constant expression of type _Decimal64 representing +infinity.
HUGE_VAL_D128
A constant expression of type _Decimal128 representing +infinity.
HUGE_VALF
A very large positive number that expands to a float expression
HUGE_VALL
A very large positive number that expands to a long double expression.
INFINITY
A constant expression of type float representing positive infinity.
NAN
A constant expression of type float representing a quiet NaN.
FP_INFINITE
The value of the macro fpclassify for an argument that is plus or minus infinity. This expands to an integer constant expression.
FP_NAN
The value of the macro fpclassify for an argument that is not-a-number (NaN). This expands to an integer constant expression.
FP_NORMAL
The value of the macro fpclassify for an argument that is finite and normalized. This expands to an integer constant expression.
FP_SUBNORMAL
The value of the macro fpclassify for an argument that is finite and denormalized. This expands to an integer constant expression.
FP_ZERO
The value of the macro fpclassify for an argument that is positive or negative. This expands to an integer constant expression.
FP_FAST_FMA
Indicates that the fma function generally executes about as fast as, or faster than, a multiply and an add of double operands.
FP_FAST_FMAF
This is the float version of FP_FAST_FMA.
FP_FAST_FMAL
This is the long double version of FP_FAST_FMA.
Note: Decimal floating-point does not support FP_FAST_FMAD32, FP_FAST_FMAD64, and FP_FAST_FMAD128.
FP_ILOGB0
The value returned by ilogb() if its argument is zero.
FP_ILOGBNAN
The value returned by ilogb() if its argument is a NaN.
MATH_ERRNO
This is defined as value 1 (one) and is used for testing the value of the macro math_errhandling to determine whether a math function reports an error by storing a nonzero value in errno.
MATH_ERREXCEPT
This is defined as value 2 and is used for testing the value of the macro math_errhandling to determine whether a math function reports an error by raising an invalid floating point exception.
math_errhandling
This macro expands to an expression that has type int and the value MATH_ERRNO, MATH_ERREXCEPT, or the bitwise OR of both. This implementation defines this macro as MATH_ERRNO.

Function-like macros or C++ function templates: Additional function-like macros or C++ function templates provided by the <math.h> header are listed.

The header file includes declarations for the built-in functions abs() and fabs(). For information about built-in functions, see Built-in functions.

The math.h header file declares the macro HUGE_VAL, which expands to a positive double constant expression, not necessarily representable as a float. Similarly, the macros HUGE_VALF and HUGE_VALL are respectively float and long double analogs of HUGE_VAL.

For all mathematical functions, a domain error occurs when an input argument is outside the range of values allowed for that function. If a domain error occurs, errno is set to the value of EDOM.

A range error occurs if the result of the function cannot be represented in a float, double, long double, _Decimal32, _Decimal64, or _Decimal128 value. If the magnitude of the result is too large (overflow), the function returns the positive or negative value of the macro HUGE_VAL, HUGE_VALF, HUGE_VALL, HUGE_VAL_D32, HUGE_VAL_D64, or HUGE_VAL_D128, as applicable, and sets errno to ERANGE. If the result is too small (underflow), the function returns 0.

A pole error occurs if the function has an exact infinite result as the finite input arguments are approached in the limit (for example, log(0.0)). If a pole error occurs, the function sets errno to ERANGE.

float_t and double_t are floating-point types whose type depends on the value of FLT_EVAL_METHOD. FLT_EVAL_METHOD is 1 which implies both float_t and double_t are double.

Note: Decimal floating-point does not support FP_FAST_FMAD32, FP_FAST_FMAD64, and FP_FAST_FMAD128.