clip

xod/math/clip

Clips an input value so that the value is always inside a [MIN, MAX] range.
clip
@/clip
Clips an input value so that the value is always inside a [MIN, MAX] range.
Xnumber
The input value
MINnumber
A lower bound of the range.
MAXnumber
An upper bound of the range.
clip
OUT
X
MIN
MAX
OUTnumber
The result. Equals to `X` if it is already in the range, otherwise, its `Xc` rounded to a nearest bound: `MIN` or `MAX`.

C++ implementation

#pragma XOD dirtieness disable

node {
    void evaluate(Context ctx) {
        auto x = getValue<input_X>(ctx);
        auto rMin = getValue<input_MIN>(ctx);
        auto rMax = getValue<input_MAX>(ctx);
        auto xc =
            x > rMax ? rMax :
            x < rMin ? rMin : x;

        emitValue<output_OUT>(ctx, xc);
    }
}

Tabular tests

XMINMAXOUT
0000
1000
-100-10.510.5-10.5
100-10.510.510.5
5.74-10.510.55.74
Inf-10.510.510.5
-Inf-10.510.5-10.5
Inf-InfInfInf
-Inf-InfInf-Inf
NaN-10.510.5NaN
50NaNNaN50