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_
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>
36 using namespace boost::multiprecision
;
39 #pragma warning(disable:4127)
47 static boost::random::mt19937 gen
;
50 while(val
!= prev_val
)
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 boost::mpl::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 boost::mpl::false_
&)
77 template <class From
, class To
>
78 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::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
, boost::mpl::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 boost::mpl::true_
&)
96 To t4
= from
.template convert_to
<To
>();
97 From tol
= std::numeric_limits
<From
>::epsilon();
98 BOOST_CHECK_CLOSE_FRACTION(From(t3
), from
, tol
);
99 BOOST_CHECK_CLOSE_FRACTION(From(t4
), from
, tol
);
101 template <class From
, class To
>
102 void test_convert_rat_int(From
const&, const boost::mpl::false_
&)
106 template <class From
, class To
>
107 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_rational
> const&)
109 for(unsigned i
= 0; i
< 100; ++i
)
111 From from
= generate_random
<From
>();
113 To t2
= from
.template convert_to
<To
>();
114 From tol
= std::numeric_limits
<From
>::epsilon();
115 BOOST_CHECK_CLOSE_FRACTION(From(t1
), from
, tol
);
116 BOOST_CHECK_CLOSE_FRACTION(From(t2
), from
, tol
);
117 test_convert_neg_rat
<From
, To
>(from
, boost::mpl::bool_
<std::numeric_limits
<From
>::is_signed
&& std::numeric_limits
<To
>::is_signed
>());
121 template <class From
, class To
>
122 void test_convert_neg_float(From from
, const boost::mpl::true_
&)
126 To t4
= from
.template convert_to
<To
>();
127 To
answer(from
.str());
128 To tol
= (std::max
)(std::numeric_limits
<To
>::epsilon(), To(std::numeric_limits
<From
>::epsilon())) * 2;
129 BOOST_CHECK_CLOSE_FRACTION(t3
, answer
, tol
);
130 BOOST_CHECK_CLOSE_FRACTION(t4
, answer
, tol
);
132 template <class From
, class To
>
133 void test_convert_neg_float(From
const&, const boost::mpl::false_
&)
137 template <class From
, class To
>
138 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_floating_point
> const&)
140 for(unsigned i
= 0; i
< 100; ++i
)
142 From from
= generate_random
<From
>();
144 To t2
= from
.template convert_to
<To
>();
145 To
answer(from
.str());
146 To tol
= (std::max
)(std::numeric_limits
<To
>::epsilon(), To(std::numeric_limits
<From
>::epsilon())) * 2;
147 BOOST_CHECK_CLOSE_FRACTION(t1
, answer
, tol
);
148 BOOST_CHECK_CLOSE_FRACTION(t2
, answer
, tol
);
149 test_convert_neg_float
<From
, To
>(from
, boost::mpl::bool_
<std::numeric_limits
<From
>::is_signed
&& std::numeric_limits
<To
>::is_signed
>());
153 template <class From
, class To
>
156 test_convert_imp
<From
, To
>(typename number_category
<From
>::type(), typename number_category
<To
>::type());
162 test_convert
<mpf_float_50
, cpp_int
>();
163 test_convert
<mpf_float_50
, int128_t
>();
164 test_convert
<mpf_float_50
, uint128_t
>();
167 test_convert
<mpf_float_50
, cpp_rational
>();
169 test_convert
<mpf_float_50
, cpp_dec_float_50
>();
171 test_convert
<mpf_float_50
, mpz_int
>();
172 test_convert
<mpf_float_50
, mpq_rational
>();
173 #if defined(HAS_MPFR)
174 test_convert
<mpf_float_50
, mpfr_float_50
>();
176 #if defined(HAS_MPFI)
177 test_convert
<mpf_float_50
, mpfi_float_50
>();
180 test_convert
<mpf_float_50
, tom_int
>();
181 test_convert
<mpf_float_50
, tom_rational
>();
184 test_convert
<mpf_float_50
, float128
>();
186 return boost::report_errors();
191 int main() { return 0; }