# C Programming/C Reference/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 floating-point 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.

## Contents

## Pre-C99 functions[edit]

Name | Description |
---|---|

`acos` |
inverse cosine |

`asin` |
inverse sine |

`atan` |
one-parameter inverse tangent |

`atan2` |
two-parameter inverse tangent |

`ceil` |
ceiling, the smallest integer not less than parameter |

`cos` |
cosine |

`cosh` |
hyperbolic cosine |

`exp` |
exponential function |

`fabs` |
absolute value (of a floating-point number) |

`floor` |
floor, the largest integer not greater than parameter |

`fmod` |
floating-point remainder: `x - y*(int)(x/y)` |

`frexp` |
break floating-point number down into mantissa and exponent |

`ldexp` |
scale floating-point number by exponent (see article) |

`log` |
natural logarithm |

`log10` |
base-10 logarithm |

`modf(` |
returns fractional part of x and stores integral part where pointer p points to |

`pow(` |
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]

Name | Description |
---|---|

`acosh` |
inverse hyperbolic cosine |

`asinh` |
inverse hyperbolic sine |

`atanh` |
inverse hyperbolic tangent |

`cbrt` |
cube root |

`copysign(` |
returns the value of x with the sign of y |

`erf` |
error function |

`erfc` |
complementary error function |

`exp2(` |
raise 2 to the power of x, 2^{x} |

`expm1(` |
one less than the exponential of x, e − 1^{x} |

`fdim(` |
positive difference between x and y, fmax(x−y, 0) |

`fma(` |
multiply and add, (x * y) + z |

`fmax(` |
largest value of x and y |

`fmin(` |
smallest value of x and y |

`hypot(` |
hypotenuse, sqrt(x^{2} + y^{2}) |

`ilogb` |
the exponent of a floating-point 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(` |
natural logarithm of 1 + x |

`log2` |
base-2 logarithm |

`logb` |
extract exponent from floating-point number |

`nan(` |
returns NaN, possibly using string argument |

`nearbyint` |
round floating-point number to nearest integer |

`nextafter(` |
returns next representable value after x (towards y) |

`nexttoward(` |
same as `nextafter` , except y is always a `long double` |

`remainder(` |
calculates remainder, as required by IEC 60559 |

`remquo(` |
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 * `FLT_RADIX` ^{n} (n is `long` ) |

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

`tgamma` |
gamma function |

`trunc` |
truncate floating-point number |

## XSI Extensions[edit]

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(` |
Bessel function of x of the first kind of order 0 |

`j1(` |
Bessel function of x of the first kind of order 1 |

`jn(` |
Bessel function of x of the first kind of order n |

`scalb(` |
x * `FLT_RADIX` ^{y} (x and y are `double` s) |

`y0(` |
Bessel function of x of the second kind of order 0 |

`y1(` |
Bessel function of x of the second kind of order 1 |

`yn(` |
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)[edit]

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]

- The Single UNIX® Specification, Issue 7 from The Open Group : mathematical declarations – Base Definitions Reference,
- C++ reference for math functions inherited from C
- Dinkumware math.h reference, a reference of all
`math.h`

functions

## References[edit]

- ↑ Math Constants, MSDN.