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
7 #define _SCL_SECURE_NO_WARNINGS
12 #include <boost/multiprecision/cpp_int.hpp>
13 #include <boost/random/mersenne_twister.hpp>
15 #include <boost/multiprecision/gmp.hpp>
18 #include <boost/multiprecision/mpfr.hpp>
21 #include <boost/multiprecision/mpfi.hpp>
24 #include <boost/multiprecision/tommath.hpp>
27 #include <boost/multiprecision/float128.hpp>
29 #include <boost/multiprecision/cpp_bin_float.hpp>
30 #include <boost/multiprecision/cpp_dec_float.hpp>
32 using namespace boost::multiprecision;
35 #pragma warning(disable : 4127)
39 T generate_random_int(unsigned bits_wanted)
41 static boost::random::mt19937 gen;
42 typedef boost::random::mt19937::result_type random_type;
46 if (std::numeric_limits<T>::is_bounded && (bits_wanted == (unsigned)std::numeric_limits<T>::digits))
48 max_val = (std::numeric_limits<T>::max)();
49 digits = std::numeric_limits<T>::digits;
53 max_val = T(1) << bits_wanted;
57 unsigned bits_per_r_val = std::numeric_limits<random_type>::digits - 1;
58 while ((random_type(1) << bits_per_r_val) > (gen.max)())
61 unsigned terms_needed = digits / bits_per_r_val + 1;
64 for (unsigned i = 0; i < terms_needed; ++i)
74 T generate_random(unsigned bits_wanted)
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));
81 template <class From, class To>
82 void test_convert_neg_val(From from, const boost::mpl::true_&)
85 typename component_type<From>::type answer = numerator(from) / denominator(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());
91 template <class From, class To>
92 void test_convert_neg_val(From const&, const boost::mpl::false_&)
96 template <class From, class To>
97 void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_integer> const&)
99 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
101 for (unsigned i = 0; i < 100; ++i)
103 From from = generate_random<From>(bits_wanted);
104 typename component_type<From>::type answer = numerator(from) / denominator(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 > ());
113 template <class From, class To>
114 void test_convert_neg_float_val(From from, To const& tol, const boost::mpl::true_&)
117 To answer = To(numerator(from)) / To(denominator(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);
123 template <class From, class To>
124 void test_convert_neg_float_val(From const&, To const&, const boost::mpl::false_&)
128 template <class From, class To>
129 void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_floating_point> const&)
131 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
133 for (unsigned i = 0; i < 100; ++i)
135 From from = generate_random<From>(bits_wanted);
136 To answer = To(numerator(from)) / To(denominator(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);
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 > ());
147 template <class From, class To>
148 void test_convert_neg_rat_val(From from, const boost::mpl::true_&)
152 To t4 = from.template convert_to<To>();
153 BOOST_CHECK_EQUAL(from.str(), t3.str());
154 BOOST_CHECK_EQUAL(from.str(), t4.str());
156 template <class From, class To>
157 void test_convert_neg_rat_val(From const&, const boost::mpl::false_&)
161 template <class From, class To>
162 void test_convert_imp(boost::mpl::int_<number_kind_rational> const&, boost::mpl::int_<number_kind_rational> const&)
164 int bits_wanted = (std::min)((std::min)(std::numeric_limits<From>::digits, std::numeric_limits<To>::digits), 2000);
166 for (unsigned i = 0; i < 100; ++i)
168 From from = generate_random<From>(bits_wanted);
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 > ());
177 template <class From, class To>
180 test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
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>();
190 test_convert<mpq_rational, cpp_bin_float_50>();
192 test_convert<mpq_rational, cpp_dec_float_50>();
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>();
199 #if defined(HAS_MPFI)
200 test_convert<mpq_rational, mpfi_float_50>();
203 test_convert<mpq_rational, tom_int>();
204 test_convert<mpq_rational, tom_rational>();
206 return boost::report_errors();
211 int main() { return 0; }