Skip to the content of the web site.

The math library

Now that you have learned about the double-precision floating-point data type double, you will likely want to perform operations with this type. You can already perform arithmetic operations with this type, but you may be interested in evaluating trigonometric, logarithmic or exponential functions. These are all implemented in the standard C math library

#include <cmath>

which you can read about at C++.com.

You will note that not all functions are implemented: if you want the secant, co-secant or cotangent functions, you must use the cosine, sine and tangent functions (std::cos, std::sin and std::tan), respectively.

You will see that the logarithmic function is called std::log. You may think this is base-10 logarithm, but this is actually the natural logarithm and if you want the base-10 logarithm, you must use std::log10.

To calculate an exponential, you must call the std::pow function.

The other function you may be interested in are the ceiling, floor, truncating, and rounding function, or std::ceil, std::floor, std::trunc and std::round:

  • The ceiling function returns a floating-point representation of the least integer greater than or equal to the argument.
  • The floor function returns a floating-point representation of the greatest integer less than or equal to the argument.
  • The truncation function zeros out the mantissa, so it rounds towards zero.
  • The round function returns a floating-point representation of the closest integer.

It is important to note that all four of these functions return a floating-point number, but if you assign the result to an integer that can store that floating-point value, that integer will get the desired value.