1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // UNSUPPORTED: c++03, c++11, c++14, c++17 10 11 // <numeric> 12 13 // template <class _Float> 14 // _Tp midpoint(_Float __a, _Float __b) noexcept 15 // 16 17 #include <numeric> 18 #include <cassert> 19 20 #include "test_macros.h" 21 #include "fp_compare.h" 22 23 // Totally arbitrary picks for precision 24 template <typename T> 25 constexpr T fp_error_pct(); 26 27 template <> 28 constexpr float fp_error_pct<float>() { return 1.0e-4f; } 29 30 template <> 31 constexpr double fp_error_pct<double>() { return 1.0e-12; } 32 33 template <> 34 constexpr long double fp_error_pct<long double>() { return 1.0e-13l; } 35 36 37 template <typename T> 38 void fp_test() 39 { 40 ASSERT_SAME_TYPE(T, decltype(std::midpoint(T(), T()))); 41 ASSERT_NOEXCEPT( std::midpoint(T(), T())); 42 43 constexpr T maxV = std::numeric_limits<T>::max(); 44 constexpr T minV = std::numeric_limits<T>::min(); 45 46 // Things that can be compared exactly 47 static_assert((std::midpoint(T(0), T(0)) == T(0)), ""); 48 static_assert((std::midpoint(T(2), T(4)) == T(3)), ""); 49 static_assert((std::midpoint(T(4), T(2)) == T(3)), ""); 50 static_assert((std::midpoint(T(3), T(4)) == T(3.5)), ""); 51 static_assert((std::midpoint(T(0), T(0.4)) == T(0.2)), ""); 52 53 // Things that can't be compared exactly 54 constexpr T pct = fp_error_pct<T>(); 55 assert((fptest_close_pct(std::midpoint(T( 1.3), T(11.4)), T( 6.35), pct))); 56 assert((fptest_close_pct(std::midpoint(T(11.33), T(31.45)), T(21.39), pct))); 57 assert((fptest_close_pct(std::midpoint(T(-1.3), T(11.4)), T( 5.05), pct))); 58 assert((fptest_close_pct(std::midpoint(T(11.4), T(-1.3)), T( 5.05), pct))); 59 assert((fptest_close_pct(std::midpoint(T(0.1), T(0.4)), T(0.25), pct))); 60 61 assert((fptest_close_pct(std::midpoint(T(11.2345), T(14.5432)), T(12.88885), pct))); 62 63 // From e to pi 64 assert((fptest_close_pct(std::midpoint(T(2.71828182845904523536028747135266249775724709369995), 65 T(3.14159265358979323846264338327950288419716939937510)), 66 T(2.92993724102441923691146542731608269097720824653752), pct))); 67 68 assert((fptest_close_pct(std::midpoint(maxV, T(0)), maxV/2, pct))); 69 assert((fptest_close_pct(std::midpoint(T(0), maxV), maxV/2, pct))); 70 assert((fptest_close_pct(std::midpoint(minV, T(0)), minV/2, pct))); 71 assert((fptest_close_pct(std::midpoint(T(0), minV), minV/2, pct))); 72 assert((fptest_close_pct(std::midpoint(maxV, maxV), maxV, pct))); 73 assert((fptest_close_pct(std::midpoint(minV, minV), minV, pct))); 74 assert((fptest_close_pct(std::midpoint(maxV, minV), maxV/2, pct))); 75 assert((fptest_close_pct(std::midpoint(minV, maxV), maxV/2, pct))); 76 77 // Near the min and the max 78 assert((fptest_close_pct(std::midpoint(maxV*T(0.75), maxV*T(0.50)), maxV*T(0.625), pct))); 79 assert((fptest_close_pct(std::midpoint(maxV*T(0.50), maxV*T(0.75)), maxV*T(0.625), pct))); 80 assert((fptest_close_pct(std::midpoint(minV*T(2), minV*T(8)), minV*T(5), pct))); 81 82 // Big numbers of different signs 83 assert((fptest_close_pct(std::midpoint(maxV*T( 0.75), maxV*T(-0.5)), maxV*T( 0.125), pct))); 84 assert((fptest_close_pct(std::midpoint(maxV*T(-0.75), maxV*T( 0.5)), maxV*T(-0.125), pct))); 85 86 // Denormalized values 87 // TODO 88 89 // Check two values "close to each other" 90 T d1 = T(3.14); 91 T d0 = std::nextafter(d1, T(2)); 92 T d2 = std::nextafter(d1, T(5)); 93 assert(d0 < d1); // sanity checking 94 assert(d1 < d2); // sanity checking 95 96 #if defined(__PPC__) && (defined(__LONG_DOUBLE_128__) && __LONG_DOUBLE_128__) && \ 97 !(defined(__LONG_DOUBLE_IEEE128__) && __LONG_DOUBLE_IEEE128__) 98 // For 128 bit long double implemented as 2 doubles on PowerPC, 99 // nextafterl() of libm gives imprecise results which fails the 100 // midpoint() tests below. So skip the test for this case. 101 if constexpr (sizeof(T) != 16) 102 #endif 103 { 104 // Since there's nothing in between, the midpoint has to be one or the other 105 T res; 106 res = std::midpoint(d0, d1); 107 assert(res == d0 || res == d1); 108 assert(d0 <= res); 109 assert(res <= d1); 110 res = std::midpoint(d1, d0); 111 assert(res == d0 || res == d1); 112 assert(d0 <= res); 113 assert(res <= d1); 114 115 res = std::midpoint(d1, d2); 116 assert(res == d1 || res == d2); 117 assert(d1 <= res); 118 assert(res <= d2); 119 res = std::midpoint(d2, d1); 120 assert(res == d1 || res == d2); 121 assert(d1 <= res); 122 assert(res <= d2); 123 } 124 } 125 126 127 int main (int, char**) 128 { 129 fp_test<float>(); 130 fp_test<double>(); 131 fp_test<long double>(); 132 133 return 0; 134 } 135