C++ Reference ยท Memory and Utilities
The <cmath> header
Reference for the C++ cmath header: sqrt, pow, trig, exp, log, floor, ceil, round, fmod, hypot, lerp, isnan, isfinite and the C++17 special functions.
<cmath> declares the standard math functions for float, double and long double, in namespace std (and usually also in the global namespace). It covers powers and roots, exponentials and logarithms, trigonometry, rounding, remainders, classification of special values, and a set of special functions such as the error function and Bessel functions.
The overloads pick the precision from the argument: std::sqrt(2.0f) returns a float. Integer arguments are converted to double, so std::sqrt(2) works. The constants pi and e are not in this header; they live in <numbers> (C++20).
#include <cmath> double h = std::hypot(3.0, 4.0); // 5
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
double sqrt(double x); float sqrt(float x); long double sqrt(long double x); double pow(double base, double exponent); double fmod(double x, double y); bool isnan(double x); bool isfinite(double x); bool isinf(double x);
| Group | Functions |
|---|---|
| Powers | sqrt, cbrt, pow, hypot (two and, since C++17, three arguments) |
| Exponential | exp, exp2, expm1, log, log2, log10, log1p |
| Trigonometry | sin, cos, tan, asin, acos, atan, atan2 and the hyperbolic versions sinh, cosh, tanh, asinh, acosh, atanh (radians, not degrees) |
| Rounding | floor, ceil, trunc, round, lround, llround, rint, nearbyint |
| Remainder | fmod, remainder, remquo |
| Absolute value, sign | abs, fabs, copysign, signbit |
| Comparison | fmin, fmax, fdim, isless, isgreater and friends |
| Classification | isnan, isinf, isfinite, isnormal, fpclassify |
| Special (C++17) | erf, erfc, tgamma, lgamma, beta, comp_ellint_1, cyl_bessel_j, riemann_zeta and others |
| Other | lerp (C++20), fma, ldexp, frexp, modf, nextafter |
Errors: domain and range errors are reported through errno and the floating-point exception flags, depending on math_errhandling; by default with GCC the functions just return NaN or infinity and set errno (EDOM, ERANGE). They do not throw.
Usage notes
- Angles are radians. Convert with
degrees * std::numbers::pi / 180. - Use
std::abs, not C'sabs, for floating-point values: the plainabs(int)from<cstdlib>would truncate a double. std::powwith an integer exponent is a floating-point call. For exact integer powers write a loop (or1 << nfor powers of two); a double result is exact only while it fits in 53 bits.- Compare floats with a tolerance, and never with
==after arithmetic. NaN is never equal to anything, including itself. roundvsrint:roundalways moves ties away from zero;rintfollows the current rounding mode, which is to-nearest-even by default.-ffast-mathbreaks NaN and infinity handling. Avoid it when you callisnan.- constexpr: most
<cmath>functions are notconstexprbefore C++26, although GCC folds many of them at compile time as an extension. - Prefer
<cmath>to<math.h>. It gives you the overload set; the C header is only guaranteed for thedoubleversions.