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