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 while (val
!= prev_val
)
57 static boost::random::uniform_int_distribution
<e_type
> ui(-20, 20);
58 return ldexp(val
, ui(gen
));
61 template <class From
, class To
>
62 void test_convert_neg_int(From from
, const boost::mpl::true_
&)
66 To t4
= from
.template convert_to
<To
>();
67 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t3
));
68 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t4
));
70 template <class From
, class To
>
71 void test_convert_neg_int(From
const&, const boost::mpl::false_
&)
75 template <class From
, class To
>
76 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_integer
> const&)
78 for (unsigned i
= 0; i
< 100; ++i
)
80 From from
= generate_random
<From
>();
82 To t2
= from
.template convert_to
<To
>();
83 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t1
));
84 BOOST_CHECK_EQUAL(From(trunc(from
)), From(t2
));
85 test_convert_neg_int
<From
, To
>(from
, boost::mpl::bool_
< std::numeric_limits
<From
>::is_signed
&& std::numeric_limits
<To
>::is_signed
> ());
89 template <class From
, class To
>
90 void test_convert_neg_rat(From from
, const boost::mpl::true_
&)
94 To t4
= from
.template convert_to
<To
>();
95 BOOST_CHECK_EQUAL(From(t3
), from
);
96 BOOST_CHECK_EQUAL(From(t4
), from
);
98 template <class From
, class To
>
99 void test_convert_rat_int(From
const&, const boost::mpl::false_
&)
103 template <class From
, class To
>
104 void test_convert_imp(boost::mpl::int_
<number_kind_floating_point
> const&, boost::mpl::int_
<number_kind_rational
> const&)
106 for (unsigned i
= 0; i
< 100; ++i
)
108 From from
= generate_random
<From
>();
110 To t2
= from
.template convert_to
<To
>();
111 BOOST_CHECK_EQUAL(From(t1
), from
);
112 BOOST_CHECK_EQUAL(From(t2
), from
);
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());
158 // Some basic sanity checks first:
160 BOOST_CHECK_EQUAL(std::numeric_limits
<float128
>::epsilon(), float128("1.92592994438723585305597794258492732e-34"));
161 BOOST_CHECK_EQUAL((std::numeric_limits
<float128
>::min
)(), float128("3.36210314311209350626267781732175260e-4932"));
162 BOOST_CHECK_EQUAL((std::numeric_limits
<float128
>::max
)(), float128("1.18973149535723176508575932662800702e4932"));
163 BOOST_CHECK_EQUAL((std::numeric_limits
<float128
>::denorm_min
)(), float128("6.475175119438025110924438958227646552e-4966"));
164 BOOST_CHECK((boost::math::isinf
)((std::numeric_limits
<float128
>::infinity
)()));
165 BOOST_CHECK((std::numeric_limits
<float128
>::infinity
)() > 0);
166 BOOST_CHECK((boost::math::isnan
)((std::numeric_limits
<float128
>::quiet_NaN
)()));
168 test_convert
<float128
, cpp_int
>();
169 test_convert
<float128
, int128_t
>();
170 test_convert
<float128
, uint128_t
>();
172 test_convert
<float128
, cpp_rational
>();
174 test_convert
<float128
, cpp_dec_float_50
>();
177 test_convert
<float128
, mpz_int
>();
178 test_convert
<float128
, mpq_rational
>();
179 test_convert
<float128
, mpf_float_50
>();
181 #if defined(HAS_MPFR)
182 test_convert
<float128
, mpfr_float_50
>();
184 #if defined(HAS_MPFI)
185 test_convert
<float128
, mpfi_float_50
>();
188 test_convert
<float128
, tom_int
>();
189 test_convert
<float128
, tom_rational
>();
191 return boost::report_errors();
196 int main() { return 0; }