Math Functions

C++ has many functions that allows you to perform mathematical tasks on numbers.

Header <cmath> declares a set of functions to compute common mathematical operations and transformations:

// Include the cmath library
#include <cmath>
std::cout << sqrt(64); // Return the square root of 64
std::cout << log(2); // Return the log of 2
std::cout << round(2.6); // Round 2.6

A list of other popular math functions from the <cmath> library can be found in the table below:

Math Functions

abs()
Returns the absolute value of the argument.
acos()
Returns the inverse cosine of the argument in radians.
asin()
Returns the inverse sine of the argument in radians.
atan()
Returns the inverse tangent of the argument in radians.
cbrt()
Returns the cube root of the argument.
ceil()
Returns the next whole number that is greater than or equal to the argument.
cos()
Returns the cosine of an argument given in radians.
cosh()
Returns the hyperbolic cosine of an argument given in radians.
exp()
Returns e raised to the power of the argument.
expm1()
Returns e raised to the power of the argument minus 1.
fabs()
Returns the absolute value of the floating point argument.
fdim()
Returns the positive difference between two arguments.
floor()
Returns the first whole number that is less than or equal to the argument.
Edit this page on GitHub

Contributors

Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn C++ on Codecademy

Edit this page on GitHub

Contributors