zlacker

[parent] [thread] 2 comments
1. dahart+(OP)[view] [source] 2024-01-16 16:54:12
Will min+max help you? What do you expect the answer to be when lo > hi? What certainty should std::clamp have? Using min+max on a number that’s between lo+hi when lo>hi will always return either lo or hi, and never your input value.
replies(1): >>svanta+a2
2. svanta+a2[view] [source] 2024-01-16 17:02:51
>>dahart+(OP)
Sure, that was the point - min(max()) forces you to give explicit preference to lo or hi, whereas with clamp it's up to the std library. I trust my users to bend my software to their will, but I don't want different behavior on mac and windows (for example).
replies(1): >>dahart+L8
◧◩
3. dahart+L8[view] [source] [discussion] 2024-01-16 17:31:52
>>svanta+a2
Yeah, seems reasonable. I think the outer call wins, so min(max()) will always return lo for empty intervals, right? I didn’t know std::clamp() was undefined for empty intervals. It does seem like a good idea to try to guarantee the interval is valid instead of worrying about clamp… even with a guarantee, the answer might still surprise someone, since technically the problem is mathematically undefined and the guaranteed answer is wrong.
[go to top]