zlacker

[parent] [thread] 15 comments
1. svanta+(OP)[view] [source] 2024-01-16 13:42:56
I'm a heavy std::clamp user, but I'm considering replacing it with min+max because of the uncertainty about what will happen when lo > hi. On windows it triggers an assertion, while other platforms just do a min+max in one or the other order. Of course, this should never happen but can be difficult to guarantee when the limits are derived from user inputs.
replies(4): >>lifthr+l >>wegfaw+O1 >>lpapez+Y6 >>dahart+FB
2. lifthr+l[view] [source] 2024-01-16 13:44:40
>>svanta+(OP)
Pretty sure that their behaviors on NaN arguments will also differ.
3. wegfaw+O1[view] [source] 2024-01-16 13:54:35
>>svanta+(OP)
I hope they fix it. Thats quite a basic functional unit for it to be a footgun all on its own.
replies(1): >>camblo+h5
◧◩
4. camblo+h5[view] [source] [discussion] 2024-01-16 14:20:22
>>wegfaw+O1
Don't get your hopes up, the behavior when lo > hi is explicitly undefined.
5. lpapez+Y6[view] [source] 2024-01-16 14:30:15
>>svanta+(OP)
> Of course, this should never happen but can be difficult to guarantee when the limits are derived from user inputs.

Sounds to me like you are missing a validation step before calling your logic. When it comes to parsing, trusting user input is a recipe for disaster in the form of buffer overruns and potential exploits.

As they used to say in the Soviet Union: "trust, but verify".

replies(2): >>PaulDa+r9 >>iknows+P9
◧◩
6. PaulDa+r9[view] [source] [discussion] 2024-01-16 14:43:12
>>lpapez+Y6
That was what Reagan said about the Soviet Union, not what was said in the Soviet Union.

Correct me if I'm wrong.

replies(3): >>useful+hd >>plucas+Md >>protom+ye
◧◩
7. iknows+P9[view] [source] [discussion] 2024-01-16 14:44:33
>>lpapez+Y6
The answer is of course

    clamp(min(a,b), max(a,b))
classic c++
◧◩◪
8. useful+hd[view] [source] [discussion] 2024-01-16 15:02:31
>>PaulDa+r9
According to wikipedia you're right (about Reagan) but it's also a Russion proverb.
◧◩◪
9. plucas+Md[view] [source] [discussion] 2024-01-16 15:05:29
>>PaulDa+r9
https://en.wikipedia.org/wiki/Trust,_but_verify

> Trust, but verify (Russian: доверяй, но проверяй, tr. doveryay, no proveryay, IPA: [dəvʲɪˈrʲæj no prəvʲɪˈrʲæj]) is a Russian proverb, which is rhyming in Russian. The phrase became internationally known in English after Suzanne Massie, a scholar of Russian history, taught it to Ronald Reagan, then president of the United States, the latter of whom used it on several occasions in the context of nuclear disarmament discussions with the Soviet Union.

Memorably referenced in "Chernobyl": https://youtu.be/9Ebah_QdBnI?t=79

replies(3): >>PaulDa+pB >>fl0ki+yk1 >>dekhn+nu1
◧◩◪
10. protom+ye[view] [source] [discussion] 2024-01-16 15:09:16
>>PaulDa+r9
Russian here. We use that expression from time to time: https://ya.ru/search/?text="доверяй%2C+но+проверяй"
◧◩◪◨
11. PaulDa+pB[view] [source] [discussion] 2024-01-16 16:52:24
>>plucas+Md
Thanks for the clarification/explanation!
12. dahart+FB[view] [source] 2024-01-16 16:54:12
>>svanta+(OP)
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+PD
◧◩
13. svanta+PD[view] [source] [discussion] 2024-01-16 17:02:51
>>dahart+FB
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+qK
◧◩◪
14. dahart+qK[view] [source] [discussion] 2024-01-16 17:31:52
>>svanta+PD
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.
◧◩◪◨
15. fl0ki+yk1[view] [source] [discussion] 2024-01-16 20:00:41
>>plucas+Md
Also referenced in the Metro Exodus "Sam's Story" DLC because of the backstory of the two characters speaking, and nuclear weapons once again being part of the scenario.
◧◩◪◨
16. dekhn+nu1[view] [source] [discussion] 2024-01-16 20:46:07
>>plucas+Md
When I hear the phrase, I rewrite it to "don't trust, verify."
[go to top]