-1

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
  • 32,499
  • 12
  • 130
  • 417
Adam Lee
  • 23,314
  • 47
  • 144
  • 221

2 Answers2

5

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
  • 32,499
  • 12
  • 130
  • 417
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,713
  • 3
  • 21
  • 37