3 ms·
That will give you the median value. What OP wants is the value `a` clamped within `min` and `max`.
by ccmcarey 6y ago
That will give you the median value. What OP wants is the value `a` clamped within `min` and `max`.
- donquichotte 6y agoCan you give an example where median([a, min, max]) is not equal to clamp(a, min, max), given max >= min?
- naikrovek 6y agoIf a < min or a > max, if I'm reading it right.
- mc10 6y agoYou can prove that this algorithm works for both of those cases. Assuming min <= max: If a < min, then the sorted array is [a, min, max]. The median is min, which is a clamped to min. If a > max, then the sorted array is [min, max, a]. The median is max, which is a clamped to max.