Math-Related Headers
Here’s an introduction to the standard library headers related to mathematical operations: mainly <numeric>
, <numbers>
, and <cmath>
.
Let’s talk about <numbers>
, which provides many commonly used constants, so you don’t have to define them yourself. Nothing is more frustrating than seeing someone define constants like Pi in the code because you never know where they copied them from. In large projects, multiple definitions of such constants can become a disaster.
The standard library has these constants defined, so it’s safest to use those provided by the standard.
and
These two headers are available starting from C++17 and above.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <numeric>
#include <numbers>
int main()
{
constexpr auto lcm = std::lcm(10, 5);
std::cout << "lcm: " << lcm << std::endl;
constexpr auto gcd = std::gcd(20, 15);
std::cout << "gcd: " << gcd << std::endl;
std::cout << "Pi: " << std::numbers::pi << std::endl;
std::cout << "Euler's number (e): " << std::numbers::e << std::endl;
std::cout << "Log base 2 of e: " << std::numbers::log2e << std::endl;
std::cout << "Square root of 2: " << std::numbers::sqrt2 << std::endl;
return 0;
}
The <numbers>
header provides constants like:
The <cmath>
header provides a wide range of mathematical functions covering basic arithmetic, trigonometry, logarithms, and more. Below is a summary of these functions:
- Absolute Value and Sign Handling std::abs(x): Computes the absolute value of x (works for integers and floating-point types). std::fabs(x): Computes the absolute value of x, specifically for floating-point numbers. std::signbit(x): Checks if x is negative, returns a boolean. std::copysign(x, y): Returns a value with the magnitude of x and the sign of y.
- Power and Root Operations std::pow(x, y): Computes x raised to the power of y. std::sqrt(x): Computes the square root of x. std::cbrt(x): Computes the cube root of x. std::hypot(x, y): Computes the length of the hypotenuse of a right triangle, i.e., sqrt(xx + yy).
- Exponential and Logarithmic Operations std::exp(x): Computes e raised to the power of x. std::log(x): Computes the natural logarithm of x (base e). std::log10(x): Computes the common logarithm of x (base 10). std::log2(x): Computes the base-2 logarithm of x.
- Trigonometric Functions std::sin(x): Computes the sine of x (in radians). std::cos(x): Computes the cosine of x (in radians). std::tan(x): Computes the tangent of x (in radians). std::asin(x): Computes the arc sine of x (returns radians). std::acos(x): Computes the arc cosine of x (returns radians). std::atan(x): Computes the arc tangent of x (returns radians). std::atan2(y, x): Computes the angle of the point (x, y) from the origin.
- Rounding and Integer Operations std::ceil(x): Rounds x upwards (towards positive infinity). std::floor(x): Rounds x downwards (towards negative infinity). std::round(x): Rounds x to the nearest integer. std::trunc(x): Truncates the fractional part of x, keeping only the integer part.
- Other Useful Functions std::fmod(x, y): Computes the remainder of x divided by y. std::modf(x, &intpart): Splits x into its integer and fractional parts, returning the fractional part. std::frexp(x, &exp): Splits x into its mantissa and exponent, returning the mantissa and storing the exponent in exp. std::ldexp(x, exp): Computes x * 2^exp.
This is just a list of the most commonly used functions. For more detailed information, you can check cppreference.