5 ms·
In languages I use there’s usually no need to write that code. C++/17 has std::clamp() in <algorithm> header. Modern C# has Math.Clamp() since .NET Core 2.0;
by Const-me 6y ago
In languages I use there’s usually no need to write that code.
C++/17 has std::clamp() in <algorithm> header.
Modern C# has Math.Clamp() since .NET Core 2.0; too bad it’s not available in desktop edition of the runtime.
HLSL has clamp() intrinsic function, and a special version saturate() to clamp into [ 0 .. +1 ] interval.
- the-dude 6y agoGreat. But I have been programming since about 1985.
- Const-me 6y agoI’ve been programming for living since 2000, but I don’t think that’s relevant. No reason not to use what’s available in standard libraries of whatever language you’re writing. For example, C++ on AMD64 is very likely to compile std::clamp<double> into 2 instructions, minsd and maxsd. I’m not so sure about nested ternaries mentioned elsewhere in the comments.
- naikrovek 6y agoThere are very good reasons to avoid std:: stuff. And if you don't already know that in your soul, I will appear to be a genuine crackpot, and the reasons not to use std::* will still exist.
- kcb 6y agoAre you saying a C++ developer shouldn't use the standard library?
- Tuna-Fish 6y agoNot the person you are replying to, but "a lot of the standard library is horrifying and should never be touched" is pretty standard advice for C++. Mind, I don't think this particular function belongs to that set.
- klyrs 6y agoMany performance-critical C++ programmers treat std with suspicion. One thing to keep in mind is that the interface is standard, but the implementations are not, and can foil you on cross-platform development. Another is that you might not need everything that a std container provides, and you can get away with a streamlined data structure that doesn't support those unnecessary operations. But as a sibling commenter notes... this isn't relevant for min/max.
- kcb 6y agoI'd much rather work on an application that utilized the standard lib than one that brought in dependencies and custom data structures. If it really is a bottleneck on a hot path then go for it. But not using it because of some ancient anecdotes is going to lead to an unmaintainable mess.
- Const-me 6y agoI think you’re overgeneralizing here. I’m aware some parts of C++ standard library are outright horrible, like iostream and I/O in general. Other parts are questionable, like date & time, locales, and futures. Meanwhile, other parts of the same standard library are actually OK (most collections, threading, synchronization, atomics, smart pointers, initializer lists). And other parts are awesome, like most of the stuff from <algorithm> header. Apparently, one of the C++ design goals was to not pay for features which aren’t used. Selectively ignoring stuff from the standard library doesn’t have much downsides.
- nickysielicki 6y agohttps://godbolt.org/z/bq5E8h https://godbolt.org/z/bq5E8h It's actually the std::min/std::max version that goes to minsd/maxsd in both clang/gcc.
- HugoDaniel 6y agoGreat. But I have been programming since about Math.min(Math.max(n, 1900), 1985 - 1)
- zhte415 6y agoYour database on https://mecoffee.nl https://mecoffee.nl seems to be down > Fout bij het maken van de databaseconnectie
- f00zz 6y agoIt gets a bit confusing when the order of arguments is different depending on the library. For instance, with std it's std::clamp(val, min, max), but with Qt it's qBound(min, val, max) (for some reason I think the order of arguments in qBound is more logical).
- nemetroid 6y agoIn Haskell, functions often take their arguments in the order that makes the most sense to partially apply. In this case, that would probably be clamp(min, max, val): supplying the first two arguments results in a reusable clamping function.
- ric2b 6y agoIt would be nice if partial application worked with keyword arguments. Actually it would be great if Haskell natively supported keyword arguments.
- Someone 6y agoThe expression Math.min(Math.max(num, min), max) is symmetric in min and num, so it doesn’t matter whether you interchange min and num (or, for that matter, max and num, but that is harder to see from that way to define the ‘clamp’ function)
- f00zz 6y agoGood point, thanks. (Though I've just checked gcc's definition of std::clamp and it looks like __glibcxx_assert(!(__hi < __lo)); return (__val < __lo) ? __lo : (__hi < __val) ? __hi : __val; so order of arguments does matter there.)
- Someone 6y agoSelf reply: Oops. That ‘for that matter’ part isn’t true, as it would mean you could permute the arguments any way you wished.
- wruza 6y ago
- Semaphor 6y ago> Modern C# has Math.Clamp() since .NET Core 2.0; too bad it’s not available in desktop edition of the runtime. Huh, that’s good to know. It’s also in .net standard 2.1. A shame it wasn’t added to Framework 4.8 (which I guess is what you mean by "desktop edition of the runtime"?)
- gameswithgo 6y ago> in desktop edition of the runtime huh?
- Const-me 6y agoMS .NET framework. Unfortunately, for the last couple years it lags behind .NET core. Even 2 years old .NET core 2.1 is better in some regards than the latest desktop version 4.8.
- OkGoDoIt 6y agoI had no idea .NET has Clamp() now. I’ve been writing something like the OP all this time ️
- brandmeyer 6y agoC++17 does have it... but it doesn't compile optimally. It compiles to something based on comparisons instead of the floating-point-specific operations. I tried this on a number of compiler combinations and didn't see anything that would emit min/max instructions for `std::clamp<double>`. https://www.godbolt.org/z/MKqTvE https://www.godbolt.org/z/MKqTvE
- ChrisLomont 6y agoYour two functions are not equivalent. std::fmax handles NaN, but std::clamp does not.
- brandmeyer 6y agoDepends on how the comparisons are ordered. Some of the orderings I've seen in here do respect NaN by virtue of `x > upper_bound` comparing false if either x or upper_bound are NaN.
- ChrisLomont 6y agoBoth functions are defined how they handle NaN by the standard. There is no ordering or implementation ambiguity by design.