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 while(boost::chrono::duration_cast
<boost::chrono::duration
<double> >(w
.elapsed()).count() < 200)
153 Float val
= generate_random_float
<Float
>();
154 do_round_trip
<Float
, Rat
>(val
);
155 do_round_trip
<Float
, Rat
>(Float(-val
));
156 do_round_trip
<Float
, Rat
>(Float(1/val
));
157 do_round_trip
<Float
, Rat
>(Float(-1/val
));
159 if(boost::detail::test_errors() > 100)
163 std::cout
<< "Execution time = " << boost::chrono::duration_cast
<boost::chrono::duration
<double> >(w
.elapsed()).count() << "s" << std::endl
;
164 std::cout
<< "Total values tested: " << count
<< std::endl
;
169 Int
generate_random_int()
171 static boost::random::mt19937 gen
;
172 static boost::random::uniform_int_distribution
<boost::random::mt19937::result_type
> d(1, 20);
179 for(int i
= 0; i
< lim
; ++i
)
187 template <class Float
, class Rat
>
188 void test_random_rationals()
190 #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS
191 std::cout
<< "Testing types " << typeid(Float
).name() << " <<==>> " << typeid(Rat
).name() << std::endl
;
192 std::cout
<< "digits = " << std::numeric_limits
<Float
>::digits
<< std::endl
;
193 std::cout
<< "digits10 = " << std::numeric_limits
<Float
>::digits10
<< std::endl
;
194 #ifndef BOOST_NO_CXX11_NUMERIC_LIMITS
195 std::cout
<< "max_digits10 = " << std::numeric_limits
<Float
>::max_digits10
<< std::endl
;
198 typedef typename
boost::multiprecision::component_type
<Rat
>::type i_type
;
199 stopwatch
<boost::chrono::high_resolution_clock
> w
;
203 while(boost::chrono::duration_cast
<boost::chrono::duration
<double> >(w
.elapsed()).count() < 200)
205 Rat
rat(generate_random_int
<i_type
>(), generate_random_int
<i_type
>());
207 Rat
new_rat(f
); // rounded value
208 int c
= new_rat
.compare(rat
);
211 // If f was rounded down, next float up must be above the original value:
212 f
= boost::math::float_next(f
);
214 BOOST_CHECK(new_rat
>= rat
);
218 // If f was rounded up, next float down must be below the original value:
219 f
= boost::math::float_prior(f
);
221 BOOST_CHECK(new_rat
<= rat
);
225 // Values were equal... nothing to test.
227 if(boost::detail::test_errors() > 100)
231 std::cout
<< "Execution time = " << boost::chrono::duration_cast
<boost::chrono::duration
<double> >(w
.elapsed()).count() << "s" << std::endl
;
232 std::cout
<< "Total values tested: " << count
<< std::endl
;
238 void double_spot_tests()
240 boost::multiprecision::cpp_rational rat
= 1;
241 boost::multiprecision::cpp_rational
twiddle(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 54));
242 rat
+= boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 50));
244 double d
= rat
.convert_to
<double>();
247 BOOST_CHECK_EQUAL(d
, rat
.convert_to
<double>());
249 // tie: round to even rounds down
250 BOOST_CHECK_EQUAL(d
, rat
.convert_to
<double>());
252 BOOST_CHECK_NE(d
, rat
.convert_to
<double>());
254 BOOST_CHECK_EQUAL(d
, rat
.convert_to
<double>());
255 rat
+= boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 52));
256 // tie, but last bit is now a 1 so we round up:
257 BOOST_CHECK_NE(d
, rat
.convert_to
<double>());
265 using namespace boost::multiprecision
;
266 #if defined(TEST1) && !defined(BOOST_MSVC)
267 test_round_trip
<number
<cpp_bin_float
<113, digit_base_2
, void, boost::int16_t> >, cpp_rational
>();
270 test_round_trip
<double, cpp_rational
>();
271 #elif defined(TEST3) && !defined(BOOST_MSVC)
272 test_random_rationals
<number
<cpp_bin_float
<113, digit_base_2
, void, boost::int16_t> >, cpp_rational
>();
274 test_random_rationals
<double, cpp_rational
>();
276 // This does not work: gmp does not correctly round integer to float or
277 // rational to float conversions:
278 test_round_trip
<double, mpq_rational
>();
280 test_round_trip
<mpfr_float_100
, mpq_rational
>();
282 test_random_rationals
<mpfr_float_100
, mpq_rational
>();
284 test_random_rationals
<double, mpq_rational
>();
286 return boost::report_errors();