0

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
phuclv
  • 37,963
  • 15
  • 156
  • 475
Adam Lee
  • 24,710
  • 51
  • 156
  • 236

2 Answers2

6

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:

phuclv
  • 37,963
  • 15
  • 156
  • 475
1

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.

Peter Clark
  • 2,863
  • 3
  • 23
  • 37