]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_convert_from_mpfi_float.cpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_convert_from_mpfi_float.cpp
1 ///////////////////////////////////////////////////////////////
2 // Copyright 2012 John Maddock. Distributed under the Boost
3 // Software License, Version 1.0. (See accompanying file
4 // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
5
6 #ifdef _MSC_VER
7 #define _SCL_SECURE_NO_WARNINGS
8 #endif
9
10 #if defined(HAS_MPFI)
11
12 #include <boost/multiprecision/cpp_int.hpp>
13 #include <boost/random/mersenne_twister.hpp>
14 #include <boost/random/uniform_int.hpp>
15 #include "test.hpp"
16
17 #if defined(HAS_GMP)
18 #include <boost/multiprecision/gmp.hpp>
19 #endif
20 #if defined(HAS_MPFR)
21 #include <boost/multiprecision/mpfr.hpp>
22 #endif
23 #if defined(HAS_MPFI)
24 #include <boost/multiprecision/mpfi.hpp>
25 #endif
26 #ifdef HAS_TOMMATH
27 #include <boost/multiprecision/tommath.hpp>
28 #endif
29 #ifdef HAS_FLOAT128
30 #include <boost/multiprecision/float128.hpp>
31 #endif
32 #include <boost/multiprecision/cpp_bin_float.hpp>
33 #include <boost/multiprecision/cpp_dec_float.hpp>
34
35 using namespace boost::multiprecision;
36
37 #ifdef BOOST_MSVC
38 #pragma warning(disable : 4127)
39 #endif
40
41 template <class T>
42 T generate_random()
43 {
44 typedef int e_type;
45 static boost::random::mt19937 gen;
46 T val = gen();
47 T prev_val = -1;
48 int digits_so_far = 0;
49 while (digits_so_far < std::numeric_limits<T>::digits)
50 {
51 val *= (gen.max)();
52 prev_val = val;
53 val += gen();
54 digits_so_far += std::numeric_limits<boost::random::mt19937::result_type>::digits;
55 }
56 e_type e;
57 val = frexp(val, &e);
58
59 static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
60 return ldexp(val, ui(gen));
61 }
62
63 template <class From, class To>
64 void test_convert_neg_int(From from, const std::integral_constant<bool, true>&)
65 {
66 from = -from;
67 To t3(from);
68 To t4 = from.template convert_to<To>();
69 BOOST_CHECK_EQUAL(From(trunc(from)), From(t3));
70 BOOST_CHECK_EQUAL(From(trunc(from)), From(t4));
71 }
72 template <class From, class To>
73 void test_convert_neg_int(From const&, const std::integral_constant<bool, false>&)
74 {
75 }
76
77 template <class From, class To>
78 void test_convert_imp(std::integral_constant<int, number_kind_floating_point> const&, std::integral_constant<int, number_kind_integer> const&)
79 {
80 for (unsigned i = 0; i < 100; ++i)
81 {
82 From from = generate_random<From>();
83 To t1(from);
84 To t2 = from.template convert_to<To>();
85 BOOST_CHECK_EQUAL(From(trunc(from)), From(t1));
86 BOOST_CHECK_EQUAL(From(trunc(from)), From(t2));
87 test_convert_neg_int<From, To>(from, std::integral_constant<bool, std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
88 }
89 }
90
91 template <class From, class To>
92 void test_convert_neg_rat(From from, const std::integral_constant<bool, true>&)
93 {
94 from = -from;
95 To t3(from);
96 To t4 = from.template convert_to<To>();
97 BOOST_CHECK_EQUAL(From(t3), median(from));
98 BOOST_CHECK_EQUAL(From(t4), median(from));
99 }
100 template <class From, class To>
101 void test_convert_rat_int(From const&, const std::integral_constant<bool, false>&)
102 {
103 }
104
105 template <class From, class To>
106 void test_convert_imp(std::integral_constant<int, number_kind_floating_point> const&, std::integral_constant<int, number_kind_rational> const&)
107 {
108 for (unsigned i = 0; i < 100; ++i)
109 {
110 From from = generate_random<From>();
111 To t1(from);
112 To t2 = from.template convert_to<To>();
113 BOOST_CHECK_EQUAL(From(t1), median(from));
114 BOOST_CHECK_EQUAL(From(t2), median(from));
115 test_convert_neg_rat<From, To>(from, std::integral_constant<bool, std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
116 }
117 }
118
119 template <class From, class To>
120 void test_convert_neg_float(From from, const std::integral_constant<bool, true>&)
121 {
122 from = -from;
123 To t3(from);
124 To t4 = from.template convert_to<To>();
125 To answer(median(from).str());
126 To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
127 BOOST_CHECK_CLOSE_FRACTION(t3, answer, tol);
128 BOOST_CHECK_CLOSE_FRACTION(t4, answer, tol);
129 }
130 template <class From, class To>
131 void test_convert_neg_float(From const&, const std::integral_constant<bool, false>&)
132 {
133 }
134
135 template <class From, class To>
136 void test_convert_imp(std::integral_constant<int, number_kind_floating_point> const&, std::integral_constant<int, number_kind_floating_point> const&)
137 {
138 for (unsigned i = 0; i < 100; ++i)
139 {
140 From from = generate_random<From>();
141 To t1(from);
142 To t2 = from.template convert_to<To>();
143 To answer(median(from).str());
144 To tol = (std::max)(std::numeric_limits<To>::epsilon(), To(std::numeric_limits<From>::epsilon())) * 2;
145 BOOST_CHECK_CLOSE_FRACTION(t1, answer, tol);
146 BOOST_CHECK_CLOSE_FRACTION(t2, answer, tol);
147 test_convert_neg_float<From, To>(from, std::integral_constant<bool, std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
148 }
149 }
150
151 template <class From, class To>
152 void test_convert()
153 {
154 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
155 }
156
157 int main()
158 {
159 try
160 {
161 test_convert<mpfi_float_50, cpp_int>();
162 test_convert<mpfi_float_50, int128_t>();
163 test_convert<mpfi_float_50, uint128_t>();
164
165 test_convert<mpfi_float_50, cpp_rational>();
166
167 test_convert<mpfi_float_50, cpp_dec_float_50>();
168
169 #if defined(HAS_GMP)
170 test_convert<mpfi_float_50, mpz_int>();
171 test_convert<mpfi_float_50, mpq_rational>();
172 test_convert<mpfi_float_50, mpf_float_50>();
173 #endif
174 #if defined(HAS_MPFR)
175 test_convert<mpfi_float_50, mpfr_float_50>();
176 #endif
177 #ifdef HAS_TOMMATH
178 test_convert<mpfi_float_50, tom_int>();
179 test_convert<mpfi_float_50, tom_rational>();
180 #endif
181 #ifdef HAS_FLOAT128
182 test_convert<mpfi_float_50, float128>();
183 #endif
184 }
185 catch (...)
186 {
187 return 1;
188 }
189 return boost::report_errors();
190 }
191
192 #else
193
194 int main() { return 0; }
195
196 #endif