C Math Functions
Math Functions
There is also a list of math functions available, that allows you to perform mathematical tasks on numbers.
To use them, you must include the math.h
header file in your program:
#include <math.h>
Square Root
To find the square root of a number, use the sqrt()
function:
Round a Number
The ceil()
function rounds a number of upwards to its nearest integer, and the floor()
function rounds a number of downwards to its nearest integer, and returns the result:
Power
The pow()
function returns the value of x to the power of y (xy):
Other Math Functions
A list of other popular math functions (from the <math.h>
library) can be found in the table below:
Function | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x |
asin(x) | Returns the arcsine of x |
atan(x) | Returns the arctangent of x |
atan2(y, x) | Returns the arctangent of y/x |
ceil(x) | Returns the smallest integer greater than or equal to x |
cbrt(x) | Returns the cube root of x |
cos(x) | Returns the cosine of x |
cosh(x) | Returns the hyperbolic cosine of x |
exp(x) | Returns the value of Ex (the exponential of the number x) |
fabs(x) | Returns the absolute value of x |
floor(x) | Returns the largest integer that is less than or equal to x |
fmod(x, y) | Returns the remainder of dividing x by y |
frexp(x, exp) | Decomposes the number x into whole and fractional parts |
hypot(x, y) | Returns the hypotenuse of a right triangle with legs x and y |
ldexp(x, exp) | Multiplies the number x by 2 to the power of exp |
log(x) | Returns the natural logarithm of x |
log10(x) | Returns the decimal logarithm of x |
modf(x, ip) | Factors a number x into its integer and fractional parts and returns the fractional part |
pow(x, y) | Returns x to the power of y |
sin(x) | Returns the sine of x (x in radians) |
sinh(x) | Returns the hyperbolic sine of x |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of x |
tanh(x) | Returns the hyperbolic tangent of x |
trunc(x) | Returns the integer part of x |
All of these functions accept arguments of type double unless otherwise specified.