Math Functions

Published Jul 30, 2021Updated Oct 28, 2022
Contribute to Docs

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.
acosh()
Returns the arc hyperbolic cosine of the given argument.
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.
atan2()
Returns the inverse tangent of a coordinate pair in radians.
atanh()
Computes the inverse hyperbolic tangent of a number 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.
hypot()
Returns the value of the longest side of a right-angled triangle.
isfinite()
Returns a value telling whether the argument is a finite value.
isinf()
Returns a value indicating whether or not the argument is an infinite 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.
modf()
Splits a given number into an integral and a fractional part.
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.
remainder()
Returns the floating-point remainder of numerator/denominator (rounded to the nearest number).
remquo()
Returns the remainder of two integer values, and stores an integer value with the sign and approximate magnitude of the quotient in a parameter.
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.
tan()
Returns the tangent of an angle given in radians.
tanh()
Returns the hyperbolic tangent of an angle given in radians.
trunc()
Returns the integral part of the argument.

All contributors

Looking to contribute?

Learn C++ on Codecademy