• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
5 
6 #ifdef _MSC_VER
7 #define _SCL_SECURE_NO_WARNINGS
8 #endif
9 
10 #if defined(HAS_GMP)
11 
12 #include <boost/multiprecision/cpp_int.hpp>
13 #include <boost/random/mersenne_twister.hpp>
14 #include <boost/random/uniform_int.hpp>
15 #include "test.hpp"
16 
17 #if defined(HAS_GMP)
18 #include <boost/multiprecision/gmp.hpp>
19 #endif
20 #if defined(HAS_MPFR)
21 #include <boost/multiprecision/mpfr.hpp>
22 #endif
23 #if defined(HAS_MPFI)
24 #include <boost/multiprecision/mpfi.hpp>
25 #endif
26 #ifdef HAS_TOMMATH
27 #include <boost/multiprecision/tommath.hpp>
28 #endif
29 #ifdef HAS_FLOAT128
30 #include <boost/multiprecision/float128.hpp>
31 #endif
32 #include <boost/multiprecision/cpp_bin_float.hpp>
33 #include <boost/multiprecision/cpp_dec_float.hpp>
34 
35 using namespace boost::multiprecision;
36 
37 #ifdef BOOST_MSVC
38 #pragma warning(disable : 4127)
39 #endif
40 
41 template <class T>
generate_random()42 T generate_random()
43 {
44    typedef int                   e_type;
45    static boost::random::mt19937 gen;
46    T                             val      = gen();
47    T                             prev_val = -1;
48    while (val != prev_val)
49    {
50       val *= (gen.max)();
51       prev_val = val;
52       val += gen();
53    }
54    e_type e;
55    val = frexp(val, &e);
56 
57    static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
58    return ldexp(val, ui(gen));
59 }
60 
61 template <class From, class To>
test_convert_neg_int(From from,const boost::mpl::true_ &)62 void test_convert_neg_int(From from, const boost::mpl::true_&)
63 {
64    from = -from;
65    To t3(from);
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));
69 }
70 template <class From, class To>
test_convert_neg_int(From const &,const boost::mpl::false_ &)71 void test_convert_neg_int(From const&, const boost::mpl::false_&)
72 {
73 }
74 
75 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_integer> const &)76 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_integer> const&)
77 {
78    for (unsigned i = 0; i < 100; ++i)
79    {
80       From from = generate_random<From>();
81       To   t1(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 > ());
86    }
87 }
88 
89 template <class From, class To>
test_convert_neg_rat(From from,const boost::mpl::true_ &)90 void test_convert_neg_rat(From from, const boost::mpl::true_&)
91 {
92    from = -from;
93    To   t3(from);
94    To   t4  = from.template convert_to<To>();
95    From tol = std::numeric_limits<From>::epsilon();
96    BOOST_CHECK_CLOSE_FRACTION(From(t3), from, tol);
97    BOOST_CHECK_CLOSE_FRACTION(From(t4), from, tol);
98 }
99 template <class From, class To>
test_convert_rat_int(From const &,const boost::mpl::false_ &)100 void test_convert_rat_int(From const&, const boost::mpl::false_&)
101 {
102 }
103 
104 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_rational> const &)105 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_rational> const&)
106 {
107    for (unsigned i = 0; i < 100; ++i)
108    {
109       From from = generate_random<From>();
110       To   t1(from);
111       To   t2  = from.template convert_to<To>();
112       From tol = std::numeric_limits<From>::epsilon();
113       BOOST_CHECK_CLOSE_FRACTION(From(t1), from, tol);
114       BOOST_CHECK_CLOSE_FRACTION(From(t2), from, tol);
115       test_convert_neg_rat<From, To>(from, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
116    }
117 }
118 
119 template <class From, class To>
test_convert_neg_float(From from,const boost::mpl::true_ &)120 void test_convert_neg_float(From from, const boost::mpl::true_&)
121 {
122    from = -from;
123    To t3(from);
124    To t4 = from.template convert_to<To>();
125    To answer(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);
129 }
130 template <class From, class To>
test_convert_neg_float(From const &,const boost::mpl::false_ &)131 void test_convert_neg_float(From const&, const boost::mpl::false_&)
132 {
133 }
134 
135 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_floating_point> const &)136 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_floating_point> const&)
137 {
138    for (unsigned i = 0; i < 100; ++i)
139    {
140       From from = generate_random<From>();
141       To   t1(from);
142       To   t2 = from.template convert_to<To>();
143       To   answer(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, boost::mpl::bool_ < std::numeric_limits<From>::is_signed && std::numeric_limits<To>::is_signed > ());
148    }
149 }
150 
151 template <class From, class To>
test_convert()152 void test_convert()
153 {
154    test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
155 }
156 
main()157 int main()
158 {
159    test_convert<mpf_float_50, cpp_int>();
160    test_convert<mpf_float_50, int128_t>();
161    test_convert<mpf_float_50, uint128_t>();
162 
163    test_convert<mpf_float_50, cpp_rational>();
164 
165    test_convert<mpf_float_50, cpp_dec_float_50>();
166 
167    test_convert<mpf_float_50, mpz_int>();
168    test_convert<mpf_float_50, mpq_rational>();
169 #if defined(HAS_MPFR)
170    test_convert<mpf_float_50, mpfr_float_50>();
171 #endif
172 #if defined(HAS_MPFI)
173    test_convert<mpf_float_50, mpfi_float_50>();
174 #endif
175 #ifdef HAS_TOMMATH
176    test_convert<mpf_float_50, tom_int>();
177    test_convert<mpf_float_50, tom_rational>();
178 #endif
179 #ifdef HAS_FLOAT128
180    test_convert<mpf_float_50, float128>();
181 #endif
182    return boost::report_errors();
183 }
184 
185 #else
186 
main()187 int main() { return 0; }
188 
189 #endif
190