Talk:cpp/numeric/midpoint
From cppreference.com
I think "ref std c++20" is missing. How it can be added? Ruslo (talk) 14:16, 1 October 2019 (PDT)
Wouldn't it be clearer to say x[std::midpoint(i,j)] rather than x[i+(j-i)/2]? 82.42.34.14 00:56, 30 April 2020 (PDT)
- the proposal actually says pointer midpoint cannot be implemented in portable C++, while integer midpoint can be (and a sample impl is provided), so there must be a difference. --Cubbi (talk) 05:44, 30 April 2020 (PDT)
[edit] Guarantees & corner cases?
What are the guarantees and corner cases? Is it guaranteed to return a value in the given range (with the exception of (-inf, inf) -> nan)? Other than inf, is it guaranteed that std::midpoint(-x, x) == 0.0
? Here are the ones I tried:
#include <format> #include <cmath> #define DO_ONE_LINE(a, b) std::cout << std::format("midpoint({}, {}) = {} vs {} ({}, {})\n", a, b, std::midpoint(a, b), a + 0.5 * (b - a), #a, #b) int main() { static constexpr auto max = std::numeric_limits<double>::max(); static constexpr auto inf = std::numeric_limits<double>::infinity(); static constexpr auto denormMin = std::numeric_limits<double>::denorm_min(); static constexpr auto lowest = std::numeric_limits<double>::lowest(); DO_ONE_LINE(0.0, inf); DO_ONE_LINE(-inf, inf); DO_ONE_LINE(-max, max); DO_ONE_LINE(-denormMin, lowest); DO_ONE_LINE(inf, -inf); DO_ONE_LINE(denormMin, lowest); DO_ONE_LINE(lowest, denormMin); DO_ONE_LINE(std::nextafter(denormMin, 1.0), lowest); DO_ONE_LINE(lowest, denormMin); DO_ONE_LINE(lowest, std::nextafter(denormMin, 1.0)); DO_ONE_LINE(std::nextafter(denormMin, 1.0), denormMin); DO_ONE_LINE(denormMin, std::nextafter(denormMin, 1.0)); DO_ONE_LINE(0.0, denormMin); DO_ONE_LINE(denormMin, 0.0); DO_ONE_LINE(denormMin, denormMin);
which produces
midpoint(0, inf) = inf vs inf (0.0, inf) midpoint(-inf, inf) = -nan vs -nan (-inf, inf) midpoint(-1.7976931348623157e+308, 1.7976931348623157e+308) = 0 vs inf (-max, max) midpoint(-5e-324, -1.7976931348623157e+308) = -8.988465674311579e+307 vs -8.988465674311579e+307 (-denormMin, lowest) midpoint(inf, -inf) = -nan vs -nan (inf, -inf) midpoint(5e-324, -1.7976931348623157e+308) = -8.988465674311579e+307 vs -8.988465674311579e+307 (denormMin, lowest) midpoint(-1.7976931348623157e+308, 5e-324) = -8.988465674311579e+307 vs -8.988465674311579e+307 (lowest, denormMin) midpoint(1e-323, -1.7976931348623157e+308) = -8.988465674311579e+307 vs -8.988465674311579e+307 (std::nextafter(denormMin, 1.0), lowest) midpoint(-1.7976931348623157e+308, 5e-324) = -8.988465674311579e+307 vs -8.988465674311579e+307 (lowest, denormMin) midpoint(-1.7976931348623157e+308, 1e-323) = -8.988465674311579e+307 vs -8.988465674311579e+307 (lowest, std::nextafter(denormMin, 1.0)) midpoint(1e-323, 5e-324) = 1e-323 vs 1e-323 (std::nextafter(denormMin, 1.0), denormMin) midpoint(5e-324, 1e-323) = 1e-323 vs 5e-324 (denormMin, std::nextafter(denormMin, 1.0)) midpoint(0, 5e-324) = 0 vs 0 (0.0, denormMin) midpoint(5e-324, 0) = 0 vs 5e-324 (denormMin, 0.0) midpoint(5e-324, 5e-324) = 5e-324 vs 5e-324 (denormMin, denormMin)
BenFrantzDale (talk) 06:28, 26 April 2024 (PDT)