{{Short description|C standard library header file}} {{C Standard Library}} '''C mathematical operations''' are a group of functions in the standard library of the C programming language implementing basic mathematical functions.<ref name="c99">{{cite book | url=https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf | title=ISO/IEC 9899:1999 specification | at=p. 212, § 7.12 }}</ref><ref name="c_primer">{{cite book|title=C primer plus | first=Stephen | last=Prata | year=2004 | publisher=Sams Publishing | isbn=0-672-32696-5 | at=Appendix B, Section V: The Standard ANSI C Library with C99 Additions}}</ref> 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<!-- Remove the word "deprecated" when C++23 comes out, which no longer considers them deprecated --> compatibility feature).
== Overview of functions ==
Most of the mathematical functions, which use floating-point numbers, are defined in <code><math.h></code> (<code><cmath></code> header in C++). The functions that operate on integers, such as <code>abs</code>, <code>labs</code>, <code>div</code>, and <code>ldiv</code>, are instead defined in the <code><stdlib.h></code> header (<code><cstdlib></code> header in C++).
Any functions that operate on angles use radians as the unit of angle.<ref name="c99"/>
Not all of these functions are available in the C89 version of the standard. For those that are, the functions accept only type <code>double</code> for the floating-point arguments, leading to expensive type conversions in code that otherwise used single-precision <code>float</code> values. In C99, this shortcoming was fixed by introducing new sets of functions that work on <code>float</code> and <code>long double</code> arguments. Those functions are identified by <code>f</code> and <code>l</code> suffixes respectively.<ref name="c_primer_c99">{{cite book|title=C primer plus | first=Stephen | last=Prata | year=2004 | publisher=Sams Publishing | isbn=0-672-32696-5 |at=Appendix B, Section VIII: C99 Numeric Computational Enhancements}}</ref>
{| class="wikitable" style="font-size:0.85em" ! ! Function ! Description |- ! rowspan=11 | | {{anchor|abs}}<code>[https://en.cppreference.com/w/c/numeric/math/abs abs]</code><br/>{{anchor|labs}}<code>[https://en.cppreference.com/w/c/numeric/math/abs labs]</code><br/>{{anchor|llabs}}<code>[https://en.cppreference.com/w/c/numeric/math/abs llabs]</code> | computes absolute value of an integer value |- | {{anchor|fabs}}<code>[https://en.cppreference.com/w/c/numeric/math/fabs fabs]</code> | computes absolute value of a floating-point value |- | {{anchor|div}}<code>[https://en.cppreference.com/w/c/numeric/math/div div]</code><br/>{{anchor|ldiv}}<code>[https://en.cppreference.com/w/c/numeric/math/div ldiv]</code><br/>{{anchor|lldiv}}<code>[https://en.cppreference.com/w/c/numeric/math/div lldiv]</code> | computes the quotient and remainder of integer division |- | {{anchor|fmod}}<code>[https://en.cppreference.com/w/c/numeric/math/fmod fmod]</code> | remainder of the floating-point division operation |- | {{anchor|remainder}}<code>[https://en.cppreference.com/w/c/numeric/math/remainder remainder]</code> | signed remainder of the division operation |- | {{anchor|remquo}}<code>[https://en.cppreference.com/w/c/numeric/math/remquo remquo]</code> | signed remainder as well as the three last bits of the division operation |- | {{anchor|fma}}<code>[http://en.cppreference.com/w/c/numeric/math/fma fma]</code> | fused multiply-add operation |- | {{anchor|fmax}}<code>[https://en.cppreference.com/w/c/numeric/math/fmax fmax]</code> | larger of two floating-point values |- | {{anchor|fmin}}<code>[https://en.cppreference.com/w/c/numeric/math/fmin fmin]</code> | smaller of two floating-point values |- | {{anchor|fdim}}<code>[https://en.cppreference.com/w/c/numeric/math/fdim fdim]</code> | positive difference of two floating-point values |- | {{anchor|nan}}<code>[https://en.cppreference.com/w/c/numeric/math/nan nan]</code><br/>{{anchor|nanf}}<code>[https://en.cppreference.com/w/c/numeric/math/nan nanf]</code><br/>{{anchor|nanl}}<code>[https://en.cppreference.com/w/c/numeric/math/nan nanl]</code> | returns a NaN (not-a-number) |- ! rowspan=9 | Exponential<br/>functions | {{anchor|exp}}<code>[http://en.cppreference.com/w/c/numeric/math/exp exp]</code> | returns e raised to the given power |- | {{anchor|exp2}}<code>[https://en.cppreference.com/w/c/numeric/math/exp2 exp2]</code> | returns 2 raised to the given power |- | {{anchor|expm1}}<code>[https://en.cppreference.com/w/c/numeric/math/expm1 expm1]</code> | returns e raised to the given power, minus one |- | {{anchor|log}}<code>[http://en.cppreference.com/w/c/numeric/math/log log]</code> | computes natural logarithm (to base e) |- | {{anchor|log2}}<code>[https://en.cppreference.com/w/c/numeric/math/log2 log2]</code> | computes binary logarithm (to base 2) |- | {{anchor|log10}}<code>[https://en.cppreference.com/w/c/numeric/math/log10 log10]</code> | computes common logarithm (to base 10) |- | {{anchor|log1p}}<code>[https://en.cppreference.com/w/c/numeric/math/log1p log1p]</code> | computes natural logarithm (to base e) of 1 plus the given number |- | {{anchor|ilogb}}<code>[https://en.cppreference.com/w/c/numeric/math/ilogb ilogb]</code> | extracts exponent of the number |- | {{anchor|logb}}<code>[https://en.cppreference.com/w/c/numeric/math/logb logb]</code> | extracts exponent of the number |- ! rowspan=4 | Power<br/>functions | {{anchor|sqrt}}<code>[https://en.cppreference.com/w/c/numeric/math/sqrt sqrt]</code> | computes square root |- | {{anchor|cbrt}}<code>[https://en.cppreference.com/w/c/numeric/math/cbrt cbrt]</code> | computes cubic root |- | {{anchor|hypot}}<code>[https://en.cppreference.com/w/c/numeric/math/hypot hypot]</code> | computes square root of the sum of the squares of two given numbers |- | {{anchor|pow}}<code>[https://en.cppreference.com/w/c/numeric/math/pow pow]</code> | raises a number to the given power<ref>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).</ref> |- ! rowspan=7 | Trigonometric<br/>functions | {{anchor|sin}}<code>[http://en.cppreference.com/w/c/numeric/math/sin sin]</code> | computes sine |- | {{anchor|cos}}<code>[http://en.cppreference.com/w/c/numeric/math/cos cos]</code> | computes cosine |- | {{anchor|tan}}<code>[http://en.cppreference.com/w/c/numeric/math/tan tan]</code> | computes tangent |- | {{anchor|asin}}<code>[http://en.cppreference.com/w/c/numeric/math/asin asin]</code> | computes arc sine |- | {{anchor|acos}}<code>[http://en.cppreference.com/w/c/numeric/math/acos acos]</code> | computes arc cosine |- | {{anchor|atan}}<code>[http://en.cppreference.com/w/c/numeric/math/atan atan]</code> | computes arc tangent |- | {{anchor|atan2}}<code>[https://en.cppreference.com/w/c/numeric/math/atan2 atan2]</code> | computes arc tangent, using signs to determine quadrants |- ! rowspan=6 | Hyperbolic<br/>functions | {{anchor|sinh}}<code>[https://en.cppreference.com/w/c/numeric/math/sinh sinh]</code> | computes hyperbolic sine |- | {{anchor|cosh}}<code>[https://en.cppreference.com/w/c/numeric/math/cosh cosh]</code> | computes hyperbolic cosine |- | {{anchor|tanh}}<code>[https://en.cppreference.com/w/c/numeric/math/tanh tanh]</code> | computes hyperbolic tangent |- | {{anchor|asinh}}<code>[https://en.cppreference.com/w/c/numeric/math/asinh asinh]</code> | computes hyperbolic arc sine |- | {{anchor|acosh}}<code>[https://en.cppreference.com/w/c/numeric/math/acosh acosh]</code> | computes hyperbolic arc cosine |- | {{anchor|atanh}}<code>[https://en.cppreference.com/w/c/numeric/math/atanh atanh]</code> | computes hyperbolic arc tangent |- ! rowspan=4 | Error and<br/>gamma<br/> functions | {{anchor|erf}}<code>[http://en.cppreference.com/w/c/numeric/math/erf erf]</code> | computes error function |- | {{anchor|erfc}}<code>[https://en.cppreference.com/w/c/numeric/math/erfc erfc]</code> | computes complementary error function |- | {{anchor|lgamma}}<code>[https://en.cppreference.com/w/c/numeric/math/lgamma lgamma]</code> | computes natural logarithm of the absolute value of the gamma function |- | {{anchor|tgamma}}<code>[https://en.cppreference.com/w/c/numeric/math/tgamma tgamma]</code> | computes gamma function |- ! rowspan=6 | Nearest<br/>integer<br/>floating-<br/>point<br/>operations | {{anchor|ceil}}<code>[https://en.cppreference.com/w/c/numeric/math/ceil ceil]</code> | returns the nearest integer not less than the given value |- | {{anchor|floor}}<code>[https://en.cppreference.com/w/c/numeric/math/floor floor]</code> | returns the nearest integer not greater than the given value |- | {{anchor|trunc}}<code>[https://en.cppreference.com/w/c/numeric/math/trunc trunc]</code> | returns the nearest integer not greater in magnitude than the given value |- | {{anchor|round}}<code>[https://en.cppreference.com/w/c/numeric/math/round round]</code><br/>{{anchor|lround}}<code>[https://en.cppreference.com/w/c/numeric/math/round lround]</code><br/>{{anchor|llround}}<code>[https://en.cppreference.com/w/c/numeric/math/round llround]</code> | returns the nearest integer, rounding away from zero in halfway cases |- | {{anchor|nearbyint}}<code>[https://en.cppreference.com/w/c/numeric/math/nearbyint nearbyint]</code> | returns the nearest integer using current rounding mode |- | {{anchor|rint}}<code>[https://en.cppreference.com/w/c/numeric/math/rint rint]</code><br/>{{anchor|lrint}}<code>[https://en.cppreference.com/w/c/numeric/math/rint lrint]</code><br/>{{anchor|llrint}}<code>[https://en.cppreference.com/w/c/numeric/math/rint llrint]</code> | returns the nearest integer using current rounding mode with exception if the result differs |- ! rowspan=6 | Floating-<br/>point<br/>manipulation<br/>functions | {{anchor|frexp}}<code>[https://en.cppreference.com/w/c/numeric/math/frexp frexp]</code> | decomposes a number into significand and a power of 2 |- | {{anchor|ldexp}}<code>[https://en.cppreference.com/w/c/numeric/math/ldexp ldexp]</code> | multiplies a number by 2 raised to a power |- | {{anchor|modf}}<code>[https://en.cppreference.com/w/c/numeric/math/modf modf]</code> | decomposes a number into integer and fractional parts |- | {{anchor|scalbn}}<code>[https://en.cppreference.com/w/c/numeric/math/scalbn scalbn]</code><br/>{{anchor|scalbln}}<code>[https://en.cppreference.com/w/c/numeric/math/scalbn scalbln]</code> | multiplies a number by FLT_RADIX raised to a power |- | {{anchor|nextafter}}<code>[https://en.cppreference.com/w/c/numeric/math/nextafter nextafter]</code><br/>{{anchor|nexttoward}}<code>[https://en.cppreference.com/w/c/numeric/math/nexttoward nexttoward]</code> | returns next representable floating-point value towards the given value |- | {{anchor|copysign}}<code>[https://en.cppreference.com/w/c/numeric/math/copysign copysign]</code> | copies the sign of a floating-point value |- ! rowspan=6 | Classification | {{anchor|fpclassify}}<code>[https://en.cppreference.com/w/c/numeric/math/fpclassify fpclassify]</code> | categorizes the given floating-point value |- | {{anchor|isfinite}}<code>[https://en.cppreference.com/w/c/numeric/math/isfinite isfinite]</code> | checks if the argument has finite value |- | {{anchor|isinf}}<code>[https://en.cppreference.com/w/c/numeric/math/isinf isinf]</code> | checks if the argument is infinite |- | {{anchor|isnan}}<code>[https://en.cppreference.com/w/c/numeric/math/isnan isnan]</code> | checks if the argument is NaN |- | {{anchor|isnormal}}<code>[https://en.cppreference.com/w/c/numeric/math/isnormal isnormal]</code> | checks if the argument is normal |- | {{anchor|signbit}}<code>[https://en.cppreference.com/w/c/numeric/math/signbit signbit]</code> | checks if the sign of the argument is negative |}
=== {{anchor|fenv.h}}Floating-point environment ===
C99 adds several functions and types for fine-grained control of floating-point environment.<ref name="c_primer_c99"/> 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 <code><fenv.h></code> header (<code><cfenv></code> in C++).
{| class="wikitable" style="font-size:0.85em" ! Function ! Description |- | {{anchor|feclearexcept}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feclearexcept feclearexcept]</code> | clears exceptions (C99) |- | {{anchor|fegetenv}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feenv fegetenv]</code> | stores current floating-point environment (C99) |- | {{anchor|fegetexceptflag}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feexceptflag fegetexceptflag]</code> | stores current status flags (C99) |- | {{anchor|fegetround}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feround fegetround]</code> | retrieves current rounding direction (C99) |- | {{anchor|feholdexcept}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feholdexcept feholdexcept]</code> | saves current floating-point environment and clears all exceptions (C99) |- | {{anchor|feraiseexcept}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feraiseexcept feraiseexcept]</code> | raises a floating-point exception (C99) |- | {{anchor|fesetenv}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feenv fesetenv]</code> | sets current floating-point environment (C99) |- | {{anchor|fesetexceptflag}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feexceptflag fesetexceptflag]</code> | sets current status flags (C99) |- | {{anchor|fesetround}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feround fesetround]</code> | sets current rounding direction (C99) |- | {{anchor|fetestexcept}}<code>[https://en.cppreference.com/w/c/numeric/fenv/fetestexcept fetestexcept]</code> | tests whether certain exceptions have been raised (C99) |- | {{anchor|feupdateenv}}<code>[https://en.cppreference.com/w/c/numeric/fenv/feupdateenv feupdateenv]</code> | restores floating-point environment, but keeps current exceptions (C99) |}
=== {{anchor|complex.h}} Complex numbers ===
C99 adds a new <code>_Complex</code> keyword (and <code>complex</code> convenience macro; only available if the <code><complex.h></code> header is included) that provides support for complex numbers. Any floating-point type can be modified with <code>complex</code>, 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 {{cpp|std::complex}}.
All operations on complex numbers are defined in the <code><complex.h></code> header. As with the real-valued functions, an <code>f</code> or <code>l</code> suffix denotes the <code>float complex</code> or <code>long double complex</code> variant of the function.
{| class="wikitable" style="font-size:0.85em" ! ! Function ! Description |- ! rowspan=6 | Basic<br/>operations | {{anchor|cabs}}<code>[https://en.cppreference.com/w/c/numeric/complex/cabs cabs]</code> | computes absolute value (C99) |- | {{anchor|carg}}<code>[https://en.cppreference.com/w/c/numeric/complex/carg carg]</code> | computes argument of a complex number (C99) |- | {{anchor|cimag}}<code>[https://en.cppreference.com/w/c/numeric/complex/cimag cimag]</code> | computes imaginary part of a complex number (C99) |- | {{anchor|creal}}<code>[https://en.cppreference.com/w/c/numeric/complex/creal creal]</code> | computes real part of a complex number (C99) |- | {{anchor|conj}}<code>[https://en.cppreference.com/w/c/numeric/complex/conj conj]</code> | computes complex conjugate (C99) |- | {{anchor|cproj}}<code>[https://en.cppreference.com/w/c/numeric/complex/cproj cproj]</code> | computes complex projection into the Riemann sphere (C99) |- ! rowspan=4 | Exponentiation<br/>operations | {{anchor|cexp}}<code>[https://en.cppreference.com/w/c/numeric/complex/cexp cexp]</code> | computes complex exponential (C99) |- | {{anchor|clog}}<code>[https://en.cppreference.com/w/c/numeric/complex/clog clog]</code> | computes complex logarithm (C99) |- | {{anchor|csqrt}}<code>[https://en.cppreference.com/w/c/numeric/complex/csqrt csqrt]</code> | computes complex square root (C99) |- | {{anchor|cpow}}<code>[https://en.cppreference.com/w/c/numeric/complex/cpow cpow]</code> | computes complex power (C99) |- ! rowspan=6 | Trigonometric<br/>operations | {{anchor|csin}}<code>[http://en.cppreference.com/w/c/numeric/complex/csin csin]</code> | computes complex sine (C99) |- | {{anchor|ccos}}<code>[http://en.cppreference.com/w/c/numeric/complex/ccos ccos]</code> | computes complex cosine (C99) |- | {{anchor|ctan}}<code>[http://en.cppreference.com/w/c/numeric/complex/ctan ctan]</code> | computes complex tangent (C99) |- | {{anchor|casin}}<code>[http://en.cppreference.com/w/c/numeric/complex/casin casin]</code> | computes complex arc sine (C99) |- | {{anchor|cacos}}<code>[http://en.cppreference.com/w/c/numeric/complex/cacos cacos]</code> | computes complex arc cosine (C99) |- | {{anchor|catan}}<code>[http://en.cppreference.com/w/c/numeric/complex/catan catan]</code> | computes complex arc tangent (C99) |- ! rowspan=6 | Hyperbolic<br/>operations | {{anchor|csinh}}<code>[https://en.cppreference.com/w/c/numeric/complex/csinh csinh]</code> | computes complex hyperbolic sine (C99) |- | {{anchor|ccosh}}<code>[https://en.cppreference.com/w/c/numeric/complex/ccosh ccosh]</code> | computes complex hyperbolic cosine (C99) |- | {{anchor|ctanh}}<code>[https://en.cppreference.com/w/c/numeric/complex/ctanh ctanh]</code> | computes complex hyperbolic tangent (C99) |- | {{anchor|casinh}}<code>[https://en.cppreference.com/w/c/numeric/complex/casinh casinh]</code> | computes complex hyperbolic arc sine (C99) |- | {{anchor|cacosh}}<code>[https://en.cppreference.com/w/c/numeric/complex/cacosh cacosh]</code> | computes complex hyperbolic arc cosine (C99) |- | {{anchor|catanh}}<code>[https://en.cppreference.com/w/c/numeric/complex/catanh catanh]</code> | computes complex hyperbolic arc tangent (C99) |}
A few more complex functions are "reserved for future use in C99".<ref>man cerf(3), man cerfc(3), see e.g. https://linux.die.net/man/3/cerf.</ref> Implementations are provided by open-source projects that are not part of the standard library. {| class="wikitable" style="font-size:0.85em" ! ! Function ! Description |- ! rowspan=2 | Error functions | {{anchor|cerf}}<code>[http://apps.jcns.fz-juelich.de/libcerf cerf]</code> | computes the complex error function (C99) |- | {{anchor|cerfc}}<code>[http://apps.jcns.fz-juelich.de/libcerf cerfc]</code> | computes the complex complementary error function (C99) |- |}
=== Type-generic functions ===
{{anchor|tgmath.h}} The header <code><tgmath.h></code> defines a type-generic macro for each mathematical function defined in <code><math.h></code> and <code><complex.h></code>. 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: <code>float</code>, <code>double</code> and <code>long double</code>, and their <code>complex</code> variants. The type-generic macros that correspond to a function that is defined for only real numbers encapsulates a total of 3 different functions: <code>float</code>, <code>double</code> and <code>long double</code> variants of the function.
The C++ language includes native support for function overloading and thus does not provide the <code><tgmath.h></code> header even as a compatibility feature.
=== Random-number generation ===
{{anchor|stdlib.h}} The header <code><stdlib.h></code> (<code><cstdlib></code> in C++) defines several functions that can be used for statistically random number generation.<ref>{{cite web |url=https://www.gnu.org/software/libc/manual/html_node/ISO-Random.html |title=The GNU C Library – ISO Random |access-date=18 July 2018}}</ref>
{| class="wikitable" style="font-size:0.85em" ! Function ! Description |- | {{anchor|rand}}<code>[https://en.cppreference.com/w/c/numeric/random/rand rand]</code> | generates a pseudo-random number between 0 and <code>RAND_MAX</code>, inclusive. |- | {{anchor|srand}}<code>[https://en.cppreference.com/w/c/numeric/random/srand srand]</code> | initializes a pseudo-random number generator |- | {{anchor|arc4random}}<code>arc4random</code> | generates a pseudo-random number between 0 and <code>UINT32_MAX</code>, usually using a better algorithm than <code>rand</code> |- | {{anchor|arc4random}}<code>arc4random_uniform</code> | generates a pseudo-random number between 0 and a maximum value. |- | {{anchor|arc4random}}<code>arc4random_buf</code> | fill a buffer with a pseudo-random bitstream. |- | {{anchor|arc4random}}<code>arc4random_stir</code> | initializes a pseudo-random number generator. |}
The <code>arc4random</code> family of random number functions are not defined in POSIX standard, but is found in some common <code>libc</code> implementations. It used to refer to the keystream generator of a leaked version of RC4 cipher (hence "'''a'''lleged '''RC4'''"), but different algorithms, usually from other ciphers like ChaCha20, have been implemented since using the same name.
The quality of randomness from <code>rand</code> are usually too weak to be even considered statistically random, and it requires explicit seeding. It is usually advised to use <code>arc4random</code> instead of <code>rand</code> when possible. Some C libraries implement <code>rand</code> using <code>arc4random_uniform</code> internally.
=== Special functions === Beginning in C++17, C++ introduces special functions into the <code><cmath></code> header.
{| class="wikitable" style="font-size:0.85em" ! Function ! Description |- | {{anchor|assoc_laguerre}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/assoc_laguerre.html assoc_laguerre]</code> | computes associated Laguerre polynomials |- | {{anchor|assoc_legendre}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/assoc_legendre.html assoc_legendre]</code> | computes associated Legendre polynomials |- | {{anchor|beta}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/beta.html beta]</code> | computes beta function |- | {{anchor|comp_ellint_1}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/comp_ellint_1.html comp_ellint_1]</code> | computes complete elliptic integral of the first kind |- | {{anchor|comp_ellint_2}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/comp_ellint_2.html comp_ellint_2]</code> | computes complete elliptic integral of the second kind |- | {{anchor|comp_ellint_3}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/comp_ellint_3.html comp_ellint_3]</code> | computes complete elliptic integral of the third kind |- | {{anchor|cyl_bessel_i}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/cyl_bessel_i.html cyl_bessel_i]</code> | computes regular modified cylindrical Bessel functions |- | {{anchor|cyl_bessel_j}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/cyl_bessel_j.html cyl_bessel_j]</code> | computes cylindrical Bessel functions of the first kind |- | {{anchor|cyl_bessel_k}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/cyl_bessel_k.html cyl_bessel_k]</code> | computes irregular modified cylindrical Bessel functions |- | {{anchor|cyl_neumann}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/cyl_neumann.html cyl_neumann]</code> | computes cylindrical Neumann functions |- | {{anchor|ellint_1}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/ellint_1.html ellint_1]</code> | computes incomplete elliptic integral of the first kind |- | {{anchor|ellint_2}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/ellint_2.html ellint_2]</code> | computes incomplete elliptic integral of the second kind |- | {{anchor|ellint_3}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/ellint_3.html ellint_3]</code> | computes incomplete elliptic integral of the third kind |- | {{anchor|expint}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/expint.html expint]</code> | computes exponential integral |- | {{anchor|hermite}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/hermite.html hermite]</code> | computes Hermite polynomials |- | {{anchor|legendre}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/legendre.html legendre]</code> | computes Legendre polynomials |- | {{anchor|laguerre}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/laguerre.html laguerre]</code> | computes Laguerre polynomials |- | {{anchor|riemann_zeta}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/riemann_zeta.html riemann_zeta]</code> | computes Riemann zeta function |- | {{anchor|sph_bessel}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/sph_bessel.html sph_bessel]</code> | computes spherical Bessel functions of the first kind |- | {{anchor|sph_legendre}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/sph_legendre.html sph_legendre]</code> | computes spherical associated Legendre functions |- | {{anchor|sph_neumann}}<code>[https://en.cppreference.com/w/cpp/numeric/special_functions/sph_neumann.html sph_neumann]</code> | computes spherical Neumann functions |}
== Implementations ==
Under POSIX systems like Linux and BSD, the mathematical functions (as declared in <code><math.h></code>) are bundled separately in the mathematical library <code>{{vanchor|libm}}</code>. Therefore, if any of those functions are used, the linker must be given the directive <code>-lm</code>. There are various <code>libm</code> implementations, including:
* GNU libc's [https://www.gnu.org/software/libc/manual/html_node/Mathematics.html libm] * AMD's [https://web.archive.org/web/20200405175916/https://developer.amd.com/amd-aocl/amd-math-library-libm/ libm], [https://github.com/amd/win-libm github], used almost as is by Windows * Intel C++ Compiler libm * Red Hat's [https://sourceware.org/newlib/libm.html libm] (Newlib) * Sun's [https://netlib.org/fdlibm/readme FDLIBM], which was used as the basis for FreeBSD's [https://svnweb.freebsd.org/base/head/lib/msun/ msun] and OpenBSD's [https://cvsweb.openbsd.org/cgi-bin/cvsweb/src/lib/libm/ libm], both of which in turn were the basis of Julia's [https://openlibm.org/ OpenLibm] * musl's [https://git.musl-libc.org/cgit/musl/tree/src/math libm], based on the BSD <code>libms</code> 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)<ref>{{cite web |title=Math Functions — The LLVM C Library |url=https://libc.llvm.org/math/index.html |website=libc.llvm.org}}</ref> * Arénaire project's [https://web.archive.org/web/20161027224938/http://lipforge.ens-lyon.fr/www/crlibm CRlibm] (correctly rounded libm), and its successor [https://web.archive.org/web/20210924212445/http://metalibm.org/ MetaLibm], which uses Remez algorithm to automatically generate approximations that are formally proven. * Rutger's RLIBM, which provides correctly rounded functions in single precision.<ref>{{cite web |title=RLibm: Rutgers Architecture and Programming Languages Lab's Correctly Rounded Libm |url=https://people.cs.rutgers.edu/~sn349/rlibm/ |website=people.cs.rutgers.edu}}</ref> {{see also|Rounding#Table-maker's dilemma}}
Implementations not necessarily under a name of {{code|libm}} include: * Arm's {{GitHub|ARM-software/optimized-routines|optimized math routines|link=hidden}} * {{GitHub|kthohr/gcem|GCE-Math|link=hidden}} is a version of C/C++ math functions written for C++ {{code|constexpr}} (compile-time calculation) * [https://core-math.gitlabpages.inria.fr/ CORE-MATH], correctly rounded for single and double precision. * SIMD (vectorized) math libraries include [https://sleef.org/ SLEEF], [https://bitbucket.org/MDukhan/yeppp Yeppp!] {{Webarchive|url=https://web.archive.org/web/20200714015029/https://bitbucket.org/MDukhan/yeppp |date=2020-07-14 }}, and Agner Fog's VCL, plus a few closed-source ones like SVML and DirectXMath.<ref>{{cite web |last1=Cordes |first1=Peter |title=intel - Where is Clang's '_mm256_pow_ps' intrinsic? |url=https://stackoverflow.com/a/36637424 |website=Stack Overflow}}</ref>
== See also ==
== References ==
{{reflist}}
== External links ==
{{wikibooks|C Programming|C mathematical operations|C Programming/C Reference}} * {{man|bd|math.h|SUS|mathematical declarations}} * [https://en.cppreference.com/w/c/numeric/math C reference for math functions]
{{CProLang|state=expanded}}
Category:C standard library