C++ : The Math Library

 

How to Include

To use the standard math library, includ the following at the top of the file
#include <math.h>

Trigonometric functions

The functions sin(x), cos(x), and tan(x) each take x in radians. They both take and return doubles.

asin(x) returns the arc sine of x in the range -pi/2 to pi/2.
acos(x) returns the arc cosine of x in the range 0 to pi.
atan(x) returns the arc tangent of x in the range -pi/2 to pi/2.

The math library also contains the functions atan2(y,x) and hypot(x,y) for converting rectangular coordinates to polar coordinates.

 

Exponentials

exp(x) computes the exponential function e^x.
expm1(x) computes (e^x)-1 accurately even for tiny x.
log(x) computes the natural logarithm of x.
log1p(x) computes log(1+x) accurately even for tiny x.
log10(x) computes the base-10 logarithm of x.

Powers

pow(x,y) returns x raised to the power y.
sqrt(x) returns the square root of x
cbrt(x) returns the cube root of x

Additional functions

ceil(x) returns the smallest integer >= x.
floor(x) returns the largest integer <= x.
fabs(x) returns the absolute value of x.

 

Constants

The following are a subset of the constants in the math library:

e           M_E             2.7182818284590452354
pi          M_PI            3.14159265358979323846
pi/2        M_PI_2          1.57079632679489661923
sqrt(2)     M_SQRT2         1.41421356237309504880
1/sqrt(2)   M_SQRT1_2       0.70710678118654752440

 

© Lynne Grewe