Search⌘ K
AI Features

New Mathematical Functions

Learn about the new mathematical functions introduced in C++17's standard library such as gcd, lcm, clamp, and advanced special functions in cmath. Understand how these additions improve numerical operations and broaden C++17's capabilities in mathematical computations.

We'll cover the following...

gcd and lcm #

std::gcd and std::lcm, introduced in P0295R0, are declared in <numeric> header:​

C++ 17
#include <iostream>
#include <numeric> // for gcm, lcm
int main() {
std::cout << std::gcd(24, 60) << ',';
std::cout << std::lcm(15, 50) << '\n';
}

clamp(v, min, max) #

Another useful function is clamp(v, min, max), declared in <algorithm>, from P0025:

C++ 17
#include <iostream>
#include <algorithm> // clamp
int main() {
std::cout << std::clamp(300, 0, 255) << ',';
std::cout << std::clamp(-10, 0, 255) << '\n';
}

​And what’s more, here are newly available special functions, defined in the <cmath> header.

Function Description
...