4 ms·
Maybe it's different since you're not native English but clamp is as intuitive for me as something like ceil or floor methods. It might not be as simple for som
by xmprt 1y ago
Maybe it's different since you're not native English but clamp is as intuitive for me as something like ceil or floor methods. It might not be as simple for someone who is ESL, probably because no major programming languages that I know of include clamp in their standard library like they do with min, max, ceil, floor.
I'm also coming from being a Go programmer where the native library is very barebones so a lot of custom utility methods like this are inevitable.
- aero_code 1y agoFYI, some major programming languages have clamp: C++: https://en.cppreference.com/w/cpp/algorithm/clamp.html https://en.cppreference.com/w/cpp/algorithm/clamp.html .NET/C#: https://learn.microsoft.com/en-us/dotnet/api/system.math.clamp?view=net-9.0 https://learn.microsoft.com/en-us/dotnet/api/system.math.cla... Java: https://docs.oracle.com/en/java/javase/24/docs/api/java.base/java/lang/Math.html#clamp(double,double,double) https://docs.oracle.com/en/java/javase/24/docs/api/java.base... Ruby: https://ruby-doc.org/core-2.4.0/Comparable.html#method-i-clamp https://ruby-doc.org/core-2.4.0/Comparable.html#method-i-cla... Rust: https://doc.rust-lang.org/stable/std/primitive.f64.html#method.clamp https://doc.rust-lang.org/stable/std/primitive.f64.html#meth...
- webstrand 1y agoEven CSS has clamp. I have to periodically remind myself that Math.clamp is missing from the stdlib, it's much easier to read when you need to use a bunch. Math.min(lower, Math.max(value, upper)) is fine for a one-off, but when you need multiple instances of clamp in the same expression the extra parentheses become troublesome.
- zahlman 1y agoI do find it strange how Python's min and max are builtin and don't require an import, but 'clamp' isn't provided at all. When I look at https://stackoverflow.com/questions/4092528 https://stackoverflow.com/questions/4092528 I feel the lack of an "obvious way to do it", since the simple approach has variant spellings and there are always people trying to be clever about it.
- TRiG_Ireland 1y agoI am a native English speaker, and I've never heard of "clamp" before. Admittedly, most of my programming experience is in PHP, and I've rarely done anything with much maths.