• 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_MPFR)
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    BOOST_CHECK_EQUAL(From(t3), from);
96    BOOST_CHECK_EQUAL(From(t4), from);
97 }
98 template <class From, class To>
test_convert_rat_int(From const &,const boost::mpl::false_ &)99 void test_convert_rat_int(From const&, const boost::mpl::false_&)
100 {
101 }
102 
103 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_rational> const &)104 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_rational> const&)
105 {
106    for (unsigned i = 0; i < 100; ++i)
107    {
108       From from = generate_random<From>();
109       To   t1(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 > ());
114    }
115 }
116 
117 template <class From, class To>
test_convert_neg_float(From from,const boost::mpl::true_ &)118 void test_convert_neg_float(From from, const boost::mpl::true_&)
119 {
120    from = -from;
121    To t3(from);
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);
127 }
128 template <class From, class To>
test_convert_neg_float(From const &,const boost::mpl::false_ &)129 void test_convert_neg_float(From const&, const boost::mpl::false_&)
130 {
131 }
132 
133 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_floating_point> const &)134 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_floating_point> const&)
135 {
136    for (unsigned i = 0; i < 100; ++i)
137    {
138       From from = generate_random<From>();
139       To   t1(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 > ());
146    }
147 }
148 
149 template <class From, class To>
test_convert()150 void test_convert()
151 {
152    test_convert_imp<From, To>(typename number_category<From>::type(), typename number_category<To>::type());
153 }
154 
main()155 int main()
156 {
157    test_convert<mpfr_float_50, cpp_int>();
158    test_convert<mpfr_float_50, int128_t>();
159    test_convert<mpfr_float_50, uint128_t>();
160 
161    test_convert<mpfr_float_50, cpp_rational>();
162 
163    test_convert<mpfr_float_50, cpp_dec_float_50>();
164 
165 #if defined(HAS_GMP)
166    test_convert<mpfr_float_50, mpz_int>();
167    test_convert<mpfr_float_50, mpq_rational>();
168    test_convert<mpfr_float_50, mpf_float_50>();
169 #endif
170 #if defined(HAS_MPFI)
171    test_convert<mpfr_float_50, mpfi_float_50>();
172 #endif
173 #ifdef HAS_TOMMATH
174    test_convert<mpfr_float_50, tom_int>();
175    test_convert<mpfr_float_50, tom_rational>();
176 #endif
177 #ifdef HAS_FLOAT128
178    test_convert<mpfr_float_50, float128>();
179 #endif
180    return boost::report_errors();
181 }
182 
183 #else
184 
main()185 int main() { return 0; }
186 
187 #endif
188