< C Programming

math.h is a header file in the standard library of the C programming language designed for basic mathematical operations. Most of the functions involve the use of floating point numbers. C++ also implements these functions for compatibility reasons and declares them in the header cmath (the C99 functions are not available in the current C++ standard, C++ 98).

All functions that take or return an angle work in radians.

All functions take doubles for floating-point arguments, unless otherwise specified. In C99, to work with floats or long doubles, append an f or an l to the name, respectively.

Mathematical library functions that operate on integers, such as abs, labs, div, and ldiv, are instead specified in the stdlib.h header.

Pre-C99 functions

NameDescription
acosinverse cosine
asininverse sine
atanone-parameter inverse tangent
atan2two-parameter inverse tangent
ceilceiling, the smallest integer not less than parameter
coscosine
coshhyperbolic cosine
cbrtcube root
expexponential function
fabsabsolute value (of a floating-point number)
floorfloor, the largest integer not greater than parameter
fmodfloating-point remainder: x - y*(int)(x/y)
frexpbreak floating-point number down into mantissa and exponent
ldexpscale floating-point number by exponent (see article)
lognatural logarithm
log10base-10 logarithm
modf(x,p)returns fractional part of x and stores integral part where pointer p points to
pow(x,y)raise x to the power of y, xy
sinsine
sinhhyperbolic sine
sqrtsquare root
tantangent
tanhhyperbolic tangent]]

(For functions to convert strings to floating point numbers (atof(), strtod(), etc.), see C Programming/C Reference/stdlib.h.)

(For functions to convert floating point numbers to strings (snprintf(), itoa(), etc.), see C Programming/C Reference/stdio.h and C_Programming/C_Reference/stdlib.h#itoa.)

C99 functions

NameDescription
acoshinverse hyperbolic cosine
asinhinverse hyperbolic sine
atanhinverse hyperbolic tangent
cbrtcube root
copysign(x,y)returns the value of x with the sign of y
erferror function
erfccomplementary error function
exp2(x)raise 2 to the power of x, 2x
expm1(x)one less than the exponential of x, ex 1
fdim(x,y)positive difference between x and y, fmax(xy, 0)
fma(x,y,z)multiply and add, (x * y) + z
fmax(x,y)largest value of x and y
fmin(x,y)smallest value of x and y
hypot(x,y)hypotenuse, sqrt(x2 + y2)
ilogbthe exponent of a floating-point value, converted to an int
lgammanatural log of the absolute value of the gamma function
llrintround to integer (returns long long) using current rounding mode
lrintround to integer (returns long) using current rounding mode
llroundround to integer (returns long long)
lroundround to integer (returns long)
log1p(x)natural logarithm of 1 + x
log2base-2 logarithm
logbextract exponent from floating-point number
nan(s)returns NaN, possibly using string argument
nearbyintround floating-point number to nearest integer
nextafter(x,y)returns next representable value after x (towards y)
nexttoward(x,y)same as nextafter, except y is always a long double
remainder(x,y)calculates remainder, as required by IEC 60559
remquo(x,y,p)same as remainder, but store quotient (as int) at target of pointer p
rintround to integer (returns double) using current rounding mode
roundround to integer (returns double), rounding halfway cases away from zero
scalbln(x,n)x * FLT_RADIXn (n is long)
scalbn(x,n)x * FLT_RADIXn (n is int)
tgammagamma function
trunctruncate floating-point number

XSI Extensions

Extra functions may be available as X/Open System Interfaces Extensions. These are not present in any ANSI or ISO C standard.

NameDescription
j0(x)Bessel function of x of the first kind of order 0
j1(x)Bessel function of x of the first kind of order 1
jn(n,x)Bessel function of x of the first kind of order n
scalb(x,y)x * FLT_RADIXy (x and y are doubles)
y0(x)Bessel function of x of the second kind of order 0
y1(x)Bessel function of x of the second kind of order 1
yn(n,x)Bessel function of x of the second kind of order n

The double-to-string conversion functions ecvt, fcvt and gcvt have been deprecated in favour of sprintf.

Mathematical constants (not standard)

NameDescription
M_EThe base of natural logarithms.
M_LOG2EThe logarithm to base 2 of M_E.
M_LOG10EThe logarithm to base 10 of M_E.
M_LN2The natural logarithm of 2.
M_LN10The natural logarithm of 10.
M_PIPi, the ratio of a circle's circumference to its diameter.
M_PI_2Pi divided by two.
M_PI_4Pi divided by four.
M_1_PIThe reciprocal of pi (1/pi).
M_2_PITwo times the reciprocal of pi.
M_2_SQRTPITwo times the reciprocal of the square root of pi.
M_SQRT2The square root of two.
M_SQRT1_2The reciprocal of the square root of two (also the square root of 1/2).

All values are of type double. As an extension, the GNU C library also defines these constants with type long double. The long double macros have a lowercase ‘l’ appended to their names: M_El, M_PIl, and so forth. These are only available if _GNU_SOURCE is defined.

Note: Some programs use a constant named PI which has the same value as M_PI. This constant is not standard; it may have appeared in some old AT&T headers, and is mentioned in Stroustrup's book on C++. It infringes on the user's name space, so the GNU C library does not define it. Fixing programs written to expect it is simple: replace PI with M_PI throughout, or put ‘-DPI=M_PI’ on the compiler command line.

While these constants are common, they are not part of the C standard, so most modern compilers require an explicit definition (such as _USE_MATH_DEFINES in Microsoft Visual C++ [1]) for them to be defined when including math.h.

References

  1. Math Constants, MSDN.
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.