C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions. 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 (the C headers are included as well, but only as a deprecated compatibility feature).

Overview of functions

Most of the mathematical functions, which use floating-point numbers, are defined in <math.h> (<cmath> header in C++). The functions that operate on integers, such as abs, labs, div, and ldiv, are instead defined in the <stdlib.h> header (<cstdlib> header in C++).

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 NaN (not-a-number)
Exponential functionsreturns e raised to the given power
returns 2 raised to the given power
returns e raised to the given power, minus one
computes natural logarithm (to base e)
computes binary logarithm (to base 2)
computes common logarithm (to base 10)
computes natural logarithm (to base e) of 1 plus the given number
extracts exponent of the number
extracts exponent of the number
Power functionscomputes square root
computes cubic root
computes square root of the sum of the squares of two given numbers
raises a number to the given power
Trigonometric functionscomputes sine
computes cosine
computes tangent
computes arc sine
computes arc cosine
computes arc tangent
computes arc tangent, using signs to determine quadrants
Hyperbolic functionscomputes hyperbolic sine
computes hyperbolic cosine
computes hyperbolic tangent
computes hyperbolic arc sine
computes hyperbolic arc cosine
computes hyperbolic arc tangent
Error and gamma functionscomputes error function
computes complementary error function
computes natural logarithm of the absolute value of the gamma function
computes gamma function
Nearest integer floating- point operationsreturns the nearest integer not less than the given value
returns the nearest integer not greater than the given value
returns the nearest integer not greater in magnitude than the given value
returns the nearest integer, rounding away from zero in halfway cases
returns the nearest integer using current rounding mode
returns the nearest integer using current rounding mode with exception if the result differs
Floating- point manipulation functionsdecomposes a number into significand and a power of 2
multiplies a number by 2 raised to a power
decomposes a number into integer and fractional parts
multiplies a number by FLT_RADIX raised to a power
returns next representable floating-point value towards the given value
copies the sign of a floating-point value
Classificationcategorizes the given floating-point value
checks if the argument has finite value
checks if the argument is infinite
checks if the argument is NaN
checks if the argument is normal
checks if the sign of the argument is negative

Floating-point environment

C99 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 <fenv.h> header (<cfenv> in C++).

FunctionDescription
clears exceptions (C99)
stores current floating-point environment (C99)
stores current status flags (C99)
retrieves current rounding direction (C99)
saves current floating-point environment and clears all exceptions (C99)
raises a floating-point exception (C99)
sets current floating-point environment (C99)
sets current status flags (C99)
sets current rounding direction (C99)
tests whether certain exceptions have been raised (C99)
restores floating-point environment, but keeps current exceptions (C99)

Complex numbers

C99 adds a new _Complex keyword (and complex convenience macro; only available if the <complex.h> header is included) 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 std::complex.

All operations on complex numbers are defined in the <complex.h> 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 operationscomputes absolute value (C99)
computes argument of a complex number (C99)
computes imaginary part of a complex number (C99)
computes real part of a complex number (C99)
computes complex conjugate (C99)
computes complex projection into the Riemann sphere (C99)
Exponentiation operationscomputes complex exponential (C99)
computes complex logarithm (C99)
computes complex square root (C99)
computes complex power (C99)
Trigonometric operationscomputes complex sine (C99)
computes complex cosine (C99)
computes complex tangent (C99)
computes complex arc sine (C99)
computes complex arc cosine (C99)
computes complex arc tangent (C99)
Hyperbolic operationscomputes complex hyperbolic sine (C99)
computes complex hyperbolic cosine (C99)
computes complex hyperbolic tangent (C99)
computes complex hyperbolic arc sine (C99)
computes complex hyperbolic arc cosine (C99)
computes complex hyperbolic arc tangent (C99)

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 (C99)
computes the complex complementary error function (C99)

Type-generic functions

The header <tgmath.h> defines a type-generic macro for each mathematical function defined in <math.h> and <complex.h>. 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 <tgmath.h> header even as a compatibility feature.

Random-number generation

The header <stdlib.h> (<cstdlib> in C++) 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 (hence "alleged RC4"), 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.

Special functions

Beginning in C++17, C++ introduces special functions into the <cmath> header.

FunctionDescription
computes associated Laguerre polynomials
computes associated Legendre polynomials
computes beta function
computes complete elliptic integral of the first kind
computes complete elliptic integral of the second kind
computes complete elliptic integral of the third kind
computes regular modified cylindrical Bessel functions
computes cylindrical Bessel functions of the first kind
computes irregular modified cylindrical Bessel functions
computes cylindrical Neumann functions
computes incomplete elliptic integral of the first kind
computes incomplete elliptic integral of the second kind
computes incomplete elliptic integral of the third kind
computes exponential integral
computes Hermite polynomials
computes Legendre polynomials
computes Laguerre polynomials
computes Riemann zeta function
computes spherical Bessel functions of the first kind
computes spherical associated Legendre functions
computes spherical Neumann functions

Implementations

Under POSIX systems like Linux and BSD, the mathematical functions (as declared in <math.h>) are bundled separately in the mathematical library libm. Therefore, if any of those functions are used, the linker must be given the directive -lm. There are various libm implementations, including:

  • GNU libc's
  • AMD's , , used almost as is by Windows
  • Intel C++ Compiler libm
  • Red Hat's (Newlib)
  • Sun's , which was used as the basis for FreeBSD's and OpenBSD's , both of which in turn were the basis of Julia's
  • musl's , based on the BSD libms and other projects like Arm
  • LLVM's libm, which is correctly rounded (i.e. errors from the mathematically correct result are lower than 0.5 unit in the last place)
  • Arénaire project's (correctly rounded libm), and its successor , which uses Remez algorithm to automatically generate approximations that are formally proven.
  • Rutger's RLIBM, which provides correctly rounded functions in single precision.

Implementations not necessarily under a name of libm include:

  • Arm's
  • is a version of C/C++ math functions written for C++ constexpr (compile-time calculation)
  • , correctly rounded for single and double precision.
  • SIMD (vectorized) math libraries include , 2020-07-14 at the Wayback Machine, and Agner Fog's VCL, plus a few closed-source ones like SVML and DirectXMath.

See also

  • C99 floating-point support

External links