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
8 #define _SCL_SECURE_NO_WARNINGS
11 #include <boost/detail/lightweight_test.hpp>
12 #include <boost/array.hpp>
15 #include <boost/multiprecision/cpp_bin_float.hpp>
16 #include <boost/multiprecision/cpp_dec_float.hpp>
17 #include <boost/multiprecision/cpp_int.hpp>
18 #include <boost/random.hpp>
21 #include <boost/multiprecision/gmp.hpp>
24 #include <boost/multiprecision/tommath.hpp>
27 #include <boost/multiprecision/mpfr.hpp>
32 using namespace boost::multiprecision;
33 using namespace boost::random;
35 independent_bits_engine<mt11213b, 1024, cpp_int> gen;
38 for (unsigned i = 0; i < 100; ++i)
42 // Integer to integer conversions first:
47 BOOST_CHECK_EQUAL(t, c);
50 BOOST_CHECK_EQUAL(t, c);
55 BOOST_CHECK_EQUAL(t2, c);
58 BOOST_CHECK_EQUAL(t2, c);
61 // Now integer to float:
63 typedef number<cpp_dec_float<500> > dec_float_500;
65 dec_float_500 df2(c.str());
66 BOOST_CHECK_EQUAL(df, df2);
69 BOOST_CHECK_EQUAL(df, df2);
71 typedef number<gmp_float<500> > mpf_type;
73 mpf_type mpf2(c.str());
74 BOOST_CHECK_EQUAL(mpf, mpf2);
77 BOOST_CHECK_EQUAL(mpf, mpf2);
80 typedef number<mpfr_float_backend<500> > mpfr_type;
82 mpfr_type mpfr2(c.str());
83 BOOST_CHECK_EQUAL(mpfr, mpfr2);
86 BOOST_CHECK_EQUAL(mpfr, mpfr2);
89 // Now float to float:
92 df /= dec_float_500(gen());
93 dec_float_500 tol("1e-500");
96 mpf2 = static_cast<mpf_type>(df.str());
97 BOOST_CHECK_EQUAL(mpf, mpf2);
99 df2 = static_cast<dec_float_500>(mpf.str());
100 BOOST_CHECK(fabs((df - df2) / df) < tol);
104 mpfr2 = static_cast<mpfr_type>(df.str());
105 BOOST_CHECK_EQUAL(mpfr, mpfr2);
107 df2 = static_cast<dec_float_500>(mpfr.str());
108 BOOST_CHECK(fabs((df - df2) / df) < tol);
111 // Rational to rational conversions:
113 cpp_rational cppr(c, gen()), cppr2, cppr3;
115 mpq_rational mpq(cppr);
117 BOOST_CHECK_EQUAL(cppr, cppr2);
120 tom_rational tr(cppr);
122 BOOST_CHECK_EQUAL(cppr, cppr3);
125 // Integer to rational conversions:
129 mpq_rational mpq2 = static_cast<mpq_rational>(c.str());
130 BOOST_CHECK_EQUAL(mpq, mpq2);
134 tom_rational tr2 = static_cast<tom_rational>(c.str());
135 BOOST_CHECK_EQUAL(tr, tr2);
138 // Rational to float:
141 df2.assign(numerator(cppr));
142 df2 /= dec_float_500(denominator(cppr));
143 BOOST_CHECK(fabs(df - df2) / df2 < tol);
145 // Float to rational:
147 static const int max_range = std::numeric_limits<double>::digits >= std::numeric_limits<int>::digits ? (std::numeric_limits<int>::max)() : (1 << (std::numeric_limits<double>::digits - 1)) - 1;
148 static const int min_range = std::numeric_limits<double>::digits >= std::numeric_limits<int>::digits ? (std::numeric_limits<int>::min)() : -(1 << (std::numeric_limits<double>::digits - 1)) + 1;
149 static const boost::random::uniform_int_distribution<> i_val_dist(min_range, max_range);
150 static const boost::random::uniform_int_distribution<> i_exp_dist(std::numeric_limits<double>::min_exponent, std::numeric_limits<double>::max_exponent - 2 - std::numeric_limits<int>::digits);
151 int iv = i_val_dist(small_gen);
152 int eval = i_exp_dist(small_gen);
154 dv = ldexp(dv, eval);
163 BOOST_CHECK_EQUAL(cppr, cppr2);
165 // Again but with bigger numbers:
167 cpp_int cppi2 = gen();
168 number<cpp_bin_float<1030> > cppbf(cppi2);
169 cppbf = ldexp(cppbf, eval);
176 BOOST_CHECK_EQUAL(cppr, cppr2);
178 // MSVC will compile either the code above, or
179 // the code below, but not both in the same file.
180 // Other compilers including Intel in msvc-compatibity
181 // mode have no such difficulty. Indeed the fact that
182 // the presence of the code below causes the code above to
183 // fail to compile strongly suggests a compiler bug.
185 #if !defined(BOOST_MSVC)
187 // Again but with bigger base 10 numbers:
189 number<cpp_dec_float<std::numeric_limits<int1024_t>::digits10 + 3> > cppdec2(cppi2);
190 cppdec2 = scalbn(cppdec2, eval);
191 cppr.assign(cppdec2);
194 cppi = pow(cppi, abs(eval));
199 BOOST_CHECK_EQUAL(cppr, cppr2);
203 return boost::report_errors();