Store a floating point number in a range

When a public function accepts a float value and that value needs to be stored in a specific range, I usually do:

void setParameter(float p)
{
  if (p < 0) p = 0;
  if (p > 1.f) p = 1.f;
  // ...do something with 'p'
}

      

Is there a better, faster and more elegant way to keep the float value in a range?

+1


source to share





All Articles