C mathematical functions


C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions. All functions use floating-point numbers in one manner or another. Different C standards provide different, albeit backwards-compatible, sets of functions. Most of these functions are also available in the C++ standard library, though in different headers.

Overview of functions

Most of the mathematical functions are defined in . The functions that operate on integers, such as abs, labs, div, and ldiv, are instead defined in the header.
Any functions that operate on angles use radians as the unit of angle.
Not all of these functions are available in the C89 version of the standard. For those that are, the functions accept only type double for the floating-point arguments, leading to expensive type conversions in code that otherwise used single-precision float values. In C99, this shortcoming was fixed by introducing new sets of functions that work on float and long double arguments. Those functions are identified by f and l suffixes respectively.
FunctionDescription


computes absolute value of an integer value
computes absolute value of a floating-point value


computes the quotient and remainder of integer division
remainder of the floating-point division operation
signed remainder of the division operation
signed remainder as well as the three last bits of the division operation
fused multiply-add operation
larger of two floating-point values
smaller of two floating-point values
positive difference of two floating-point values


returns a not-a-number
Exponential
functions
returns e raised to the given power
Exponential
functions
returns 2 raised to the given power
Exponential
functions
returns e raised to the given power, minus one
Exponential
functions
computes natural logarithm
Exponential
functions
computes binary logarithm
Exponential
functions
computes common logarithm
Exponential
functions
computes natural logarithm of 1 plus the given number
Exponential
functions
extracts exponent of the number
Exponential
functions
extracts exponent of the number
Power
functions
computes square root
Power
functions
computes cubic root
Power
functions
computes square root of the sum of the squares of two given numbers
Power
functions
raises a number to the given power
Trigonometric
functions
computes sine
Trigonometric
functions
computes cosine
Trigonometric
functions
computes tangent
Trigonometric
functions
computes arc sine
Trigonometric
functions
computes arc cosine
Trigonometric
functions
computes arc tangent
Trigonometric
functions
computes arc tangent, using signs to determine quadrants
Hyperbolic
functions
computes hyperbolic sine
Hyperbolic
functions
computes hyperbolic cosine
Hyperbolic
functions
computes hyperbolic tangent
Hyperbolic
functions
computes hyperbolic arc sine
Hyperbolic
functions
computes hyperbolic arc cosine
Hyperbolic
functions
computes hyperbolic arc tangent
Error and
gamma
functions
computes error function
Error and
gamma
functions
computes complementary error function
Error and
gamma
functions
computes natural logarithm of the absolute value of the gamma function
Error and
gamma
functions
computes gamma function
Nearest
integer
floating-
point
operations
returns the nearest integer not less than the given value
Nearest
integer
floating-
point
operations
returns the nearest integer not greater than the given value
Nearest
integer
floating-
point
operations
returns the nearest integer not greater in magnitude than the given value
Nearest
integer
floating-
point
operations


returns the nearest integer, rounding away from zero in halfway cases
Nearest
integer
floating-
point
operations
returns the nearest integer using current rounding mode
Nearest
integer
floating-
point
operations


returns the nearest integer using current rounding mode with exception if the result differs
Floating-
point
manipulation
functions
decomposes a number into significand and a power of 2
Floating-
point
manipulation
functions
multiplies a number by 2 raised to a power
Floating-
point
manipulation
functions
decomposes a number into integer and fractional parts
Floating-
point
manipulation
functions

multiplies a number by FLT_RADIX raised to a power
Floating-
point
manipulation
functions

returns next representable floating-point value towards the given value
Floating-
point
manipulation
functions
copies the sign of a floating-point value
Classificationcategorizes the given floating-point value
Classificationchecks if the given number has finite value
Classificationchecks if the given number is infinite
Classificationchecks if the given number is NaN
Classificationchecks if the given number is normal
Classificationchecks if the given number is negative

Floating-point environment

adds several functions and types for fine-grained control of floating-point environment. These functions can be used to control a variety of settings that affect floating-point computations, for example, the rounding mode, on what conditions exceptions occur, when numbers are flushed to zero, etc. The floating-point environment functions and types are defined in header.
FunctionDescription
clears exceptions
stores current floating-point environment
stores current status flags
retrieves current rounding direction
saves current floating-point environment and clears all exceptions
raises a floating-point exception
sets current floating-point environment
sets current status flags
sets current rounding direction
tests whether certain exceptions have been raised
restores floating-point environment, but keeps current exceptions

Complex numbers

adds a new _Complex keyword that provides support for complex numbers. Any floating-point type can be modified with complex, and is then defined as a pair of floating-point numbers. Note that C99 and C++ do not implement complex numbers in a code-compatible way – the latter instead provides the class.
All operations on complex numbers are defined in header. As with the real-valued functions, an f or l suffix denotes the float complex or long double complex variant of the function.
FunctionDescription
Basic
operations
computes absolute value
Basic
operations
computes argument of a complex number
Basic
operations
computes imaginary part of a complex number
Basic
operations
computes real part of a complex number
Basic
operations
computes complex conjugate
Basic
operations
computes complex projection into the Riemann sphere
Exponentiation
operations
computes complex exponential
Exponentiation
operations
computes complex logarithm
Exponentiation
operations
computes complex square root
Exponentiation
operations
computes complex power
Trigonometric
operations
computes complex sine
Trigonometric
operations
computes complex cosine
Trigonometric
operations
computes complex tangent
Trigonometric
operations
computes complex arc sine
Trigonometric
operations
computes complex arc cosine
Trigonometric
operations
computes complex arc tangent
Hyperbolic
operations
computes complex hyperbolic sine
Hyperbolic
operations
computes complex hyperbolic cosine
Hyperbolic
operations
computes complex hyperbolic tangent
Hyperbolic
operations
computes complex hyperbolic arc sine
Hyperbolic
operations
computes complex hyperbolic arc cosine
Hyperbolic
operations
computes complex hyperbolic arc tangent

A few more complex functions are "reserved for future use in C99". Implementations are provided by open-source projects that are not part of the standard library.
FunctionDescription
Error functionscomputes the complex error function
Error functionscomputes the complex complementary error function

Type-generic functions

The header defines a type-generic macro for each mathematical function defined in and . This adds a limited support for function overloading of the mathematical functions: the same function name can be used with different types of parameters; the actual function will be selected at compile time according to the types of the parameters.
Each type-generic macro that corresponds to a function that is defined for both real and complex numbers encapsulates a total of 6 different functions: float, double and long double, and their complex variants. The type-generic macros that correspond to a function that is defined for only real numbers encapsulates a total of 3 different functions: float, double and long double variants of the function.
The C++ language includes native support for function overloading and thus does not provide the header even as a compatibility feature.

Random number generation

The header defines several functions that can be used for statistically random number generation.
FunctionDescription
generates a pseudo-random number between 0 and RAND_MAX, inclusive.
initializes a pseudo-random number generator
arc4randomgenerates a pseudo-random number between 0 and UINT32_MAX, usually using a better algorithm than rand
arc4random_uniformgenerates a pseudo-random number between 0 and a maximum value.
arc4random_buffill a buffer with a pseudo-random bitstream.
arc4random_stirinitializes a pseudo-random number generator.

The arc4random family of random number functions are not defined in POSIX standard, but is found in some common libc implementations. It used to refer to the keystream generator of a leaked version of RC4 cipher, but different algorithms, usually from other ciphers like ChaCha20, have been implemented since using the same name.
The quality of randomness from rand are usually too weak to be even considered statistically random, and it requires explicit seeding. It is usually advised to use arc4random instead of rand when possible. Some C libraries implement rand using arc4random_uniform internally.

Implementations

Under POSIX systems like Linux and BSD, the mathematical functions are bundled separately in the mathematical library . Therefore, if any of those functions are used, the linker must be given the directive -lm. There are various libm implementations, including:
Implementations not necessarily under a name of include: