I have a code with pixel brightness that is limited to [0..255].
And it’s evaluated for some reason, so I can get value outside the bounds.
I can do
(if x>maxValue) x = maxValue;
or
x = min(x, MaxValue);
or
(x > MaxValue) ? MaxValue : x;
but I wonder what is the nice way? How to limit a value with less comparisons and with good code style?
One comparison is least, any lesser means there is no comparison. Look at Compute minimum without branching, if you want this.
For good style, this is best –