▲ | aero_code 14 hours ago | |||||||
FYI, some major programming languages have clamp: C++: https://en.cppreference.com/w/cpp/algorithm/clamp.html .NET/C#: https://learn.microsoft.com/en-us/dotnet/api/system.math.cla... Java: 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-cla... Rust: https://doc.rust-lang.org/stable/std/primitive.f64.html#meth... | ||||||||
▲ | webstrand 13 hours ago | parent [-] | |||||||
Even 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. | ||||||||
|