• 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 #include <boost/multiprecision/cpp_int.hpp>
11 #include <boost/random/mersenne_twister.hpp>
12 #include <boost/random/uniform_int.hpp>
13 #include "test.hpp"
14 
15 #if defined(HAS_GMP)
16 #include <boost/multiprecision/gmp.hpp>
17 #endif
18 #if defined(HAS_MPFR)
19 #include <boost/multiprecision/mpfr.hpp>
20 #endif
21 #if defined(HAS_MPFI)
22 #include <boost/multiprecision/mpfi.hpp>
23 #endif
24 #ifdef HAS_TOMMATH
25 #include <boost/multiprecision/tommath.hpp>
26 #endif
27 #ifdef HAS_FLOAT128
28 #include <boost/multiprecision/float128.hpp>
29 #endif
30 #include <boost/multiprecision/cpp_bin_float.hpp>
31 #include <boost/multiprecision/cpp_dec_float.hpp>
32 
33 using namespace boost::multiprecision;
34 
35 #ifdef BOOST_MSVC
36 #pragma warning(disable : 4127)
37 #endif
38 
39 template <class T>
generate_random()40 T generate_random()
41 {
42    typedef int                   e_type;
43    static boost::random::mt19937 gen;
44    T                             val      = gen();
45    T                             prev_val = -1;
46    while (val != prev_val)
47    {
48       val *= (gen.max)();
49       prev_val = val;
50       val += gen();
51    }
52    e_type e;
53    val = frexp(val, &e);
54 
55    static boost::random::uniform_int_distribution<e_type> ui(-20, 20);
56    return ldexp(val, ui(gen));
57 }
58 
59 template <class From, class To>
test_convert_neg_int(From from,const boost::mpl::true_ &)60 void test_convert_neg_int(From from, const boost::mpl::true_&)
61 {
62    from = -from;
63    To t3(from);
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));
67 }
68 template <class From, class To>
test_convert_neg_int(From const &,const boost::mpl::false_ &)69 void test_convert_neg_int(From const&, const boost::mpl::false_&)
70 {
71 }
72 
73 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_integer> const &)74 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_integer> const&)
75 {
76    for (unsigned i = 0; i < 100; ++i)
77    {
78       From from = generate_random<From>();
79       To   t1(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 > ());
84    }
85 }
86 
87 template <class From, class To>
test_convert_neg_rat(From from,const boost::mpl::true_ &)88 void test_convert_neg_rat(From from, const boost::mpl::true_&)
89 {
90    from = -from;
91    To   t3(from);
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);
96 }
97 template <class From, class To>
test_convert_rat_int(From const &,const boost::mpl::false_ &)98 void test_convert_rat_int(From const&, const boost::mpl::false_&)
99 {
100 }
101 
102 template <class From, class To>
test_convert_imp(boost::mpl::int_<number_kind_floating_point> const &,boost::mpl::int_<number_kind_rational> const &)103 void test_convert_imp(boost::mpl::int_<number_kind_floating_point> const&, boost::mpl::int_<number_kind_rational> const&)
104 {
105    for (unsigned i = 0; i < 100; ++i)
106    {
107       From from = generate_random<From>();
108       To   t1(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 > ());
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<cpp_dec_float_50, cpp_int>();
158    test_convert<cpp_dec_float_50, int128_t>();
159    test_convert<cpp_dec_float_50, uint128_t>();
160 
161    test_convert<cpp_dec_float_50, cpp_rational>();
162 
163    test_convert<cpp_dec_float_50, cpp_bin_float_50>();
164 
165 #if defined(HAS_GMP)
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>();
169 #endif
170 #if defined(HAS_MPFR)
171    test_convert<cpp_dec_float_50, mpfr_float_50>();
172 #endif
173 #if defined(HAS_MPFI)
174    test_convert<cpp_dec_float_50, mpfi_float_50>();
175 #endif
176 #ifdef HAS_TOMMATH
177    test_convert<cpp_dec_float_50, tom_int>();
178    test_convert<cpp_dec_float_50, tom_rational>();
179 #endif
180 #ifdef HAS_FLOAT128
181    test_convert<cpp_dec_float_50, float128>();
182 #endif
183    return boost::report_errors();
184 }
185