]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/26_numerics/midpoint/floating.cc
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 26_numerics / midpoint / floating.cc
1 // Copyright (C) 2019-2024 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++20 } }
19
20 #include <numeric>
21 #include <limits>
22 #include <cfloat>
23 #include <testsuite_hooks.h>
24
25 void
26 test01()
27 {
28 using lim = std::numeric_limits<double>;
29
30 VERIFY( std::midpoint(2.0, 4.0) == 3.0 );
31 VERIFY( std::midpoint(0.0, 0.4) == 0.2 );
32 VERIFY( std::midpoint(0.0, -0.0) == 0.0 );
33 VERIFY( std::midpoint(9e9, -9e9) == 0.0 );
34
35 VERIFY( std::midpoint(lim::max(), lim::max()) == lim::max() );
36 }
37
38 void
39 test02()
40 {
41 using lim = std::numeric_limits<float>;
42
43 VERIFY( std::midpoint(2.0f, 4.0f) == 3.0f );
44 VERIFY( std::midpoint(0.0f, 0.4f) == 0.2f );
45 VERIFY( std::midpoint(0.0f, -0.0f) == 0.0f );
46 VERIFY( std::midpoint(9e9f, -9e9f) == 0.0f );
47 }
48
49 void
50 test03()
51 {
52 using lim = std::numeric_limits<long double>;
53
54 VERIFY( std::midpoint(2.0l, 4.0l) == 3.0l );
55 VERIFY( std::midpoint(0.0l, 0.4l) == 0.2l );
56 VERIFY( std::midpoint(0.0l, -0.0l) == 0.0l );
57 VERIFY( std::midpoint(9e9l, -9e9l) == 0.0l );
58 }
59
60 namespace test04
61 {
62 // https://gcc.gnu.org/ml/libstdc++/2019-03/msg00065.html
63 constexpr double d = DBL_MIN + DBL_TRUE_MIN;
64 static_assert( std::midpoint(d, d) == d );
65
66 constexpr float f = FLT_MIN + FLT_TRUE_MIN;
67 static_assert( std::midpoint(f, f) == f );
68
69 constexpr long double l = LDBL_MIN + LDBL_TRUE_MIN;
70 static_assert( std::midpoint(l, l) == l );
71 }
72
73 int main()
74 {
75 test01();
76 test02();
77 test03();
78 }