The OpenD Programming Language

clamp

Clamps val into the given bounds. Result has the same type as val.

T1
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: T1

lower if val is less than lower, upper if val is greater than upper, and val in all other cases. Comparisons are made correctly (using std.functional.lessThan and the return value is converted to the return type using the standard integer coversion rules std.functional.greaterThan) even if the signedness of T1, T2, and T3 are different.

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);

auto x = clamp(42, uint.max, uint.max);
static assert(is(typeof(x) == int));
assert(x == -1);

Meta