Learn about mathematical functions in C++ for calculations like exponentiation, logarithms, trigonometry, rounding, and more.
<cmath>
library and allow us to perform operations like exponentiation, logarithms, trigonometry, rounding, and more.
<cmath>
Library<cmath>
library. To use them, you need to include this header file at the beginning of your program:
pow(base, exponent)
: Raises a number to a power.sqrt(x)
: Computes the square root of a number.log(x)
: Returns the natural logarithm (base e).log10(x)
: Returns the base-10 logarithm.exp(x)
: Computes e^x (exponential function).sin()
, cos()
, and tan()
that work with radians.
ceil(x)
: Rounds up to the nearest integer.floor(x)
: Rounds down to the nearest integer.round(x)
: Rounds to the nearest integer.abs(x)
: Returns the absolute value.fmod(x, y)
: Returns the remainder of x/y.Error | Cause | Solution |
---|---|---|
Undefined function | <cmath> not included | Add #include <cmath> |
Wrong trigonometric result | Angle in degrees instead of radians | Convert degrees to radians using M_PI / 180.0 |
Incorrect logarithm | Using log(x) instead of log10(x) | Use log10(x) for base-10 logs |
pow()
, sqrt()
)log()
, log10()
)sin()
, cos()
, tan()
)ceil()
, floor()
, round()
)abs()
, fmod()
)