3 ms·
C++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 tri
by brandmeyer 6y ago
C++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.