1 // Copyright John Maddock 2013.
3 // Use, modification and distribution are subject to the
4 // Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt
6 // or copy at http://www.boost.org/LICENSE_1_0.txt)
9 #define _SCL_SECURE_NO_WARNINGS
12 #if defined(TEST1) || defined(TEST2) || defined(TEST3) || defined(TEST4)
13 #include <boost/multiprecision/cpp_bin_float.hpp>
14 #include <boost/multiprecision/cpp_int.hpp>
16 #include <boost/multiprecision/mpfr.hpp>
18 #include <boost/math/special_functions/next.hpp>
20 #include <boost/random/mersenne_twister.hpp>
21 #include <boost/random/uniform_int.hpp>
22 #include <boost/chrono.hpp>
24 #include <boost/array.hpp>
29 #pragma warning(disable : 4127)
32 template <class Clock>
35 typedef typename Clock::duration duration;
38 m_start = Clock::now();
42 return Clock::now() - m_start;
46 m_start = Clock::now();
50 typename Clock::time_point m_start;
58 template <class T, boost::multiprecision::expression_template_option ET>
59 struct exponent_type<boost::multiprecision::number<T, ET> >
61 typedef typename T::exponent_type type;
65 T generate_random_float()
68 typedef typename exponent_type<T>::type e_type;
69 static boost::random::mt19937 gen;
72 while (val != prev_val)
81 static const int max_exponent_value = (std::min)(static_cast<int>(std::numeric_limits<T>::max_exponent - std::numeric_limits<T>::digits - 20), 2000);
82 static boost::random::uniform_int_distribution<e_type> ui(0, max_exponent_value);
83 return ldexp(val, ui(gen));
86 template <class Float, class Rat>
87 void do_round_trip(const Float& val)
89 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
93 BOOST_CHECK_EQUAL(val, new_f);
95 // Try adding or subtracting an insignificant amount
96 // (0.25ulp) from rat and check that it rounds to the same value:
98 typename exponent_type<Float>::type e;
99 Float t = frexp(val, &e);
100 (void)t; // warning suppression
101 e -= std::numeric_limits<Float>::digits + 2;
102 BOOST_ASSERT(val == (val + ldexp(Float(1), e)));
104 typedef typename boost::multiprecision::component_type<Rat>::type i_type;
106 i <<= (e < 0 ? -e : e);
110 delta = Rat(i_type(1), i);
111 rounded = rat + delta;
112 new_f = static_cast<Float>(rounded);
113 BOOST_CHECK_EQUAL(val, new_f);
114 rounded = rat - delta;
115 new_f = static_cast<Float>(rounded);
116 BOOST_CHECK_EQUAL(val, new_f);
119 rounded = rat + delta;
120 new_f = static_cast<Float>(rounded);
121 BOOST_CHECK_EQUAL(val, new_f);
122 rounded = rat - delta;
123 new_f = static_cast<Float>(rounded);
124 BOOST_CHECK_EQUAL(val, new_f);
127 rounded = rat + delta;
128 new_f = static_cast<Float>(rounded);
129 BOOST_CHECK_EQUAL(val, new_f);
130 rounded = rat - delta;
131 new_f = static_cast<Float>(rounded);
132 BOOST_CHECK_EQUAL(val, new_f);
136 template <class Float, class Rat>
137 void test_round_trip()
139 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
140 std::cout << "Testing types " << typeid(Float).name() << " <<==>> " << typeid(Rat).name() << std::endl;
141 std::cout << "digits = " << std::numeric_limits<Float>::digits << std::endl;
142 std::cout << "digits10 = " << std::numeric_limits<Float>::digits10 << std::endl;
143 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
144 std::cout << "max_digits10 = " << std::numeric_limits<Float>::max_digits10 << std::endl;
147 stopwatch<boost::chrono::high_resolution_clock> w;
151 #ifndef CI_SUPPRESS_KNOWN_ISSUES
152 while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
154 while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 50)
157 Float val = generate_random_float<Float>();
158 do_round_trip<Float, Rat>(val);
159 do_round_trip<Float, Rat>(Float(-val));
160 do_round_trip<Float, Rat>(Float(1 / val));
161 do_round_trip<Float, Rat>(Float(-1 / val));
163 if (boost::detail::test_errors() > 100)
167 std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
168 std::cout << "Total values tested: " << count << std::endl;
173 Int generate_random_int()
175 static boost::random::mt19937 gen;
176 static boost::random::uniform_int_distribution<boost::random::mt19937::result_type> d(1, 20);
183 for (int i = 0; i < lim; ++i)
191 template <class Float, class Rat>
192 void test_random_rationals()
194 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
195 std::cout << "Testing types " << typeid(Float).name() << " <<==>> " << typeid(Rat).name() << std::endl;
196 std::cout << "digits = " << std::numeric_limits<Float>::digits << std::endl;
197 std::cout << "digits10 = " << std::numeric_limits<Float>::digits10 << std::endl;
198 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
199 std::cout << "max_digits10 = " << std::numeric_limits<Float>::max_digits10 << std::endl;
202 typedef typename boost::multiprecision::component_type<Rat>::type i_type;
203 stopwatch<boost::chrono::high_resolution_clock> w;
207 #ifndef CI_SUPPRESS_KNOWN_ISSUES
208 while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 200)
210 while (boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() < 50)
213 Rat rat(generate_random_int<i_type>(), generate_random_int<i_type>());
215 Rat new_rat(f); // rounded value
216 int c = new_rat.compare(rat);
219 // If f was rounded down, next float up must be above the original value:
220 f = boost::math::float_next(f);
222 BOOST_CHECK(new_rat >= rat);
226 // If f was rounded up, next float down must be below the original value:
227 f = boost::math::float_prior(f);
229 BOOST_CHECK(new_rat <= rat);
233 // Values were equal... nothing to test.
235 if (boost::detail::test_errors() > 100)
239 std::cout << "Execution time = " << boost::chrono::duration_cast<boost::chrono::duration<double> >(w.elapsed()).count() << "s" << std::endl;
240 std::cout << "Total values tested: " << count << std::endl;
246 void double_spot_tests()
248 boost::multiprecision::cpp_rational rat = 1;
249 boost::multiprecision::cpp_rational twiddle(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 54));
250 rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 50));
252 double d = rat.convert_to<double>();
255 BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
257 // tie: round to even rounds down
258 BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
260 BOOST_CHECK_NE(d, rat.convert_to<double>());
262 BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
263 rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 52));
264 // tie, but last bit is now a 1 so we round up:
265 BOOST_CHECK_NE(d, rat.convert_to<double>());
272 using namespace boost::multiprecision;
273 #if defined(TEST1) && !defined(BOOST_MSVC)
274 test_round_trip<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
277 test_round_trip<double, cpp_rational>();
278 #elif defined(TEST3) && !defined(BOOST_MSVC)
279 test_random_rationals<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
281 test_random_rationals<double, cpp_rational>();
283 // This does not work: gmp does not correctly round integer to float or
284 // rational to float conversions:
285 test_round_trip<double, mpq_rational>();
287 test_round_trip<mpfr_float_100, mpq_rational>();
289 test_random_rationals<mpfr_float_100, mpq_rational>();
291 test_random_rationals<double, mpq_rational>();
293 return boost::report_errors();