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
10 #include <boost/multiprecision/cpp_int.hpp>
11 #include <boost/random/mersenne_twister.hpp>
12 #include <boost/random/uniform_int.hpp>
16 #include <boost/multiprecision/gmp.hpp>
19 #include <boost/multiprecision/mpfr.hpp>
22 #include <boost/multiprecision/mpfi.hpp>
25 #include <boost/multiprecision/tommath.hpp>
28 #include <boost/multiprecision/float128.hpp>
30 #include <boost/multiprecision/cpp_bin_float.hpp>
31 #include <boost/multiprecision/cpp_dec_float.hpp>
33 using namespace boost::multiprecision
;
36 #pragma warning(disable : 4127)
43 static boost::random::mt19937 gen
;
46 while (val
!= prev_val
)
55 static boost::random::uniform_int_distribution
<e_type
> ui(-20, 20);
56 return ldexp(val
, ui(gen
));
59 template <class From
, class To
>
60 void test_convert_neg_int(From from
, const boost::mpl::true_
&)
64 To t4
= from
.template convert_to
<To
>();
65 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t3
));
66 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t4
));
68 template <class From
, class To
>
69 void test_convert_neg_int(From
const&, const boost::mpl::false_
&)
73 template <class From
, class To
>
74 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_integer
> const&)
76 for (unsigned i
= 0; i
< 100; ++i
)
78 From from
= generate_random
<From
>();
80 To t2
= from
.template convert_to
<To
>();
81 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t1
));
82 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t2
));
83 test_convert_neg_int
<From
, To
>(from
, boost::mpl::bool_
< std::numeric_limits
<From
>::is_signed
&& std::numeric_limits
<To
>::is_signed
> ());
87 template <class From
, class To
>
88 void test_convert_neg_rat(From from
, const boost::mpl::true_
&)
92 To t4
= from
.template convert_to
<To
>();
93 From tol
= std::numeric_limits
<From
>::epsilon();
94 BOOST_CHECK_CLOSE_FRACTION(From(t3
), from
, tol
);
95 BOOST_CHECK_CLOSE_FRACTION(From(t4
), from
, tol
);
97 template <class From
, class To
>
98 void test_convert_rat_int(From
const&, const boost::mpl::false_
&)
102 template <class From
, class To
>
103 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_rational
> const&)
105 for (unsigned i
= 0; i
< 100; ++i
)
107 From from
= generate_random
<From
>();
109 To t2
= from
.template convert_to
<To
>();
110 From tol
= std::numeric_limits
<From
>::epsilon();
111 BOOST_CHECK_CLOSE_FRACTION(From(t1
), from
, tol
);
112 BOOST_CHECK_CLOSE_FRACTION(From(t2
), from
, tol
);
113 test_convert_neg_rat
<From
, To
>(from
, boost::mpl::bool_
< std::numeric_limits
<From
>::is_signed
&& std::numeric_limits
<To
>::is_signed
> ());
117 template <class From
, class To
>
118 void test_convert_neg_float(From from
, const boost::mpl::true_
&)
122 To t4
= from
.template convert_to
<To
>();
123 To
answer(from
.str());
124 To tol
= (std::max
)(std::numeric_limits
<To
>::epsilon(), To(std::numeric_limits
<From
>::epsilon())) * 2;
125 BOOST_CHECK_CLOSE_FRACTION(t3
, answer
, tol
);
126 BOOST_CHECK_CLOSE_FRACTION(t4
, answer
, tol
);
128 template <class From
, class To
>
129 void test_convert_neg_float(From
const&, const boost::mpl::false_
&)
133 template <class From
, class To
>
134 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_floating_point
> const&)
136 for (unsigned i
= 0; i
< 100; ++i
)
138 From from
= generate_random
<From
>();
140 To t2
= from
.template convert_to
<To
>();
141 To
answer(from
.str());
142 To tol
= (std::max
)(std::numeric_limits
<To
>::epsilon(), To(std::numeric_limits
<From
>::epsilon())) * 2;
143 BOOST_CHECK_CLOSE_FRACTION(t1
, answer
, tol
);
144 BOOST_CHECK_CLOSE_FRACTION(t2
, answer
, tol
);
145 test_convert_neg_float
<From
, To
>(from
, boost::mpl::bool_
< std::numeric_limits
<From
>::is_signed
&& std::numeric_limits
<To
>::is_signed
> ());
149 template <class From
, class To
>
152 test_convert_imp
<From
, To
>(typename number_category
<From
>::type(), typename number_category
<To
>::type());
157 test_convert
<cpp_dec_float_50
, cpp_int
>();
158 test_convert
<cpp_dec_float_50
, int128_t
>();
159 test_convert
<cpp_dec_float_50
, uint128_t
>();
161 test_convert
<cpp_dec_float_50
, cpp_rational
>();
163 test_convert
<cpp_dec_float_50
, cpp_bin_float_50
>();
166 test_convert
<cpp_dec_float_50
, mpz_int
>();
167 test_convert
<cpp_dec_float_50
, mpq_rational
>();
168 test_convert
<cpp_dec_float_50
, mpf_float_50
>();
170 #if defined(HAS_MPFR)
171 test_convert
<cpp_dec_float_50
, mpfr_float_50
>();
173 #if defined(HAS_MPFI)
174 test_convert
<cpp_dec_float_50
, mpfi_float_50
>();
177 test_convert
<cpp_dec_float_50
, tom_int
>();
178 test_convert
<cpp_dec_float_50
, tom_rational
>();
181 test_convert
<cpp_dec_float_50
, float128
>();
183 return boost::report_errors();