C mathematical functions
Template:Short description Template:C Standard Library C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical functions.[1][2] 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.[1]
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.[3]
| Function | Description | |
|---|---|---|
Script error: No such module "anchor".absScript error: No such module "anchor". labsScript error: No such module "anchor". llabs
|
computes absolute value of an integer value | |
Script error: No such module "anchor".fabs
|
computes absolute value of a floating-point value | |
Script error: No such module "anchor".divScript error: No such module "anchor". ldivScript error: No such module "anchor". lldiv
|
computes the quotient and remainder of integer division | |
Script error: No such module "anchor".fmod
|
remainder of the floating-point division operation | |
Script error: No such module "anchor".remainder
|
signed remainder of the division operation | |
Script error: No such module "anchor".remquo
|
signed remainder as well as the three last bits of the division operation | |
Script error: No such module "anchor".fma
|
fused multiply-add operation | |
Script error: No such module "anchor".fmax
|
larger of two floating-point values | |
Script error: No such module "anchor".fmin
|
smaller of two floating-point values | |
Script error: No such module "anchor".fdim
|
positive difference of two floating-point values | |
Script error: No such module "anchor".nanScript error: No such module "anchor". nanfScript error: No such module "anchor". nanl
|
returns a NaN (not-a-number) | |
| Exponential functions |
Script error: No such module "anchor".exp
|
returns e raised to the given power |
Script error: No such module "anchor".exp2
|
returns 2 raised to the given power | |
Script error: No such module "anchor".expm1
|
returns e raised to the given power, minus one | |
Script error: No such module "anchor".log
|
computes natural logarithm (to base e) | |
Script error: No such module "anchor".log2
|
computes binary logarithm (to base 2) | |
Script error: No such module "anchor".log10
|
computes common logarithm (to base 10) | |
Script error: No such module "anchor".log1p
|
computes natural logarithm (to base e) of 1 plus the given number | |
Script error: No such module "anchor".ilogb
|
extracts exponent of the number | |
Script error: No such module "anchor".logb
|
extracts exponent of the number | |
| Power functions |
Script error: No such module "anchor".sqrt
|
computes square root |
Script error: No such module "anchor".cbrt
|
computes cubic root | |
Script error: No such module "anchor".hypot
|
computes square root of the sum of the squares of two given numbers | |
Script error: No such module "anchor".pow
|
raises a number to the given power[4] | |
| Trigonometric functions |
Script error: No such module "anchor".sin
|
computes sine |
Script error: No such module "anchor".cos
|
computes cosine | |
Script error: No such module "anchor".tan
|
computes tangent | |
Script error: No such module "anchor".asin
|
computes arc sine | |
Script error: No such module "anchor".acos
|
computes arc cosine | |
Script error: No such module "anchor".atan
|
computes arc tangent | |
Script error: No such module "anchor".atan2
|
computes arc tangent, using signs to determine quadrants | |
| Hyperbolic functions |
Script error: No such module "anchor".sinh
|
computes hyperbolic sine |
Script error: No such module "anchor".cosh
|
computes hyperbolic cosine | |
Script error: No such module "anchor".tanh
|
computes hyperbolic tangent | |
Script error: No such module "anchor".asinh
|
computes hyperbolic arc sine | |
Script error: No such module "anchor".acosh
|
computes hyperbolic arc cosine | |
Script error: No such module "anchor".atanh
|
computes hyperbolic arc tangent | |
| Error and gamma functions |
Script error: No such module "anchor".erf
|
computes error function |
Script error: No such module "anchor".erfc
|
computes complementary error function | |
Script error: No such module "anchor".lgamma
|
computes natural logarithm of the absolute value of the gamma function | |
Script error: No such module "anchor".tgamma
|
computes gamma function | |
| Nearest integer floating- point operations |
Script error: No such module "anchor".ceil
|
returns the nearest integer not less than the given value |
Script error: No such module "anchor".floor
|
returns the nearest integer not greater than the given value | |
Script error: No such module "anchor".trunc
|
returns the nearest integer not greater in magnitude than the given value | |
Script error: No such module "anchor".roundScript error: No such module "anchor". lroundScript error: No such module "anchor". llround
|
returns the nearest integer, rounding away from zero in halfway cases | |
Script error: No such module "anchor".nearbyint
|
returns the nearest integer using current rounding mode | |
Script error: No such module "anchor".rintScript error: No such module "anchor". lrintScript error: No such module "anchor". llrint
|
returns the nearest integer using current rounding mode with exception if the result differs | |
| Floating- point manipulation functions |
Script error: No such module "anchor".frexp
|
decomposes a number into significand and a power of 2 |
Script error: No such module "anchor".ldexp
|
multiplies a number by 2 raised to a power | |
Script error: No such module "anchor".modf
|
decomposes a number into integer and fractional parts | |
Script error: No such module "anchor".scalbnScript error: No such module "anchor". scalbln
|
multiplies a number by FLT_RADIX raised to a power | |
Script error: No such module "anchor".nextafterScript error: No such module "anchor". nexttoward
|
returns next representable floating-point value towards the given value | |
Script error: No such module "anchor".copysign
|
copies the sign of a floating-point value | |
| Classification | Script error: No such module "anchor".fpclassify
|
categorizes the given floating-point value |
Script error: No such module "anchor".isfinite
|
checks if the argument has finite value | |
Script error: No such module "anchor".isinf
|
checks if the argument is infinite | |
Script error: No such module "anchor".isnan
|
checks if the argument is NaN | |
Script error: No such module "anchor".isnormal
|
checks if the argument is normal | |
Script error: No such module "anchor".signbit
|
checks if the sign of the argument is negative |
Script error: No such module "anchor".Floating-point environment
C99 adds several functions and types for fine-grained control of floating-point environment.[3] 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++).
| Function | Description |
|---|---|
Script error: No such module "anchor".feclearexcept
|
clears exceptions (C99) |
Script error: No such module "anchor".fegetenv
|
stores current floating-point environment (C99) |
Script error: No such module "anchor".fegetexceptflag
|
stores current status flags (C99) |
Script error: No such module "anchor".fegetround
|
retrieves current rounding direction (C99) |
Script error: No such module "anchor".feholdexcept
|
saves current floating-point environment and clears all exceptions (C99) |
Script error: No such module "anchor".feraiseexcept
|
raises a floating-point exception (C99) |
Script error: No such module "anchor".fesetenv
|
sets current floating-point environment (C99) |
Script error: No such module "anchor".fesetexceptflag
|
sets current status flags (C99) |
Script error: No such module "anchor".fesetround
|
sets current rounding direction (C99) |
Script error: No such module "anchor".fetestexcept
|
tests whether certain exceptions have been raised (C99) |
Script error: No such module "anchor".feupdateenv
|
restores floating-point environment, but keeps current exceptions (C99) |
Script error: No such module "anchor". 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 Template:Cpp.
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.
| Function | Description | |
|---|---|---|
| Basic operations |
Script error: No such module "anchor".cabs
|
computes absolute value (C99) |
Script error: No such module "anchor".carg
|
computes argument of a complex number (C99) | |
Script error: No such module "anchor".cimag
|
computes imaginary part of a complex number (C99) | |
Script error: No such module "anchor".creal
|
computes real part of a complex number (C99) | |
Script error: No such module "anchor".conj
|
computes complex conjugate (C99) | |
Script error: No such module "anchor".cproj
|
computes complex projection into the Riemann sphere (C99) | |
| Exponentiation operations |
Script error: No such module "anchor".cexp
|
computes complex exponential (C99) |
Script error: No such module "anchor".clog
|
computes complex logarithm (C99) | |
Script error: No such module "anchor".csqrt
|
computes complex square root (C99) | |
Script error: No such module "anchor".cpow
|
computes complex power (C99) | |
| Trigonometric operations |
Script error: No such module "anchor".csin
|
computes complex sine (C99) |
Script error: No such module "anchor".ccos
|
computes complex cosine (C99) | |
Script error: No such module "anchor".ctan
|
computes complex tangent (C99) | |
Script error: No such module "anchor".casin
|
computes complex arc sine (C99) | |
Script error: No such module "anchor".cacos
|
computes complex arc cosine (C99) | |
Script error: No such module "anchor".catan
|
computes complex arc tangent (C99) | |
| Hyperbolic operations |
Script error: No such module "anchor".csinh
|
computes complex hyperbolic sine (C99) |
Script error: No such module "anchor".ccosh
|
computes complex hyperbolic cosine (C99) | |
Script error: No such module "anchor".ctanh
|
computes complex hyperbolic tangent (C99) | |
Script error: No such module "anchor".casinh
|
computes complex hyperbolic arc sine (C99) | |
Script error: No such module "anchor".cacosh
|
computes complex hyperbolic arc cosine (C99) | |
Script error: No such module "anchor".catanh
|
computes complex hyperbolic arc tangent (C99) |
A few more complex functions are "reserved for future use in C99".[5] Implementations are provided by open-source projects that are not part of the standard library.
| Function | Description | |
|---|---|---|
| Error functions | Script error: No such module "anchor".cerf
|
computes the complex error function (C99) |
Script error: No such module "anchor".cerfc
|
computes the complex complementary error function (C99) |
Type-generic functions
Script error: No such module "anchor".
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
Script error: No such module "anchor".
The header <stdlib.h> (<cstdlib> in C++) defines several functions that can be used for statistically random number generation.[6]
| Function | Description |
|---|---|
Script error: No such module "anchor".rand
|
generates a pseudo-random number between 0 and RAND_MAX, inclusive.
|
Script error: No such module "anchor".srand
|
initializes a pseudo-random number generator |
Script error: No such module "anchor".arc4random
|
generates a pseudo-random number between 0 and UINT32_MAX, usually using a better algorithm than rand
|
Script error: No such module "anchor".arc4random_uniform
|
generates a pseudo-random number between 0 and a maximum value. |
Script error: No such module "anchor".arc4random_buf
|
fill a buffer with a pseudo-random bitstream. |
Script error: No such module "anchor".arc4random_stir
|
initializes 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.
Implementations
Under POSIX systems like Linux and BSD, the mathematical functions (as declared in <math.h>) are bundled separately in the mathematical library Template:Vanchor. 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 libm
- AMD's libm, github, used almost as is by Windows
- Intel C++ Compiler libm
- Red Hat's libm (Newlib)
- Sun's FDLIBM, which was used as the basis for FreeBSD's msun and OpenBSD's libm, both of which in turn were the basis of Julia's OpenLibm
- musl's libm, based on the BSD
libmsand 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)[7]
- Arénaire project's CRlibm (correctly rounded libm), and its successor MetaLibm, which uses Remez algorithm to automatically generate approximations that are formally proven.
- Rutger's RLIBM, which provides correctly rounded functions in single precision.[8]
Script error: No such module "Labelled list hatnote".
Implementations not necessarily under a name of libm include:
- Arm's Template:GitHub
- Template:GitHub is a version of C/C++ math functions written for C++
constexpr(compile-time calculation) - CORE-MATH, correctly rounded for single and double precision.
- SIMD (vectorized) math libraries include SLEEF, Yeppp!, and Agner Fog's VCL, plus a few closed-source ones like SVML and DirectXMath.[9]
See also
References
External links
- ↑ a b Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ a b Script error: No such module "citation/CS1".
- ↑ Notationally, it may seem convenient to use pow(x,2) or pow(x,3) to compute squares or cubes. However, this is not advisable in time-critical code. Unless an implementation takes special care of these cases at compile time, x*x or x*x*x will execute much faster. Also, sqrt(x) and cbrt(x) should be preferred over pow(x,.5) or pow(x,1./3).
- ↑ man cerf(3), man cerfc(3), see e.g. https://linux.die.net/man/3/cerf.
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".
- ↑ Script error: No such module "citation/CS1".