Hacker News new | ask | show | jobs
by lebubule 882 days ago
Yes, I arrived at the same conclusion.

The various code snippets in the article don't compute the same "function". The order between the min() and max() matters even when done "by hand". This is apparent when min is greater than max as the results differ in the choice of the boundaries.

Funny that for such simple functions the discussion can become quickly so difficult/interesting.

Some toying around with the various implementations in C [1]:

[1]: https://godbolt.org/z/d4Tcdojx3

1 comments

Yes, you are correct, the faster clamp is incorrect because it does not return v when v is equal to lo and hi.