C Programming/math.h
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 double
s for floatingpoint arguments, unless otherwise specified. In C99, to work with float
s or long double
s, 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.
PreC99 functions[edit  edit source]
Name  Description 

acos 
inverse cosine 
asin 
inverse sine 
atan 
oneparameter inverse tangent 
atan2 
twoparameter inverse tangent 
ceil 
ceiling, the smallest integer not less than parameter 
cos 
cosine 
cosh 
hyperbolic cosine 
cbrt 
cube root 
exp 
exponential function 
fabs 
absolute value (of a floatingpoint number) 
floor 
floor, the largest integer not greater than parameter 
fmod 
floatingpoint remainder: x  y*(int)(x/y)

frexp 
break floatingpoint number down into mantissa and exponent 
ldexp 
scale floatingpoint number by exponent (see article) 
log 
natural logarithm 
log10 
base10 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, x^{y} 
sin 
sine 
sinh 
hyperbolic sine 
sqrt 
square root 
tan 
tangent 
tanh 
hyperbolic 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[edit  edit source]
Name  Description 

acosh 
inverse hyperbolic cosine 
asinh 
inverse hyperbolic sine 
atanh 
inverse hyperbolic tangent 
cbrt 
cube root 
copysign(x,y) 
returns the value of x with the sign of y 
erf 
error function 
erfc 
complementary error function 
exp2(x) 
raise 2 to the power of x, 2^{x} 
expm1(x) 
one less than the exponential of x, e^{x} − 1 
fdim(x,y) 
positive difference between x and y, fmax(x−y, 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(x^{2} + y^{2}) 
ilogb 
the exponent of a floatingpoint value, converted to an int

lgamma 
natural log of the absolute value of the gamma function 
llrint 
round to integer (returns long long ) using current rounding mode

lrint 
round to integer (returns long ) using current rounding mode

llround 
round to integer (returns long long )

lround 
round to integer (returns long )

log1p(x) 
natural logarithm of 1 + x 
log2 
base2 logarithm 
logb 
extract exponent from floatingpoint number 
nan(s) 
returns NaN, possibly using string argument 
nearbyint 
round floatingpoint 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

rint 
round to integer (returns double ) using current rounding mode

round 
round to integer (returns double ), rounding halfway cases away from zero

scalbln(x,n) 
x * FLT_RADIX ^{n} (n is long )

scalbn(x,n) 
x * FLT_RADIX ^{n} (n is int )

tgamma 
gamma function 
trunc 
truncate floatingpoint number 
XSI Extensions[edit  edit source]
Extra functions may be available as X/Open System Interfaces Extensions. These are not present in any ANSI or ISO C standard.
Name  Description 

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_RADIX ^{y} (x and y are double s)

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
tostring conversion functions ecvt
, fcvt
and gcvt
have been deprecated in favour of sprintf
.
Mathematical constants (not standard)[edit  edit source]
Name  Description 

M_E 
The base of natural logarithms. 
M_LOG2E 
The logarithm to base 2 of M_E. 
M_LOG10E 
The logarithm to base 10 of M_E. 
M_LN2 
The natural logarithm of 2. 
M_LN10 
The natural logarithm of 10. 
M_PI 
Pi, the ratio of a circle's circumference to its diameter. 
M_PI_2 
Pi divided by two. 
M_PI_4 
Pi divided by four. 
M_1_PI 
The reciprocal of pi (1/pi). 
M_2_PI 
Two times the reciprocal of pi. 
M_2_SQRTPI 
Two times the reciprocal of the square root of pi. 
M_SQRT2 
The square root of two. 
M_SQRT1_2 
The 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.
External links[edit  edit source]
 The Single UNIX® Specification, Issue 7 from The Open Group : mathematical declarations – Base Definitions Reference,
 C++ reference for math functions inherited from C
References[edit  edit source]
 ↑ Math Constants, MSDN.