C standard library (libc) |
---|
General topics |
Miscellaneous headers |
C mathematical operations are a group of functions in the standard library of the C programming language implementing basic mathematical operations.[1] Most of the functions involve the use of floating point numbers. Different C standards provide different albeit backwards-compatible, sets of functions. C mathematical functions are inherited in C++.
Function overview
Most of the mathematical functions are placed in math.h header (cmath header in C++). The functions that operate on integers, such as abs, labs, div, and ldiv, are instead specified in the stdlib.h header (cstdlib header in C++).
Any functions that operate on angles use radians as the unit of angle.[1]
In original C, all functions accept only double
for the floating-point arguments. In C99, this limitation was fixed by introducing a new set of functions with f or l suffix that work on float
and long double
arguments respectively.
- Basic operations
- abs, labs, llabs - C/C++ - computes absolute value of an integral value
- fabs - C/C++ - computes absolute value of a floating point value
- div, ldiv, lldiv - C/C++ - computes the quotient and remainder of integer division
- fmod- C/C++ - remainder of the floating point division operation
- remainder - C99/C++11 - signed remainder of the division operation
- remquo - C99/C++11 - signed remainder as well as the three last bits of the division operation
- fma - C99/C++11 - fused multiply-add operation
- fmax - C99/C++11 - larger of two floating point values
- fmin - C99/C++11 - smaller of two floating point values
- fdim - C99/C++11 - positive difference of two floating point values
- nan, nanf, nanl - C99/C++11 - returns a not-a-number (NaN)
- Exponential functions
- exp - C/C++ - returns e raised to the given power
- exp2 - C99/C++11 - returns 2 raised to the given power
- expm1 - C99/C++11 - returns e raised to the given power, minus one
- log - C/C++ - computes natural (base e) logarithm (to base e)
- log10 - C/C++ - computes common (base 10) logarithm
- log1p - C99/C++11 - computes natural logarithm (to base e) of 1 plus the given number
- ilogb - C99/C++11 - extracts exponent of the number
- logb - C99/C++11 - extracts exponent of the number
- Power functions
- sqrt - C/C++ - computes square root
- cbrt - C99/C++11 - computes cubic root
- hypot - C99/C++11 - computes square root of the sum of the squares of two given numbers
- pow - C/C++ - raises a number to the given power
- Trigonometric functions
- sin - C/C++ - computes sine
- cos - C/C++ - computes cosine
- tan - C/C++ - computes tangent
- asin - C/C++ - computes arc sine
- acos - C/C++ - computes arc cosine
- atan - C/C++ - computes arc tangent
- atan2 - C/C++ - computes arc tangent, using signs to determine quadrants
- Hyperbolic functions
- sinh - C/C++ - computes hyperbolic sine
- cosh - C/C++ - computes hyperbolic cosine
- tanh - C/C++ - computes hyperbolic tangent
- asinh - C99/C++11 - computes hyperbolic arc sine
- acosh - C99/C++11 - computes hyperbolic arc cosine
- atanh - C99/C++11 - computes hyperbolic arc tangent
- Error and gamma functions
- erf - C99/C++11 - computes error function
- erfc - C99/C++11 - computes complementary error function
- lgamma - C99/C++11 - computes natural logarithm of the gamma function
- tgamma - C99/C++11 - computes gamma function
- Nearest integer floating point operations
- ceil - C/C++ - returns the nearest integer not less than the given value
- floor - C/C++ - returns the nearest integer not greater than the given value
- trunc - C99/C++11 - returns the nearest integer not greater in magnitude than the given value
- round, lround, llround - C99/C++11 - returns the nearest integer, rounding away from zero in halfway cases
- nearbyint - C99/C++11 - returns the nearest integer using current rounding mode
- rint, lrint, llrint - C99/C++11 - returns the nearest integer using current rounding mode with exception if the result differs
- Floating point manipulation functions
- frexp - C/C++ - decomposes a number into significand and a power of 2
- ldexp - C/C++ - multiplies a number by 2 raised to a power
- modf - C/C++ - decomposes a number into integer and fractional parts
- scalbn, scalbln - C99/C++11 - multiplies a number by FLT_RADIX raised to a power
- nextafter, nexttoward - C99/C++11 - returns next representable floating point value towards the given value
- copysign - C/C++ - copies the sign of a floating point value
- Classification
- fpclassify - C99/C++11 - categorizes the given floating point value
- isfinite - C99/C++11 - checks if the given number has finite value
- isinf - C99/C++11 - checks if the given number is infinite
- isnan - C99/C++11 - checks if the given number is NaN
- isnormal - C99/C++11 - checks if the given number is normal
- signbit - C99/C++11 - checks if the given number is negative
Floating point environment
Declaration | Description |
---|---|
int (int excepts); |
clear exceptions specified by excepts |
int (fenv_t *penv); |
store current floating-point environment in penv |
int (fexcept_t *pflag, int excepts); |
store current status flags in pflags |
int (void); |
retrieve current rounding direction |
int (fenv_t *penv); |
save current floating-point environment to penv and clear all exceptions |
int (int excepts); |
raise floating-point exceptions |
int (const fenv_t *penv); |
set current floating-point environment to penv |
int (const fexcept_t *pflags, int excepts); |
set current status flags to those stored in pflags |
int (int round); |
set current rounding direction to round |
int (int excepts); |
test whether certain exceptions have been raised |
int (const fenv_t *penv); |
restore floating-point environment penv, but keep current exceptions |
int fesetprec(int prec) |
Sets the precision mode to the value specified by prec. |
Complex numbers
C99 adds a new _Complex keyword that provides support for complex numbers. Any floating point type can be modified with _Complex. In that case, a variable of such type contains a pair of floating point numbers and in such a way defines a complex number. C++ does not provide complex numbers in backwards compatible way. As an alternative, std::complex
can be used.
All operations on complex numbers are defined in complex.h header.
- Basic operations
- cabs- C99 - computes absolute value
- carg- C99 - computes argument of a complex number
- cimag- C99 - computes imaginary part of a complex number
- creal- C99 - computes real part of a complex number
- conj- C99 - computes complex conjugate
- cproj- C99 - computes complex projection into the Riemann sphere
- Exponentiation operations
- cexp- C99 - computes complex exponential
- clog- C99 - computes complex logarithm
- csqrt- C99 - computes complex square root
- cpow- C99 - computes complex power
- Trigonometric operations
- csin- C99 - computes complex sine
- ccos- C99 - computes complex cosine
- ctan- C99 - computes complex tangent
- casin- C99 - computes complex arc sine
- cacos- C99 - computes complex arc cosine
- catan- C99 - computes complex arc tangent
- Hyperbolic operations
- csinh- C99 - computes complex hyperbolic sine
- ccosh- C99 - computes complex hyperbolic cosine
- ctanh- C99 - computes complex hyperbolic tangent
- casinh- C99 - computes complex hyperbolic arc sine
- cacosh- C99 - computes complex hyperbolic arc cosine
- catanh- C99 - computes complex hyperbolic arc tangent
Type-generic functions
The header tgmath.h defines a type-generic macro for each mathematical function, so that the same function name can be used to call functions accepting different types of the arguments.
References
- ^ a b ISO/IEC 9899:1999 specification (PDF). p. 224, § 7.12.