]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_rat_float_interconv.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_rat_float_interconv.cpp
1 // Copyright John Maddock 2013.
2
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
7
8 #ifdef _MSC_VER
9 # define _SCL_SECURE_NO_WARNINGS
10 #endif
11
12 #if defined(TEST1) || defined(TEST2) || defined(TEST3) || defined(TEST4)
13 #include <boost/multiprecision/cpp_bin_float.hpp>
14 #include <boost/multiprecision/cpp_int.hpp>
15 #else
16 #include <boost/multiprecision/mpfr.hpp>
17 #endif
18 #include <boost/math/special_functions/next.hpp>
19
20 #include <boost/random/mersenne_twister.hpp>
21 #include <boost/random/uniform_int.hpp>
22 #include <boost/chrono.hpp>
23 #include "test.hpp"
24 #include <boost/array.hpp>
25 #include <iostream>
26 #include <iomanip>
27
28 #ifdef BOOST_MSVC
29 #pragma warning(disable:4127)
30 #endif
31
32 template <class Clock>
33 struct stopwatch
34 {
35 typedef typename Clock::duration duration;
36 stopwatch()
37 {
38 m_start = Clock::now();
39 }
40 duration elapsed()
41 {
42 return Clock::now() - m_start;
43 }
44 void reset()
45 {
46 m_start = Clock::now();
47 }
48
49 private:
50 typename Clock::time_point m_start;
51 };
52
53 template <class T>
54 struct exponent_type
55 {
56 typedef int type;
57 };
58 template <class T, boost::multiprecision::expression_template_option ET>
59 struct exponent_type<boost::multiprecision::number<T, ET> >
60 {
61 typedef typename T::exponent_type type;
62 };
63
64 template <class T>
65 T generate_random_float()
66 {
67 BOOST_MATH_STD_USING
68 typedef typename exponent_type<T>::type e_type;
69 static boost::random::mt19937 gen;
70 T val = gen();
71 T prev_val = -1;
72 while(val != prev_val)
73 {
74 val *= (gen.max)();
75 prev_val = val;
76 val += gen();
77 }
78 e_type e;
79 val = frexp(val, &e);
80
81 static const int max_exponent_value = (std::min)(static_cast<int>(std::numeric_limits<T>::max_exponent - std::numeric_limits<T>::digits - 20), 2000);
82 static boost::random::uniform_int_distribution<e_type> ui(0, max_exponent_value);
83 return ldexp(val, ui(gen));
84 }
85
86 template <class Float, class Rat>
87 void do_round_trip(const Float& val)
88 {
89 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
90 BOOST_MATH_STD_USING
91 Rat rat(val);
92 Float new_f(rat);
93 BOOST_CHECK_EQUAL(val, new_f);
94 //
95 // Try adding or subtracting an insignificant amount
96 // (0.25ulp) from rat and check that it rounds to the same value:
97 //
98 typename exponent_type<Float>::type e;
99 Float t = frexp(val, &e);
100 (void)t; // warning suppression
101 e -= std::numeric_limits<Float>::digits + 2;
102 BOOST_ASSERT(val == (val + ldexp(Float(1), e)));
103 Rat delta, rounded;
104 typedef typename boost::multiprecision::component_type<Rat>::type i_type;
105 i_type i(1);
106 i <<= (e < 0 ? -e : e);
107 if(e > 0)
108 delta.assign(i);
109 else
110 delta = Rat(i_type(1), i);
111 rounded = rat + delta;
112 new_f = static_cast<Float>(rounded);
113 BOOST_CHECK_EQUAL(val, new_f);
114 rounded = rat - delta;
115 new_f = static_cast<Float>(rounded);
116 BOOST_CHECK_EQUAL(val, new_f);
117
118 delta /= 2;
119 rounded = rat + delta;
120 new_f = static_cast<Float>(rounded);
121 BOOST_CHECK_EQUAL(val, new_f);
122 rounded = rat - delta;
123 new_f = static_cast<Float>(rounded);
124 BOOST_CHECK_EQUAL(val, new_f);
125
126 delta /= 2;
127 rounded = rat + delta;
128 new_f = static_cast<Float>(rounded);
129 BOOST_CHECK_EQUAL(val, new_f);
130 rounded = rat - delta;
131 new_f = static_cast<Float>(rounded);
132 BOOST_CHECK_EQUAL(val, new_f);
133 #endif
134 }
135
136 template <class Float, class Rat>
137 void test_round_trip()
138 {
139 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
140 std::cout << "Testing types " << typeid(Float).name() << " <<==>> " << typeid(Rat).name() << std::endl;
141 std::cout << "digits = " << std::numeric_limits<Float>::digits << std::endl;
142 std::cout << "digits10 = " << std::numeric_limits<Float>::digits10 << std::endl;
143 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
144 std::cout << "max_digits10 = " << std::numeric_limits<Float>::max_digits10 << std::endl;
145 #endif
146
147 stopwatch<boost::chrono::high_resolution_clock> w;
148
149 int count = 0;
150
151 while(boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
152 {
153 Float val = generate_random_float<Float>();
154 do_round_trip<Float, Rat>(val);
155 do_round_trip<Float, Rat>(Float(-val));
156 do_round_trip<Float, Rat>(Float(1/val));
157 do_round_trip<Float, Rat>(Float(-1/val));
158 count += 4;
159 if(boost::detail::test_errors() > 100)
160 break;
161 }
162
163 std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
164 std::cout << "Total values tested: " << count << std::endl;
165 #endif
166 }
167
168 template <class Int>
169 Int generate_random_int()
170 {
171 static boost::random::mt19937 gen;
172 static boost::random::uniform_int_distribution<boost::random::mt19937::result_type> d(1, 20);
173
174 int lim;
175 Int cppi(0);
176
177 lim = d(gen);
178
179 for(int i = 0; i < lim; ++i)
180 {
181 cppi *= (gen.max)();
182 cppi += gen();
183 }
184 return cppi;
185 }
186
187 template <class Float, class Rat>
188 void test_random_rationals()
189 {
190 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
191 std::cout << "Testing types " << typeid(Float).name() << " <<==>> " << typeid(Rat).name() << std::endl;
192 std::cout << "digits = " << std::numeric_limits<Float>::digits << std::endl;
193 std::cout << "digits10 = " << std::numeric_limits<Float>::digits10 << std::endl;
194 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
195 std::cout << "max_digits10 = " << std::numeric_limits<Float>::max_digits10 << std::endl;
196 #endif
197
198 typedef typename boost::multiprecision::component_type<Rat>::type i_type;
199 stopwatch<boost::chrono::high_resolution_clock> w;
200
201 int count = 0;
202
203 while(boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
204 {
205 Rat rat(generate_random_int<i_type>(), generate_random_int<i_type>());
206 Float f(rat);
207 Rat new_rat(f); // rounded value
208 int c = new_rat.compare(rat);
209 if(c < 0)
210 {
211 // If f was rounded down, next float up must be above the original value:
212 f = boost::math::float_next(f);
213 new_rat.assign(f);
214 BOOST_CHECK(new_rat >= rat);
215 }
216 else if(c > 0)
217 {
218 // If f was rounded up, next float down must be below the original value:
219 f = boost::math::float_prior(f);
220 new_rat.assign(f);
221 BOOST_CHECK(new_rat <= rat);
222 }
223 else
224 {
225 // Values were equal... nothing to test.
226 }
227 if(boost::detail::test_errors() > 100)
228 break;
229 }
230
231 std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
232 std::cout << "Total values tested: " << count << std::endl;
233 #endif
234 }
235
236 #if defined(TEST2)
237
238 void double_spot_tests()
239 {
240 boost::multiprecision::cpp_rational rat = 1;
241 boost::multiprecision::cpp_rational twiddle(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 54));
242 rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 50));
243
244 double d = rat.convert_to<double>();
245
246 rat += twiddle;
247 BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
248 rat += twiddle;
249 // tie: round to even rounds down
250 BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
251 rat += twiddle;
252 BOOST_CHECK_NE(d, rat.convert_to<double>());
253 rat -= twiddle;
254 BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
255 rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 52));
256 // tie, but last bit is now a 1 so we round up:
257 BOOST_CHECK_NE(d, rat.convert_to<double>());
258
259 }
260
261 #endif
262
263 int main()
264 {
265 using namespace boost::multiprecision;
266 #if defined(TEST1) && !defined(BOOST_MSVC)
267 test_round_trip<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
268 #elif defined(TEST2)
269 double_spot_tests();
270 test_round_trip<double, cpp_rational>();
271 #elif defined(TEST3) && !defined(BOOST_MSVC)
272 test_random_rationals<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
273 #elif defined(TEST4)
274 test_random_rationals<double, cpp_rational>();
275 #elif defined(TEST5)
276 // This does not work: gmp does not correctly round integer to float or
277 // rational to float conversions:
278 test_round_trip<double, mpq_rational>();
279 #elif defined(TEST6)
280 test_round_trip<mpfr_float_100, mpq_rational>();
281 #elif defined(TEST7)
282 test_random_rationals<mpfr_float_100, mpq_rational>();
283 #elif defined(TEST8)
284 test_random_rationals<double, mpq_rational>();
285 #endif
286 return boost::report_errors();
287 }
288