mirror of
https://github.com/boostorg/multiprecision.git
synced 2026-02-18 14:12:32 +00:00
Merge branch 'develop' of https://github.com/boostorg/multiprecision into develop
This commit is contained in:
@@ -310,7 +310,7 @@ typename enable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
|
||||
num = -num;
|
||||
}
|
||||
int denom_bits = msb(denom);
|
||||
int shift = std::numeric_limits<To>::digits + denom_bits - msb(num) + 1;
|
||||
int shift = std::numeric_limits<To>::digits + denom_bits - msb(num);
|
||||
if(shift > 0)
|
||||
num <<= shift;
|
||||
else if(shift < 0)
|
||||
@@ -318,7 +318,7 @@ typename enable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
|
||||
Integer q, r;
|
||||
divide_qr(num, denom, q, r);
|
||||
int q_bits = msb(q);
|
||||
if(q_bits == std::numeric_limits<To>::digits)
|
||||
if(q_bits == std::numeric_limits<To>::digits - 1)
|
||||
{
|
||||
//
|
||||
// Round up if 2 * r > denom:
|
||||
@@ -334,7 +334,7 @@ typename enable_if_c<is_number<To>::value || is_floating_point<To>::value>::type
|
||||
}
|
||||
else
|
||||
{
|
||||
BOOST_ASSERT(q_bits == 1 + std::numeric_limits<To>::digits);
|
||||
BOOST_ASSERT(q_bits == std::numeric_limits<To>::digits);
|
||||
//
|
||||
// We basically already have the rounding info:
|
||||
//
|
||||
|
||||
@@ -233,12 +233,40 @@ void test_random_rationals()
|
||||
#endif
|
||||
}
|
||||
|
||||
#if defined(TEST2)
|
||||
|
||||
void double_spot_tests()
|
||||
{
|
||||
boost::multiprecision::cpp_rational rat = 1;
|
||||
boost::multiprecision::cpp_rational twiddle(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 54));
|
||||
rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 50));
|
||||
|
||||
double d = rat.convert_to<double>();
|
||||
|
||||
rat += twiddle;
|
||||
BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
|
||||
rat += twiddle;
|
||||
// tie: round to even rounds down
|
||||
BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
|
||||
rat += twiddle;
|
||||
BOOST_CHECK_NE(d, rat.convert_to<double>());
|
||||
rat -= twiddle;
|
||||
BOOST_CHECK_EQUAL(d, rat.convert_to<double>());
|
||||
rat += boost::multiprecision::cpp_rational(boost::multiprecision::cpp_int(1), boost::multiprecision::cpp_int(boost::multiprecision::cpp_int(1) << 52));
|
||||
// tie, but last bit is now a 1 so we round up:
|
||||
BOOST_CHECK_NE(d, rat.convert_to<double>());
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
int main()
|
||||
{
|
||||
using namespace boost::multiprecision;
|
||||
#if defined(TEST1) && !defined(BOOST_MSVC)
|
||||
test_round_trip<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
|
||||
#elif defined(TEST2)
|
||||
double_spot_tests();
|
||||
test_round_trip<double, cpp_rational>();
|
||||
#elif defined(TEST3) && !defined(BOOST_MSVC)
|
||||
test_random_rationals<number<cpp_bin_float<113, digit_base_2, void, boost::int16_t> >, cpp_rational>();
|
||||
|
||||
Reference in New Issue
Block a user