2

What I am looking for is a math function that constrains a primitive variable between a minimum and a maximum value in only one single function call, if it exists, in the standard math libraries for Objective-C.

I currently use:

float constrainedValue = fminf( maxValue, fmaxf( minValue, inValue ) );

Since I know that both fminf and fmaxf could potentially have instruction jumps or branches, it seems likely there could be a simple routine that could conjoin both of these operations into one, optimized function.

  • There is `boost::algorithm::clamp(n, lower, upper);` – Jarod42 Mar 29 '16 at 20:35
  • 3
    Related (solution using a macro): [How can I write a 'clamp' / 'clip' / 'bound' macro for returning a value in a given range?](http://stackoverflow.com/a/14770282/4573247) – dfrib Mar 29 '16 at 20:39

1 Answers1

1

This topic is thoroughly discussed here: Fastest way to clamp a real (fixed/floating point) value?

'clamp' is the keyword I was looking for.

Community
  • 1
  • 1