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