Math Functions

C++ has many functions that allows mathematical tasks to be performed 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.
asinh()
Returns the inverse hyperbolic sine of an angle given 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 the base-e exponential function of the argument.
exp2()
Returns the base-2 exponential function, or 2 raised to the power of the given 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.
fmax()
Returns the larger of two arguments.
fmin()
Returns the smaller of two arguments.
fmod()
Returns the floating-point remainder of the division of two values, rounded towards zero.
isfinite()
Returns a value telling whether the argument is a finite value.
isnan()
Returns a number telling whether a given value is a NaN value.
log()
Returns the natural, base-e logarithm of the argument.
log10()
Returns the base-10 logarithm of the argument.
log2()
Returns the base-2 logarithm of the argument.
nan()
Returns a quiet NaN (Not-A-Number) value of type double.
nearbyint()
Returns the argument rounded to the closest integer as a floating-point value.
pow()
Returns the result of a base value raised to the power of an exponent.
rint()
Rounds the argument to an integral value using the current rounding direction.
round()
Returns the integer that is nearest to the argument, with halfway cases rounded away from the ending zero.
signbit()
Returns true if the floating point argument is negative and false if floating point argument is positive.
sin()
Returns the sine of an angle, given in radians, in the range [-1,1].
sinh()
Returns the hyperbolic sine of an argument given in radians.
sqrt()
Returns square root of the argument.
tanh()
Returns the hyperbolic tangent of an angle given in radians.
trunc()
Returns the integral part of the argument.

Contributors

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

Learn C++ on Codecademy