The math.h header file includes declarations of mathematical functions.
All the functions return a value of type double.
Functions for Numerical Calculations
Function | Operation |
---|---|
floor(x) | Returns the largest integer that isn't greater than x as type double |
ceil(x) | Returns the smallest integer that isn't less than x as type double |
fabs(x) | Returns the absolute value of x |
log(x) | Returns the natural logarithm (base e) of x |
log10(x) | Returns the logarithm to base 10 of x |
exp(x) | Returns the value of ex |
sqrt(x) pow(x, y) | Returns the square root of x Returns the value xy |
There are versions of these for types float and long double that have f or l, respectively, appended to the function name.
ceilf() applies to float values and sqrtl() applies to long double values, for example.
Here are some examples demonstrating use of the functions:
double x = 2.25; double less = 0.0; double more = 0.0; double root = 0.0; less = floor(x); // Result is 2.0 more = ceil(x); // Result is 3.0 root = sqrt(x); // Result is 1.5
The following table lists trigonometric functions.
Those for types, float and type long double have f or l, respectively, appended to the name.
Arguments and values returned are of type float, type double, or type long double and angles are expressed in radians.
Function | Operation |
---|---|
sin(x) | Sine of x expressed in radians |
cos(x) | Cosine of x |
tan(x) | Tangent of x |
Here are some examples:
double angle = 45.0; // Angle in degrees double pi = 3.14159265; double sine = 0.0; double cosine = 0.0; sine = sin(pi*angle/180.0); // Angle converted to radians cosine = sin(pi*angle/180.0); // Angle converted to radians
Dividing an angle measured in degrees by 180 and multiplying by the value of p will produce the angle in radians, as required by these functions.
The inverse trigonometric functions are available: asin(), acos(), and atan()
The hyperbolic functions are sinh(), cosh(), and tanh().