Action to clamp a value between min and max bounds

I’ve just had an idea for a new action that would force a value to be between two min and max bounds. For example, Data Manipulation > Clamp which would take 3 parameters:

  • a location containing the value to clamp
  • a min bound (inclusive)
  • a max bound (inclusive)

Full text displayed would give something like Clamp location between min and max. For example, clamping between 5 and 10 would:

  • store 5 in the location if it contains anything less than 5
  • do nothing if the location contains a value >= 5 and <= 10
  • store 10 in the location if it contains anything greater than 10

This would be doable on anything comparable such as Integer, Number, Interval and Datetime types.

For situations where you don’t want to hinder the operator with button clicks and error messages, I think this would save a couple ifs and would allow to build apps faster. What do you think?

Here is how to make a clamp function with the current expressions.
This example will work with Numbers and clamps between 5 and 10:

max(5, min(10, @Variable.num1 ))

add a floor() to work with integers:

floor(max(5, min(10, @Variable.int1 )))

I don’t find this to complicated.

For datetime variable I have no better approach as the if() combination for now, what in fact is a bit harder to read:

if(@Variable.date_to_check  > @Variable.dateMax , @Variable.dateMax , if(@Variable.date_to_check  < @Variable.dateMin , @Variable.dateMin , @Variable.date_to_check ))
1 Like

Thanks for these two workarounds! I didn’t think of using MIN() and MAX() this way and I’ll keep that one mind. The main issue I have with these two solutions is readability (especially for people who aren’t into expressions that much). But at least it works! :slight_smile:

1 Like