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