]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/multiprecision/test/test_convert_from_cpp_rational.cpp
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / boost / libs / multiprecision / test / test_convert_from_cpp_rational.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 http://www.boost.org/LICENSE_1_
5
6 #ifdef _MSC_VER
7 # define _SCL_SECURE_NO_WARNINGS
8 #endif
9
10 #include <boost/multiprecision/cpp_int.hpp>
11 #include <boost/random/mersenne_twister.hpp>
12 #include "test.hpp"
13
14 #if defined(HAS_GMP)
15 #include <boost/multiprecision/gmp.hpp>
16 #endif
17 #if defined(HAS_MPFR)
18 #include <boost/multiprecision/mpfr.hpp>
19 #endif
20 #if defined(HAS_MPFI)
21 #include <boost/multiprecision/mpfi.hpp>
22 #endif
23 #ifdef HAS_TOMMATH
24 #include <boost/multiprecision/tommath.hpp>
25 #endif
26 #ifdef HAS_FLOAT128
27 #include <boost/multiprecision/float128.hpp>
28 #endif
29 #include <boost/multiprecision/cpp_bin_float.hpp>
30 #include <boost/multiprecision/cpp_dec_float.hpp>
31
32
33 using namespace boost::multiprecision;
34
35 #ifdef BOOST_MSVC
36 #pragma warning(disable:4127)
37 #endif
38
39
40 template <class T>
41 T generate_random_int(unsigned bits_wanted)
42 {
43 static boost::random::mt19937 gen;
44 typedef boost::random::mt19937::result_type random_type;
45
46 T max_val;
47 unsigned digits;
48 if(std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
49 {
50 max_val = (std::numeric_limits<T>::max)();
51 digits = std::numeric_limits<T>::digits;
52 }
53 else
54 {
55 max_val = T(1) << bits_wanted;
56 digits = bits_wanted;
57 }
58
59 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
60 while((random_type(1) << bits_per_r_val) > (gen.max)()) --bits_per_r_val;
61
62 unsigned terms_needed = digits / bits_per_r_val + 1;
63
64 T val = 0;
65 for(unsigned i = 0; i < terms_needed; ++i)
66 {
67 val *= (gen.max)();
68 val += gen();
69 }
70 val %= max_val;
71 return val;
72 }
73
74 template <class T>
75 T generate_random(unsigned bits_wanted)
76 {
77 typedef typename component_type<T>::type int_type;
78 T val(generate_random_int<int_type>(bits_wanted), generate_random_int<int_type>(bits_wanted));
79 return val;
80 }
81
82 template <class From, class To>
83 void test_convert_neg_val(From from, const boost::mpl::true_&)
84 {
85 from = -from;
86 typename component_type<From>::type answer = numerator(from) / denominator(from);
87 To t3(from);
88 To t4 = from.template convert_to<To>();
89 BOOST_CHECK_EQUAL(answer.str(), t3.str());
90 BOOST_CHECK_EQUAL(answer.str(), t4.str());
91 }
92 template <class From, class To>
93 void test_convert_neg_val(From const&, const boost::mpl::false_&)
94 {
95 }
96
97 template <class From, class To>
98 void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_integer> const&)
99 {
100 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
101
102 for(unsigned i = 0; i < 100; ++i)
103 {
104 From from = generate_random<From>(bits_wanted);
105 typename component_type<From>::type answer = numerator(from) / denominator(from);
106 To t1(from);
107 To t2 = from.template convert_to<To>();
108 BOOST_CHECK_EQUAL(answer.str(), t1.str());
109 BOOST_CHECK_EQUAL(answer.str(), t2.str());
110 test_convert_neg_val<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
111 }
112 }
113
114 template <class From, class To>
115 void test_convert_neg_float_val(From from, To const& tol, const boost::mpl::true_&)
116 {
117 from = -from;
118 To answer = To(numerator(from)) / To(denominator(from));
119 To t3(from);
120 To t4 = from.template convert_to<To>();
121 BOOST_CHECK_CLOSE_FRACTION(answer, t3, tol);
122 BOOST_CHECK_CLOSE_FRACTION(answer, t4, tol);
123 }
124 template <class From, class To>
125 void test_convert_neg_float_val(From const&, To const&, const boost::mpl::false_&)
126 {
127 }
128
129 template <class From, class To>
130 void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_floating_point> const&)
131 {
132 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
133
134 for(unsigned i = 0; i < 100; ++i)
135 {
136 From from = generate_random<From>(bits_wanted);
137 To answer = To(numerator(from)) / To(denominator(from));
138 To t1(from);
139 To t2 = from.template convert_to<To>();
140 To tol = std::numeric_limits<To>::is_specialized ? std::numeric_limits<To>::epsilon() : ldexp(To(1), 1 - bits_wanted);
141 tol *= 2;
142 BOOST_CHECK_CLOSE_FRACTION(answer, t1, tol);
143 BOOST_CHECK_CLOSE_FRACTION(answer, t2, tol);
144 test_convert_neg_float_val<From, To>(from, tol, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
145 }
146 }
147
148 template <class From, class To>
149 void test_convert_neg_rat_val(From from, const boost::mpl::true_&)
150 {
151 from = -from;
152 To t3(from);
153 To t4 = from.template convert_to<To>();
154 BOOST_CHECK_EQUAL(from.str(), t3.str());
155 BOOST_CHECK_EQUAL(from.str(), t4.str());
156 }
157 template <class From, class To>
158 void test_convert_neg_rat_val(From const&, const boost::mpl::false_&)
159 {
160 }
161
162 template <class From, class To>
163 void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_rational> const&)
164 {
165 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
166
167 for(unsigned i = 0; i < 100; ++i)
168 {
169 From from = generate_random<From>(bits_wanted);
170 To t1(from);
171 To t2 = from.template convert_to<To>();
172 BOOST_CHECK_EQUAL(from.str(), t1.str());
173 BOOST_CHECK_EQUAL(from.str(), t2.str());
174 test_convert_neg_rat_val<From, To>(from, boost::mpl::bool_<std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed>());
175 }
176 }
177
178
179
180
181 template <class From, class To>
182 void test_convert()
183 {
184 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
185 }
186
187
188 int main()
189 {
190 test_convert<cpp_rational, cpp_int>();
191 test_convert<cpp_rational, int128_t>();
192 test_convert<cpp_rational, uint128_t>();
193
194 test_convert<cpp_rational, cpp_bin_float_50>();
195
196 test_convert<cpp_rational, cpp_dec_float_50>();
197
198 #if defined(HAS_GMP)
199 test_convert<cpp_rational, mpz_int>();
200 test_convert<cpp_rational, mpq_rational>();
201 test_convert<cpp_rational, mpf_float_50>();
202 #endif
203 #if defined(HAS_MPFR)
204 test_convert<cpp_rational, mpfr_float_50>();
205 #endif
206 #if defined(HAS_MPFI)
207 test_convert<cpp_rational, mpfi_float_50>();
208 #endif
209 #ifdef HAS_TOMMATH
210 test_convert<cpp_rational, tom_int>();
211 test_convert<cpp_rational, tom_rational>();
212 #endif
213 return boost::report_errors();
214 }
215