clamp

Clamps a value into the given bounds.

This functions is equivalent to max(lower, min(upper,val)).

clamp
(
T1
T2
T3
)
(
T1 val
,,)

Parameters

val T1

The value to clamp.

lower T2

The _lower bound of the clamp.

upper T3

The _upper bound of the clamp.

Return Value

Type: auto

Returns val, if it is between lower and upper. Otherwise returns the nearest of the two.

Examples

assert(clamp(2, 1, 3) == 2);
assert(clamp(0, 1, 3) == 1);
assert(clamp(4, 1, 3) == 3);

assert(clamp(1, 1, 1) == 1);

assert(clamp(5, -1, 2u) == 2);

Meta

Suggestion Box / Bug Report