Time and Numeric Utilities
Clamp Value
std::clamp keeps a value inside a minimum and maximum range.
Clamp Value
clamp_value.cpp
#include <algorithm>
#include <iostream>
int main() {
int score = ;
int bounded = std::clamp(score, 0, 100);
bool changed = bounded != score;
std::cout << "score=" << score << std::endl;
std::cout << "bounded=" << bounded << std::endl;
std::cout << "changed=" << changed << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
int main() {
int score = ;
int bounded = std::clamp(score, 0, 100);
bool changed = bounded != score;
std::cout << "score=" << score << std::endl;
std::cout << "bounded=" << bounded << std::endl;
std::cout << "changed=" << changed << std::endl;
return 0;
}
#include <algorithm>
#include <iostream>
int main() {
int score = ;
int bounded = std::clamp(score, 0, 100);
bool changed = bounded != score;
std::cout << "score=" << score << std::endl;
std::cout << "bounded=" << bounded << std::endl;
std::cout << "changed=" << changed << std::endl;
return 0;
}
clamp
`std::clamp(value, low, high)` returns `low`, `high`, or the original value depending on the range.
bounds
Bounds make invalid or extreme values fit a rule before later calculations use them.