Adam Lee
Adam Lee

Reputation: 25768

Any builtin clamp method in the C++ library?

I would like to know if there is some builtin clamp method which can clamp a value between a range, say between (0,1)?

clamp(a) = a if a is in (0,1)
a < 0 a = 0
a > 1 a = 1

Upvotes: 1

Views: 3284

Answers (2)

phuclv
phuclv

Reputation: 41952

C++17 introduced std::clamp(). Now you don't need to implement your own. Just use std::clamp(a, 0.0, 1.0)

If you don't have C++17 but boost is an option then use boost::algorithm::clamp(n, lower, upper);

Related:

Upvotes: 7

Peter Clark
Peter Clark

Reputation: 2943

C++ has no built in clamp function. You can either implement your own, or if you happen to be using boost it has a clamp function.

Upvotes: 1

Related Questions