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>
14 #include <boost/random/uniform_int.hpp>
18 #include <boost/multiprecision/gmp.hpp>
21 #include <boost/multiprecision/mpfr.hpp>
24 #include <boost/multiprecision/mpfi.hpp>
27 #include <boost/multiprecision/tommath.hpp>
30 #include <boost/multiprecision/float128.hpp>
32 #include <boost/multiprecision/cpp_bin_float.hpp>
33 #include <boost/multiprecision/cpp_dec_float.hpp>
35 using namespace boost::multiprecision
;
38 #pragma warning(disable : 4127)
45 static boost::random::mt19937 gen
;
48 int digits_so_far
= 0;
49 while (digits_so_far
< std::numeric_limits
<T
>::digits
)
54 digits_so_far
+= std::numeric_limits
<boost::random::mt19937::result_type
>::digits
;
59 static boost::random::uniform_int_distribution
<e_type
> ui(-20, 20);
60 return ldexp(val
, ui(gen
));
63 template <class From
, class To
>
64 void test_convert_neg_int(From from
, const std::integral_constant
<bool, true>&)
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
));
72 template <class From
, class To
>
73 void test_convert_neg_int(From
const&, const std::integral_constant
<bool, false>&)
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&)
80 for (unsigned i
= 0; i
< 100; ++i
)
82 From from
= generate_random
<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
> ());
91 template <class From
, class To
>
92 void test_convert_neg_rat(From from
, const std::integral_constant
<bool, true>&)
96 To t4
= from
.template convert_to
<To
>();
97 BOOST_CHECK_EQUAL(From(t3
), median(from
));
98 BOOST_CHECK_EQUAL(From(t4
), median(from
));
100 template <class From
, class To
>
101 void test_convert_rat_int(From
const&, const std::integral_constant
<bool, false>&)
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&)
108 for (unsigned i
= 0; i
< 100; ++i
)
110 From from
= generate_random
<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
> ());
119 template <class From
, class To
>
120 void test_convert_neg_float(From from
, const std::integral_constant
<bool, true>&)
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
);
130 template <class From
, class To
>
131 void test_convert_neg_float(From
const&, const std::integral_constant
<bool, false>&)
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&)
138 for (unsigned i
= 0; i
< 100; ++i
)
140 From from
= generate_random
<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
> ());
151 template <class From
, class To
>
154 test_convert_imp
<From
, To
>(typename number_category
<From
>::type(), typename number_category
<To
>::type());
161 test_convert
<mpfi_float_50
, cpp_int
>();
162 test_convert
<mpfi_float_50
, int128_t
>();
163 test_convert
<mpfi_float_50
, uint128_t
>();
165 test_convert
<mpfi_float_50
, cpp_rational
>();
167 test_convert
<mpfi_float_50
, cpp_dec_float_50
>();
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
>();
174 #if defined(HAS_MPFR)
175 test_convert
<mpfi_float_50
, mpfr_float_50
>();
178 test_convert
<mpfi_float_50
, tom_int
>();
179 test_convert
<mpfi_float_50
, tom_rational
>();
182 test_convert
<mpfi_float_50
, float128
>();
189 return boost::report_errors();
194 int main() { return 0; }