...

/

New Mathematical Functions

New Mathematical Functions

With C++17 we get lots of new mathematical functions like gcd, lcm, clamp and other special ones.

We'll cover the following...

gcd and lcm #

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

Press + to interact
#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:

Press + to interact
#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
...