| C standard library (libc) |
|---|
| General topics |
| Miscellaneous headers |
C mathematical operations are a group of functions in thestandard library of theC 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 theC++ standard library, though in different headers (the C headers are included as well, but only as a deprecated compatibility feature).
Most of the mathematical functions, which usefloating-point numbers, are defined in<math.h> (<cmath> header in C++). The functions that operate onintegers, such asabs,labs,div, andldiv, are instead defined in the<stdlib.h> header (<cstdlib> header in C++).
Any functions that operate on angles useradians as the unit of angle.[1]
Not all of these functions are available in theC89 version of the standard. For those that are, the functions accept only typedouble for the floating-point arguments, leading to expensive type conversions in code that otherwise used single-precisionfloat values. In C99, this shortcoming was fixed by introducing new sets of functions that work onfloat andlong double arguments. Those functions are identified byf andl suffixes respectively.[3]
| Function | Description | |
|---|---|---|
abslabsllabs | computesabsolute value of an integer value | |
fabs | computes absolute value of a floating-point value | |
divldivlldiv | computes the quotient and remainder ofinteger division | |
fmod | remainder of the floating-point division operation | |
remainder | signed remainder of the division operation | |
remquo | signed remainder as well as the three last bits of the division operation | |
fma | fused multiply-add operation | |
fmax | larger of two floating-point values | |
fmin | smaller of two floating-point values | |
fdim | positive difference of two floating-point values | |
nannanfnanl | returns aNaN (not-a-number) | |
| Exponential functions | exp | returnse raised to the given power |
exp2 | returns 2 raised to the given power | |
expm1 | returns e raised to the given power, minus one | |
log | computesnatural logarithm (to base e) | |
log2 | computesbinary logarithm (to base 2) | |
log10 | computescommon logarithm (to base 10) | |
log1p | computes natural logarithm (to base e) of 1 plus the given number | |
ilogb | extracts exponent of the number | |
logb | extracts exponent of the number | |
| Power functions | sqrt | computessquare root |
cbrt | computescubic root | |
hypot | computessquare root of the sum of the squares of two given numbers | |
pow | raises a number to the given power[4] | |
| Trigonometric functions | sin | computessine |
cos | computescosine | |
tan | computestangent | |
asin | computesarc sine | |
acos | computesarc cosine | |
atan | computesarc tangent | |
atan2 | computesarc tangent, using signs to determine quadrants | |
| Hyperbolic functions | sinh | computes hyperbolic sine |
cosh | computes hyperbolic cosine | |
tanh | computes hyperbolic tangent | |
asinh | computes hyperbolic arc sine | |
acosh | computes hyperbolic arc cosine | |
atanh | computes hyperbolic arc tangent | |
| Error and gamma functions | erf | computeserror function |
erfc | computescomplementary error function | |
lgamma | computes natural logarithm of the absolute value of thegamma function | |
tgamma | computes gamma function | |
| Nearest integer floating- point operations | ceil | returns the nearest integer not less than the given value |
floor | returns the nearest integer not greater than the given value | |
trunc | returns the nearest integer not greater in magnitude than the given value | |
roundlroundllround | returns the nearest integer, rounding away from zero in halfway cases | |
nearbyint | returns the nearest integer using current rounding mode | |
rintlrintllrint | returns the nearest integer using current rounding mode with exception if the result differs | |
| Floating- point manipulation functions | frexp | decomposes a number into significand and a power of 2 |
ldexp | multiplies a number by 2 raised to a power | |
modf | decomposes a number into integer and fractional parts | |
scalbnscalbln | multiplies a number by FLT_RADIX raised to a power | |
nextafternexttoward | returns next representable floating-point value towards the given value | |
copysign | copies the sign of a floating-point value | |
| Classification | fpclassify | categorizes the given floating-point value |
isfinite | checks if the argument has finite value | |
isinf | checks if the argument is infinite | |
isnan | checks if the argument is NaN | |
isnormal | checks if the argument is normal | |
signbit | checks if the sign of the argument is negative |
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> inC++).
| Function | Description |
|---|---|
feclearexcept | clears exceptions (C99) |
fegetenv | stores current floating-point environment (C99) |
fegetexceptflag | stores current status flags (C99) |
fegetround | retrieves current rounding direction (C99) |
feholdexcept | saves current floating-point environment and clears all exceptions (C99) |
feraiseexcept | raises a floating-point exception (C99) |
fesetenv | sets current floating-point environment (C99) |
fesetexceptflag | sets current status flags (C99) |
fesetround | sets current rounding direction (C99) |
fetestexcept | tests whether certain exceptions have been raised (C99) |
feupdateenv | restores floating-point environment, but keeps current exceptions (C99) |
C99 adds a new_Complex keyword (andcomplex convenience macro; only available if the<complex.h> header is included) that provides support for complex numbers. Any floating-point type can be modified withcomplex, 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 classstd::complex.
All operations on complex numbers are defined in the<complex.h> header. As with the real-valued functions, anf orl suffix denotes thefloat complex orlong double complex variant of the function.
| Function | Description | |
|---|---|---|
| Basic operations | cabs | computesabsolute value (C99) |
carg | computesargument of a complex number (C99) | |
cimag | computesimaginary part of a complex number (C99) | |
creal | computesreal part of a complex number (C99) | |
conj | computescomplex conjugate (C99) | |
cproj | computes complex projection into theRiemann sphere (C99) | |
| Exponentiation operations | cexp | computes complexexponential (C99) |
clog | computes complexlogarithm (C99) | |
csqrt | computes complexsquare root (C99) | |
cpow | computes complexpower (C99) | |
| Trigonometric operations | csin | computes complexsine (C99) |
ccos | computes complexcosine (C99) | |
ctan | computes complextangent (C99) | |
casin | computes complexarc sine (C99) | |
cacos | computes complexarc cosine (C99) | |
catan | computes complexarc tangent (C99) | |
| Hyperbolic operations | csinh | computes complexhyperbolic sine (C99) |
ccosh | computes complexhyperbolic cosine (C99) | |
ctanh | computes complexhyperbolic tangent (C99) | |
casinh | computes complexhyperbolic arc sine (C99) | |
cacosh | computes complexhyperbolic arc cosine (C99) | |
catanh | computes complexhyperbolic 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 | cerf | computes the complexerror function (C99) |
cerfc | computes the complex complementaryerror function (C99) |
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 forfunction 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 andlong double, and theircomplex 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 andlong 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.
The header<stdlib.h> (<cstdlib> in C++) defines several functions that can be used for statistically random number generation.[6]
| Function | Description |
|---|---|
rand | generates a pseudo-random number between 0 andRAND_MAX, inclusive. |
srand | initializes apseudo-random number generator |
arc4random | generates a pseudo-random number between 0 andUINT32_MAX, usually using a better algorithm thanrand |
arc4random_uniform | generates a pseudo-random number between 0 and a maximum value. |
arc4random_buf | fill a buffer with a pseudo-random bitstream. |
arc4random_stir | initializes apseudo-random number generator. |
Thearc4random family of random number functions are not defined in POSIX standard, but is found in some commonlibc implementations. It used to refer to the keystream generator of a leaked version ofRC4 cipher (hence "allegedRC4"), but different algorithms, usually from other ciphers likeChaCha20, have been implemented since using the same name.
The quality of randomness fromrand are usually too weak to be even considered statistically random, and it requires explicit seeding. It is usually advised to usearc4random instead ofrand when possible. Some C libraries implementrand usingarc4random_uniform internally.
UnderPOSIX systems likeLinux andBSD, the mathematical functions (as declared in<math.h>) are bundled separately in the mathematical librarylibm. Therefore, if any of those functions are used, the linker must be given the directive-lm. There are variouslibm implementations, including:
libms and other projects like ArmImplementations not necessarily under a name oflibm include:
constexpr (compile-time calculation)math.h: mathematical declarations – Base Definitions Reference,The Single UNIX Specification, Version 5 fromThe Open Group