Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我想知道是否有一些内置的钳位方法可以钳位一个范围之间的值,比如(0,1)之间?
clamp(a) = a if a is in (0,1) a < 0 a = 0 a > 1 a = 1
C++17 引入std::clamp()。现在你不需要实现你自己的。只需使用std::clamp(a, 0.0, 1.0)
std::clamp()
std::clamp(a, 0.0, 1.0)
如果您没有 C++17 但可以选择 boost,则使用boost::algorithm::clamp(n, lower, upper);
boost::algorithm::clamp(n, lower, upper);
有关的:
C++ 没有内置的钳位函数。您可以实现自己的,或者如果您碰巧使用boost它有一个钳位功能。