/////////////////////////////////////////////////////////////// // Copyright 2012 John Maddock. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_ #ifdef TEST_VLD #include #endif #include #include #include "test.hpp" template struct is_boost_rational : public boost::mpl::false_{}; template struct is_checked_cpp_int : public boost::mpl::false_ {}; #ifdef BOOST_MSVC // warning C4127: conditional expression is constant #pragma warning(disable:4127) #endif template Target checked_lexical_cast(const Source& val) { #ifndef BOOST_NO_EXCEPTIONS try { #endif return boost::lexical_cast(val); #ifndef BOOST_NO_EXCEPTIONS } catch(...) { std::cerr << "Error in lexical cast\nSource type = " << typeid(Source).name() << " \"" << val << "\"\n"; std::cerr << "Target type = " << typeid(Target).name() << std::endl; throw; } #endif } bool isfloat(float){ return true; } bool isfloat(double){ return true; } bool isfloat(long double){ return true; } template bool isfloat(T){ return false; } namespace detail{ template typename boost::multiprecision::detail::expression::result_type abs(boost::multiprecision::detail::expression const& v) { typedef typename boost::multiprecision::detail::expression::result_type result_type; return v < 0 ? result_type(-v) : result_type(v); } } template struct is_twos_complement_integer : public boost::mpl::true_ {}; template struct related_type { typedef T type; }; template void test_comparisons(Val, Val, const boost::mpl::false_) {} int normalize_compare_result(int r) { return r > 0 ? 1 : r < 0 ? -1 : 0; } template void test_comparisons(Val a, Val b, const boost::mpl::true_) { Real r1(a); Real r2(b); Real z(1); int cr = a < b ? -1 : a > b ? 1 : 0; BOOST_CHECK_EQUAL(r1 == r2, a == b); BOOST_CHECK_EQUAL(r1 != r2, a != b); BOOST_CHECK_EQUAL(r1 <= r2, a <= b); BOOST_CHECK_EQUAL(r1 < r2, a < b); BOOST_CHECK_EQUAL(r1 >= r2, a >= b); BOOST_CHECK_EQUAL(r1 > r2, a > b); BOOST_CHECK_EQUAL(r1 == b, a == b); BOOST_CHECK_EQUAL(r1 != b, a != b); BOOST_CHECK_EQUAL(r1 <= b, a <= b); BOOST_CHECK_EQUAL(r1 < b, a < b); BOOST_CHECK_EQUAL(r1 >= b, a >= b); BOOST_CHECK_EQUAL(r1 > b, a > b); BOOST_CHECK_EQUAL(a == r2, a == b); BOOST_CHECK_EQUAL(a != r2, a != b); BOOST_CHECK_EQUAL(a <= r2, a <= b); BOOST_CHECK_EQUAL(a < r2, a < b); BOOST_CHECK_EQUAL(a >= r2, a >= b); BOOST_CHECK_EQUAL(a > r2, a > b); BOOST_CHECK_EQUAL(r1*z == r2, a == b); BOOST_CHECK_EQUAL(r1*z != r2, a != b); BOOST_CHECK_EQUAL(r1*z <= r2, a <= b); BOOST_CHECK_EQUAL(r1*z < r2, a < b); BOOST_CHECK_EQUAL(r1*z >= r2, a >= b); BOOST_CHECK_EQUAL(r1*z > r2, a > b); BOOST_CHECK_EQUAL(r1 == r2*z, a == b); BOOST_CHECK_EQUAL(r1 != r2*z, a != b); BOOST_CHECK_EQUAL(r1 <= r2*z, a <= b); BOOST_CHECK_EQUAL(r1 < r2*z, a < b); BOOST_CHECK_EQUAL(r1 >= r2*z, a >= b); BOOST_CHECK_EQUAL(r1 > r2*z, a > b); BOOST_CHECK_EQUAL(r1*z == r2*z, a == b); BOOST_CHECK_EQUAL(r1*z != r2*z, a != b); BOOST_CHECK_EQUAL(r1*z <= r2*z, a <= b); BOOST_CHECK_EQUAL(r1*z < r2*z, a < b); BOOST_CHECK_EQUAL(r1*z >= r2*z, a >= b); BOOST_CHECK_EQUAL(r1*z > r2*z, a > b); BOOST_CHECK_EQUAL(r1*z == b, a == b); BOOST_CHECK_EQUAL(r1*z != b, a != b); BOOST_CHECK_EQUAL(r1*z <= b, a <= b); BOOST_CHECK_EQUAL(r1*z < b, a < b); BOOST_CHECK_EQUAL(r1*z >= b, a >= b); BOOST_CHECK_EQUAL(r1*z > b, a > b); BOOST_CHECK_EQUAL(a == r2*z, a == b); BOOST_CHECK_EQUAL(a != r2*z, a != b); BOOST_CHECK_EQUAL(a <= r2*z, a <= b); BOOST_CHECK_EQUAL(a < r2*z, a < b); BOOST_CHECK_EQUAL(a >= r2*z, a >= b); BOOST_CHECK_EQUAL(a > r2*z, a > b); BOOST_CHECK_EQUAL(normalize_compare_result(r1.compare(r2)), cr); BOOST_CHECK_EQUAL(normalize_compare_result(r2.compare(r1)), -cr); BOOST_CHECK_EQUAL(normalize_compare_result(r1.compare(b)), cr); BOOST_CHECK_EQUAL(normalize_compare_result(r2.compare(a)), -cr); } template void test_conditional(Real v, Exp e) { // // Verify that Exp is usable in Boolean contexts, and has the same value as v: // if(e) { BOOST_CHECK(v); } else { BOOST_CHECK(!v); } if(!e) { BOOST_CHECK(!v); } else { BOOST_CHECK(v); } } template void test_complement(Real a, Real b, Real c, const boost::mpl::true_&) { int i = 1020304; int j = 56789123; int sign_mask = ~0; if(std::numeric_limits::is_signed) { BOOST_CHECK_EQUAL(~a , (~i & sign_mask)); c = a & ~b; BOOST_CHECK_EQUAL(c , (i & (~j & sign_mask))); c = ~(a | b); BOOST_CHECK_EQUAL(c , (~(i | j) & sign_mask)); } else { BOOST_CHECK_EQUAL((~a & a) , 0); } } template void test_complement(Real, Real, Real, const boost::mpl::false_&) { } template void test_integer_ops(const T&){} template void test_rational(const boost::mpl::true_&) { Real a(2); a /= 3; BOOST_CHECK_EQUAL(numerator(a) , 2); BOOST_CHECK_EQUAL(denominator(a) , 3); Real b(4); b /= 6; BOOST_CHECK_EQUAL(a , b); // // Check IO code: // std::stringstream ss; ss << a; ss >> b; BOOST_CHECK_EQUAL(a, b); } template void test_rational(const boost::mpl::false_&) { Real a(2); a /= 3; BOOST_CHECK_EQUAL(numerator(a) , 2); BOOST_CHECK_EQUAL(denominator(a) , 3); Real b(4); b /= 6; BOOST_CHECK_EQUAL(a , b); #ifndef BOOST_NO_EXCEPTIONS BOOST_CHECK_THROW(Real(a / 0), std::overflow_error); BOOST_CHECK_THROW(Real("3.14"), std::runtime_error); #endif b = Real("2/3"); BOOST_CHECK_EQUAL(a, b); // // Check IO code: // std::stringstream ss; ss << a; ss >> b; BOOST_CHECK_EQUAL(a, b); } template void test_integer_ops(const boost::mpl::int_&) { test_rational(is_boost_rational()); } template void test_signed_integer_ops(const boost::mpl::true_&) { Real a(20); Real b(7); Real c(5); BOOST_CHECK_EQUAL(-a % c , 0); BOOST_CHECK_EQUAL(-a % b , -20 % 7); BOOST_CHECK_EQUAL(-a % -b , -20 % -7); BOOST_CHECK_EQUAL(a % -b , 20 % -7); BOOST_CHECK_EQUAL(-a % 7 , -20 % 7); BOOST_CHECK_EQUAL(-a % -7 , -20 % -7); BOOST_CHECK_EQUAL(a % -7 , 20 % -7); BOOST_CHECK_EQUAL(-a % 7u , -20 % 7); BOOST_CHECK_EQUAL(-a % a , 0); BOOST_CHECK_EQUAL(-a % 5 , 0); BOOST_CHECK_EQUAL(-a % -5 , 0); BOOST_CHECK_EQUAL(a % -5 , 0); b = -b; BOOST_CHECK_EQUAL(a % b , 20 % -7); a = -a; BOOST_CHECK_EQUAL(a % b , -20 % -7); BOOST_CHECK_EQUAL(a % -7 , -20 % -7); b = 7; BOOST_CHECK_EQUAL(a % b , -20 % 7); BOOST_CHECK_EQUAL(a % 7 , -20 % 7); BOOST_CHECK_EQUAL(a % 7u , -20 % 7); a = 20; a %= b; BOOST_CHECK_EQUAL(a , 20 % 7); a = -20; a %= b; BOOST_CHECK_EQUAL(a , -20 % 7); a = 20; a %= -b; BOOST_CHECK_EQUAL(a , 20 % -7); a = -20; a %= -b; BOOST_CHECK_EQUAL(a , -20 % -7); a = 5; a %= b - a; BOOST_CHECK_EQUAL(a , 5 % (7-5)); a = -20; a %= 7; BOOST_CHECK_EQUAL(a , -20 % 7); a = 20; a %= -7; BOOST_CHECK_EQUAL(a , 20 % -7); a = -20; a %= -7; BOOST_CHECK_EQUAL(a , -20 % -7); #ifndef BOOST_NO_LONG_LONG a = -20; a %= 7uLL; BOOST_CHECK_EQUAL(a , -20 % 7); a = 20; a %= -7LL; BOOST_CHECK_EQUAL(a , 20 % -7); a = -20; a %= -7LL; BOOST_CHECK_EQUAL(a , -20 % -7); #endif a = 400; b = 45; BOOST_CHECK_EQUAL(gcd(a, -45) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(a, -45) , boost::math::lcm(400, 45)); BOOST_CHECK_EQUAL(gcd(-400, b) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(-400, b) , boost::math::lcm(400, 45)); a = -20; BOOST_CHECK_EQUAL(abs(a) , 20); BOOST_CHECK_EQUAL(abs(-a) , 20); BOOST_CHECK_EQUAL(abs(+a) , 20); a = 20; BOOST_CHECK_EQUAL(abs(a) , 20); BOOST_CHECK_EQUAL(abs(-a) , 20); BOOST_CHECK_EQUAL(abs(+a) , 20); a = -400; b = 45; BOOST_CHECK_EQUAL(gcd(a, b) , boost::math::gcd(-400, 45)); BOOST_CHECK_EQUAL(lcm(a, b) , boost::math::lcm(-400, 45)); BOOST_CHECK_EQUAL(gcd(a, 45) , boost::math::gcd(-400, 45)); BOOST_CHECK_EQUAL(lcm(a, 45) , boost::math::lcm(-400, 45)); BOOST_CHECK_EQUAL(gcd(-400, b) , boost::math::gcd(-400, 45)); BOOST_CHECK_EQUAL(lcm(-400, b) , boost::math::lcm(-400, 45)); Real r; divide_qr(a, b, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); BOOST_CHECK_EQUAL(integer_modulus(a, 57) , abs(a % 57)); b = -57; divide_qr(a, b, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); BOOST_CHECK_EQUAL(integer_modulus(a, -57) , abs(a % -57)); a = 458; divide_qr(a, b, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); BOOST_CHECK_EQUAL(integer_modulus(a, -57) , abs(a % -57)); #ifndef TEST_CHECKED_INT if(is_checked_cpp_int::value) { a = -1; #ifndef BOOST_NO_EXCEPTIONS BOOST_CHECK_THROW(a << 2, std::range_error); BOOST_CHECK_THROW(a >> 2, std::range_error); BOOST_CHECK_THROW(a <<= 2, std::range_error); BOOST_CHECK_THROW(a >>= 2, std::range_error); #endif } else { a = -1; BOOST_CHECK_EQUAL(a << 10, (boost::intmax_t(-1) << 10)); a = -23; BOOST_CHECK_EQUAL(a << 10, (boost::intmax_t(-23) << 10)); a = -23456; BOOST_CHECK_EQUAL(a >> 10, (boost::intmax_t(-23456) >> 10)); a = -3; BOOST_CHECK_EQUAL(a >> 10, (boost::intmax_t(-3) >> 10)); } #endif } template void test_signed_integer_ops(const boost::mpl::false_&) { } template void test_integer_round_trip() { if(std::numeric_limits::digits >= std::numeric_limits::digits) { Real m((std::numeric_limits::max)()); Int r = m.template convert_to(); BOOST_CHECK_EQUAL(m, r); if(std::numeric_limits::is_signed && (std::numeric_limits::digits > std::numeric_limits::digits)) { m = (std::numeric_limits::min)(); r = m.template convert_to(); BOOST_CHECK_EQUAL(m, r); } } } template void test_integer_ops(const boost::mpl::int_&) { test_signed_integer_ops(boost::mpl::bool_::is_signed>()); Real a(20); Real b(7); Real c(5); BOOST_CHECK_EQUAL(a % b , 20 % 7); BOOST_CHECK_EQUAL(a % 7 , 20 % 7); BOOST_CHECK_EQUAL(a % 7u , 20 % 7); BOOST_CHECK_EQUAL(a % a , 0); BOOST_CHECK_EQUAL(a % c , 0); BOOST_CHECK_EQUAL(a % 5 , 0); a = a % (b + 0); BOOST_CHECK_EQUAL(a , 20 % 7); a = 20; c = (a + 2) % (a - 1); BOOST_CHECK_EQUAL(c , 22 % 19); c = 5; a = b % (a - 15); BOOST_CHECK_EQUAL(a , 7 % 5); a = 20; a = 20; a %= 7; BOOST_CHECK_EQUAL(a , 20 % 7); #ifndef BOOST_NO_LONG_LONG a = 20; a %= 7uLL; BOOST_CHECK_EQUAL(a , 20 % 7); #endif a = 20; ++a; BOOST_CHECK_EQUAL(a , 21); --a; BOOST_CHECK_EQUAL(a , 20); BOOST_CHECK_EQUAL(a++ , 20); BOOST_CHECK_EQUAL(a , 21); BOOST_CHECK_EQUAL(a-- , 21); BOOST_CHECK_EQUAL(a , 20); a = 2000; a <<= 20; BOOST_CHECK_EQUAL(a , 2000L << 20); a >>= 20; BOOST_CHECK_EQUAL(a , 2000); a <<= 20u; BOOST_CHECK_EQUAL(a , 2000L << 20); a >>= 20u; BOOST_CHECK_EQUAL(a , 2000); #ifndef BOOST_NO_EXCEPTIONS BOOST_CHECK_THROW(a <<= -20, std::out_of_range); BOOST_CHECK_THROW(a >>= -20, std::out_of_range); BOOST_CHECK_THROW(Real(a << -20), std::out_of_range); BOOST_CHECK_THROW(Real(a >> -20), std::out_of_range); #endif #ifndef BOOST_NO_LONG_LONG if(sizeof(long long) > sizeof(std::size_t)) { // extreme values should trigger an exception: #ifndef BOOST_NO_EXCEPTIONS BOOST_CHECK_THROW(a >>= (1uLL << (sizeof(long long) * CHAR_BIT - 2)), std::out_of_range); BOOST_CHECK_THROW(a <<= (1uLL << (sizeof(long long) * CHAR_BIT - 2)), std::out_of_range); BOOST_CHECK_THROW(a >>= -(1LL << (sizeof(long long) * CHAR_BIT - 2)), std::out_of_range); BOOST_CHECK_THROW(a <<= -(1LL << (sizeof(long long) * CHAR_BIT - 2)), std::out_of_range); BOOST_CHECK_THROW(a >>= (1LL << (sizeof(long long) * CHAR_BIT - 2)), std::out_of_range); BOOST_CHECK_THROW(a <<= (1LL << (sizeof(long long) * CHAR_BIT - 2)), std::out_of_range); #endif // Unless they fit within range: a = 2000L; a <<= 20uLL; BOOST_CHECK_EQUAL(a, (2000L << 20)); a = 2000; a <<= 20LL; BOOST_CHECK_EQUAL(a, (2000L << 20)); #ifndef BOOST_NO_EXCEPTIONS BOOST_CHECK_THROW(Real(a >> (1uLL << (sizeof(long long) * CHAR_BIT - 2))), std::out_of_range); BOOST_CHECK_THROW(Real(a <<= (1uLL << (sizeof(long long) * CHAR_BIT - 2))), std::out_of_range); BOOST_CHECK_THROW(Real(a >>= -(1LL << (sizeof(long long) * CHAR_BIT - 2))), std::out_of_range); BOOST_CHECK_THROW(Real(a <<= -(1LL << (sizeof(long long) * CHAR_BIT - 2))), std::out_of_range); BOOST_CHECK_THROW(Real(a >>= (1LL << (sizeof(long long) * CHAR_BIT - 2))), std::out_of_range); BOOST_CHECK_THROW(Real(a <<= (1LL << (sizeof(long long) * CHAR_BIT - 2))), std::out_of_range); #endif // Unless they fit within range: a = 2000L; BOOST_CHECK_EQUAL(Real(a << 20uLL) , (2000L << 20)); a = 2000; BOOST_CHECK_EQUAL(Real(a << 20LL) , (2000L << 20)); } #endif a = 20; b = a << 20; BOOST_CHECK_EQUAL(b , (20 << 20)); b = a >> 2; BOOST_CHECK_EQUAL(b , (20 >> 2)); b = (a + 2) << 10; BOOST_CHECK_EQUAL(b , (22 << 10)); b = (a + 3) >> 3; BOOST_CHECK_EQUAL(b , (23 >> 3)); // // Bit fiddling: // int i = 1020304; int j = 56789123; int k = 4523187; a = i; b = j; c = a; c &= b; BOOST_CHECK_EQUAL(c , (i & j)); c = a; c &= j; BOOST_CHECK_EQUAL(c , (i & j)); c = a; c &= a + b; BOOST_CHECK_EQUAL(c , (i & (i + j))); BOOST_CHECK_EQUAL((a & b) , (i & j)); c = k; a = a & (b + k); BOOST_CHECK_EQUAL(a , (i & (j + k))); a = i; a = (b + k) & a; BOOST_CHECK_EQUAL(a , (i & (j + k))); a = i; c = a & b & k; BOOST_CHECK_EQUAL(c , (i&j&k)); c = a; c &= (c+b); BOOST_CHECK_EQUAL(c , (i & (i+j))); c = a & (b | 1); BOOST_CHECK_EQUAL(c , (i & (j | 1))); test_complement(a, b, c, typename is_twos_complement_integer::type()); a = i; b = j; c = a; c |= b; BOOST_CHECK_EQUAL(c , (i | j)); c = a; c |= j; BOOST_CHECK_EQUAL(c , (i | j)); c = a; c |= a + b; BOOST_CHECK_EQUAL(c , (i | (i + j))); BOOST_CHECK_EQUAL((a | b) , (i | j)); c = k; a = a | (b + k); BOOST_CHECK_EQUAL(a , (i | (j + k))); a = i; a = (b + k) | a; BOOST_CHECK_EQUAL(a , (i | (j + k))); a = i; c = a | b | k; BOOST_CHECK_EQUAL(c , (i|j|k)); c = a; c |= (c + b); BOOST_CHECK_EQUAL(c , (i | (i+j))); c = a | (b | 1); BOOST_CHECK_EQUAL(c , (i | (j | 1))); a = i; b = j; c = a; c ^= b; BOOST_CHECK_EQUAL(c , (i ^ j)); c = a; c ^= j; BOOST_CHECK_EQUAL(c , (i ^ j)); c = a; c ^= a + b; BOOST_CHECK_EQUAL(c , (i ^ (i + j))); BOOST_CHECK_EQUAL((a ^ b) , (i ^ j)); c = k; a = a ^ (b + k); BOOST_CHECK_EQUAL(a , (i ^ (j + k))); a = i; a = (b + k) ^ a; BOOST_CHECK_EQUAL(a , (i ^ (j + k))); a = i; c = a ^ b ^ k; BOOST_CHECK_EQUAL(c , (i^j^k)); c = a; c ^= (c + b); BOOST_CHECK_EQUAL(c , (i ^ (i+j))); c = a ^ (b | 1); BOOST_CHECK_EQUAL(c , (i ^ (j | 1))); a = i; b = j; c = k; // // Non-member functions: // a = 400; b = 45; BOOST_CHECK_EQUAL(gcd(a, b) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(a, b) , boost::math::lcm(400, 45)); BOOST_CHECK_EQUAL(gcd(a, 45) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(a, 45) , boost::math::lcm(400, 45)); BOOST_CHECK_EQUAL(gcd(a, 45u) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(a, 45u) , boost::math::lcm(400, 45)); BOOST_CHECK_EQUAL(gcd(400, b) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(400, b) , boost::math::lcm(400, 45)); BOOST_CHECK_EQUAL(gcd(400u, b) , boost::math::gcd(400, 45)); BOOST_CHECK_EQUAL(lcm(400u, b) , boost::math::lcm(400, 45)); // // Conditionals involving 2 arg functions: // test_conditional(Real(gcd(a, b)), gcd(a, b)); Real r; divide_qr(a, b, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); divide_qr(a + 0, b, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); divide_qr(a, b+0, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); divide_qr(a+0, b+0, c, r); BOOST_CHECK_EQUAL(c , a / b); BOOST_CHECK_EQUAL(r , a % b); BOOST_CHECK_EQUAL(integer_modulus(a, 57) , a % 57); for(i = 0; i < 20; ++i) { if(std::numeric_limits::is_specialized && (!std::numeric_limits::is_bounded || ((int)i * 17 < std::numeric_limits::digits))) { BOOST_CHECK_EQUAL(lsb(Real(1) << (i * 17)), static_cast(i * 17)); BOOST_CHECK_EQUAL(msb(Real(1) << (i * 17)), static_cast(i * 17)); BOOST_CHECK(bit_test(Real(1) << (i * 17), i * 17)); BOOST_CHECK(!bit_test(Real(1) << (i * 17), i * 17 + 1)); if(i) { BOOST_CHECK(!bit_test(Real(1) << (i * 17), i * 17 - 1)); } Real zero(0); BOOST_CHECK(bit_test(bit_set(zero, i * 17), i * 17)); zero = 0; BOOST_CHECK_EQUAL(bit_flip(zero, i*17) , Real(1) << i * 17); zero = Real(1) << i * 17; BOOST_CHECK_EQUAL(bit_flip(zero, i * 17) , 0); zero = Real(1) << i * 17; BOOST_CHECK_EQUAL(bit_unset(zero, i * 17) , 0); } } // // pow, powm: // BOOST_CHECK_EQUAL(pow(Real(3), 4u) , 81); BOOST_CHECK_EQUAL(pow(Real(3) + Real(0), 4u) , 81); BOOST_CHECK_EQUAL(powm(Real(3), Real(4), Real(13)) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), Real(4), 13) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), Real(4), Real(13) + 0) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), Real(4) + 0, Real(13)) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), Real(4) + 0, 13) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), Real(4) + 0, Real(13) + 0) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), 4 + 0, Real(13)) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), 4 + 0, 13) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3), 4 + 0, Real(13) + 0) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, Real(4), Real(13)) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, Real(4), 13) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, Real(4), Real(13) + 0) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, Real(4) + 0, Real(13)) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, Real(4) + 0, 13) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, Real(4) + 0, Real(13) + 0) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, 4 + 0, Real(13)) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, 4 + 0, 13) , 81 % 13); BOOST_CHECK_EQUAL(powm(Real(3) + 0, 4 + 0, Real(13) + 0) , 81 % 13); // // Conditionals involving 3 arg functions: // test_conditional(Real(powm(Real(3), Real(4), Real(13))), powm(Real(3), Real(4), Real(13))); #ifndef BOOST_NO_EXCEPTIONS // // Things that are expected errors: // BOOST_CHECK_THROW(Real("3.14"), std::runtime_error); BOOST_CHECK_THROW(Real("3L"), std::runtime_error); BOOST_CHECK_THROW(Real(Real(20) / 0u), std::overflow_error); #endif // // Extra tests added for full coverage: // a = 20; b = 7; c = 20 % b; BOOST_CHECK_EQUAL(c , (20 % 7)); c = 20 % (b + 0); BOOST_CHECK_EQUAL(c , (20 % 7)); c = a & 10; BOOST_CHECK_EQUAL(c , (20 & 10)); c = 10 & a; BOOST_CHECK_EQUAL(c , (20 & 10)); c = (a + 0) & (b + 0); BOOST_CHECK_EQUAL(c , (20 & 7)); c = 10 & (a + 0); BOOST_CHECK_EQUAL(c , (20 & 10)); c = 10 | a; BOOST_CHECK_EQUAL(c , (20 | 10)); c = (a + 0) | (b + 0); BOOST_CHECK(c == (20 | 7)) c = 20 | (b + 0); BOOST_CHECK_EQUAL(c , (20 | 7)); c = a ^ 7; BOOST_CHECK_EQUAL(c , (20 ^ 7)); c = 20 ^ b; BOOST_CHECK_EQUAL(c , (20 ^ 7)); c = (a + 0) ^ (b + 0); BOOST_CHECK_EQUAL(c , (20 ^ 7)); c = 20 ^ (b + 0); BOOST_CHECK_EQUAL(c , (20 ^ 7)); // // Round tripping of built in integers: // test_integer_round_trip(); test_integer_round_trip(); test_integer_round_trip(); test_integer_round_trip(); test_integer_round_trip(); test_integer_round_trip(); #ifndef BOOST_NO_CXX11_LONG_LONG test_integer_round_trip(); test_integer_round_trip(); #endif } template void test_float_funcs(const T&){} template void test_float_funcs(const boost::mpl::true_&) { if(boost::multiprecision::is_interval_number::value) return; // // Test variable reuse in function calls, see https://svn.boost.org/trac/boost/ticket/8326 // Real a(2), b(10), c, d; a = pow(a, b); BOOST_CHECK_EQUAL(a, 1024); a = 2; b = pow(a, b); BOOST_CHECK_EQUAL(b, 1024); b = 10; a = pow(a, 10); BOOST_CHECK_EQUAL(a, 1024); a = -2; a = abs(a); BOOST_CHECK_EQUAL(a, 2); a = -2; a = fabs(a); BOOST_CHECK_EQUAL(a, 2); a = 2.5; a = floor(a); BOOST_CHECK_EQUAL(a, 2); a = 2.5; a = ceil(a); BOOST_CHECK_EQUAL(a, 3); a = 2.5; a = trunc(a); BOOST_CHECK_EQUAL(a, 2); a = 2.25; a = round(a); BOOST_CHECK_EQUAL(a, 2); a = 2; a = ldexp(a, 1); BOOST_CHECK_EQUAL(a, 4); int i; a = frexp(a, &i); BOOST_CHECK_EQUAL(a, 0.5); Real tol = std::numeric_limits::epsilon() * 3; a = 4; a = sqrt(a); BOOST_CHECK_CLOSE_FRACTION(a, 2, tol); a = 3; a = exp(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(exp(Real(3))), tol); a = 3; a = log(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(log(Real(3))), tol); a = 3; a = log10(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(log10(Real(3))), tol); a = 0.5; a = sin(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(sin(Real(0.5))), tol); a = 0.5; a = cos(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(cos(Real(0.5))), tol); a = 0.5; a = tan(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(tan(Real(0.5))), tol); a = 0.5; a = asin(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(asin(Real(0.5))), tol); a = 0.5; a = acos(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(acos(Real(0.5))), tol); a = 0.5; a = atan(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(atan(Real(0.5))), tol); a = 0.5; a = sinh(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(sinh(Real(0.5))), tol); a = 0.5; a = cosh(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(cosh(Real(0.5))), tol); a = 0.5; a = tanh(a); BOOST_CHECK_CLOSE_FRACTION(a, Real(tanh(Real(0.5))), tol); // fmod, need to check all the sign permutations: a = 4; b = 2; a = fmod(a, b); BOOST_CHECK_CLOSE_FRACTION(a, Real(fmod(Real(4), Real(2))), tol); a = 4; b = fmod(a, b); BOOST_CHECK_CLOSE_FRACTION(b, Real(fmod(Real(4), Real(2))), tol); a = 4; b = 2; a = fmod(-a, b); BOOST_CHECK_CLOSE_FRACTION(a, Real(fmod(-Real(4), Real(2))), tol); a = 4; b = fmod(-a, b); BOOST_CHECK_CLOSE_FRACTION(b, Real(-fmod(Real(4), Real(2))), tol); a = 4; b = 2; a = fmod(a, -b); BOOST_CHECK_CLOSE_FRACTION(a, Real(fmod(Real(4), -Real(2))), tol); a = 4; b = fmod(a, -b); BOOST_CHECK_CLOSE_FRACTION(b, Real(fmod(Real(4), -Real(2))), tol); a = 4; b = 2; a = fmod(-a, -b); BOOST_CHECK_CLOSE_FRACTION(a, Real(fmod(-Real(4), -Real(2))), tol); a = 4; b = fmod(-a, -b); BOOST_CHECK_CLOSE_FRACTION(b, Real(fmod(-Real(4), -Real(2))), tol); // modf: a = 5; a /= 2; b = modf(a, &c); BOOST_CHECK_EQUAL(b + c, a); BOOST_CHECK_EQUAL(b > 0, a > 0); BOOST_CHECK_EQUAL(c > 0, a > 0); a = -a; b = modf(a, &c); BOOST_CHECK_EQUAL(b + c, a); BOOST_CHECK_EQUAL(b > 0, a > 0); BOOST_CHECK_EQUAL(c > 0, a > 0); b = modf(a, &c); c = 0; modf(a, &c); BOOST_CHECK_EQUAL(b + c, a); BOOST_CHECK_EQUAL(b > 0, a > 0); BOOST_CHECK_EQUAL(c > 0, a > 0); a = -a; b = modf(a, &c); c = 0; modf(a, &c); BOOST_CHECK_EQUAL(b + c, a); BOOST_CHECK_EQUAL(b > 0, a > 0); BOOST_CHECK_EQUAL(c > 0, a > 0); if(std::numeric_limits::has_infinity) { a = std::numeric_limits::infinity(); b = modf(a, &c); BOOST_CHECK_EQUAL(a, c); BOOST_CHECK_EQUAL(b, 0); a = -std::numeric_limits::infinity(); b = modf(a, &c); BOOST_CHECK_EQUAL(a, c); BOOST_CHECK_EQUAL(b, 0); } if(std::numeric_limits::has_quiet_NaN) { a = std::numeric_limits::quiet_NaN(); b = modf(a, &c); BOOST_CHECK((boost::math::isnan)(b)); BOOST_CHECK((boost::math::isnan)(c)); } a = 4; b = 2; a = atan2(a, b); BOOST_CHECK_CLOSE_FRACTION(a, Real(atan2(Real(4), Real(2))), tol); a = 4; b = atan2(a, b); BOOST_CHECK_CLOSE_FRACTION(b, Real(atan2(Real(4), Real(2))), tol); // fma: a = 2; b = 4; c = 6; BOOST_CHECK_EQUAL(fma(a, b, c), 14); BOOST_CHECK_EQUAL(fma(a, 4, c), 14); BOOST_CHECK_EQUAL(fma(a, b, 6), 14); BOOST_CHECK_EQUAL(fma(a, 4, 6), 14); BOOST_CHECK_EQUAL(fma(a + 0, b, c), 14); BOOST_CHECK_EQUAL(fma(a - 0, 4, c), 14); BOOST_CHECK_EQUAL(fma(a * 1, b, 6), 14); BOOST_CHECK_EQUAL(fma(a / 1, 4, 6), 14); BOOST_CHECK_EQUAL(fma(2, b, c), 14); BOOST_CHECK_EQUAL(fma(2, b, 6), 14); BOOST_CHECK_EQUAL(fma(2, b * 1, c), 14); BOOST_CHECK_EQUAL(fma(2, b + 0, 6), 14); BOOST_CHECK_EQUAL(fma(2, 4, c), 14); BOOST_CHECK_EQUAL(fma(2, 4, c + 0), 14); // Default construct, for consistency with native floats, default constructed values are zero: Real zero; BOOST_CHECK_EQUAL(zero, 0); } template void compare_NaNs(const T& a, const U& b) { BOOST_CHECK_EQUAL(a == b, false); BOOST_CHECK_EQUAL(a != b, true); BOOST_CHECK_EQUAL(a <= b, false); BOOST_CHECK_EQUAL(a >= b, false); BOOST_CHECK_EQUAL(a > b, false); BOOST_CHECK_EQUAL(a < b, false); // // Again where LHS may be an expression template: // BOOST_CHECK_EQUAL(1 * a == b, false); BOOST_CHECK_EQUAL(1 * a != b, true); BOOST_CHECK_EQUAL(1 * a <= b, false); BOOST_CHECK_EQUAL(1 * a >= b, false); BOOST_CHECK_EQUAL(1 * a > b, false); BOOST_CHECK_EQUAL(1 * a < b, false); // // Again where RHS may be an expression template: // BOOST_CHECK_EQUAL(a == b * 1, false); BOOST_CHECK_EQUAL(a != b * 1, true); BOOST_CHECK_EQUAL(a <= b * 1, false); BOOST_CHECK_EQUAL(a >= b * 1, false); BOOST_CHECK_EQUAL(a > b * 1, false); BOOST_CHECK_EQUAL(a < b * 1, false); // // Again where LHS and RHS may be an expression templates: // BOOST_CHECK_EQUAL(1 * a == b * 1, false); BOOST_CHECK_EQUAL(1 * a != b * 1, true); BOOST_CHECK_EQUAL(1 * a <= b * 1, false); BOOST_CHECK_EQUAL(1 * a >= b * 1, false); BOOST_CHECK_EQUAL(1 * a > b * 1, false); BOOST_CHECK_EQUAL(1 * a < b * 1, false); } template void test_float_ops(const T&){} template void test_float_ops(const boost::mpl::int_&) { BOOST_CHECK_EQUAL(abs(Real(2)) , 2); BOOST_CHECK_EQUAL(abs(Real(-2)) , 2); BOOST_CHECK_EQUAL(fabs(Real(2)) , 2); BOOST_CHECK_EQUAL(fabs(Real(-2)) , 2); BOOST_CHECK_EQUAL(floor(Real(5) / 2) , 2); BOOST_CHECK_EQUAL(ceil(Real(5) / 2) , 3); BOOST_CHECK_EQUAL(floor(Real(-5) / 2) , -3); BOOST_CHECK_EQUAL(ceil(Real(-5) / 2) , -2); BOOST_CHECK_EQUAL(trunc(Real(5) / 2) , 2); BOOST_CHECK_EQUAL(trunc(Real(-5) / 2) , -2); // // ldexp and frexp, these pretty much have to be implemented by each backend: // typedef typename Real::backend_type::exponent_type e_type; BOOST_CHECK_EQUAL(ldexp(Real(2), 5) , 64); BOOST_CHECK_EQUAL(ldexp(Real(2), -5) , Real(2) / 32); Real v(512); e_type exponent; Real r = frexp(v, &exponent); BOOST_CHECK_EQUAL(r , 0.5); BOOST_CHECK_EQUAL(exponent , 10); BOOST_CHECK_EQUAL(v , 512); v = 1 / v; r = frexp(v, &exponent); BOOST_CHECK_EQUAL(r , 0.5); BOOST_CHECK_EQUAL(exponent , -8); BOOST_CHECK_EQUAL(ldexp(Real(2), e_type(5)) , 64); BOOST_CHECK_EQUAL(ldexp(Real(2), e_type(-5)) , Real(2) / 32); v = 512; e_type exp2; r = frexp(v, &exp2); BOOST_CHECK_EQUAL(r , 0.5); BOOST_CHECK_EQUAL(exp2 , 10); BOOST_CHECK_EQUAL(v , 512); v = 1 / v; r = frexp(v, &exp2); BOOST_CHECK_EQUAL(r , 0.5); BOOST_CHECK_EQUAL(exp2 , -8); // // scalbn and logb, these are the same as ldexp and frexp unless the radix is // something other than 2: // if(std::numeric_limits::is_specialized && std::numeric_limits::radix) { BOOST_CHECK_EQUAL(scalbn(Real(2), 5), 2 * pow(double(std::numeric_limits::radix), 5)); BOOST_CHECK_EQUAL(scalbn(Real(2), -5), Real(2) / pow(double(std::numeric_limits::radix), 5)); v = 512; exponent = ilogb(v); r = scalbn(v, -exponent); BOOST_CHECK(r >= 1); BOOST_CHECK(r < std::numeric_limits::radix); BOOST_CHECK_EQUAL(exponent, logb(v)); BOOST_CHECK_EQUAL(v, scalbn(r, exponent)); v = 1 / v; exponent = ilogb(v); r = scalbn(v, -exponent); BOOST_CHECK(r >= 1); BOOST_CHECK(r < std::numeric_limits::radix); BOOST_CHECK_EQUAL(exponent, logb(v)); BOOST_CHECK_EQUAL(v, scalbn(r, exponent)); } // // pow and exponent: // v = 3.25; r = pow(v, 0); BOOST_CHECK_EQUAL(r , 1); r = pow(v, 1); BOOST_CHECK_EQUAL(r , 3.25); r = pow(v, 2); BOOST_CHECK_EQUAL(r , boost::math::pow<2>(3.25)); r = pow(v, 3); BOOST_CHECK_EQUAL(r , boost::math::pow<3>(3.25)); r = pow(v, 4); BOOST_CHECK_EQUAL(r , boost::math::pow<4>(3.25)); r = pow(v, 5); BOOST_CHECK_EQUAL(r , boost::math::pow<5>(3.25)); r = pow(v, 6); BOOST_CHECK_EQUAL(r , boost::math::pow<6>(3.25)); r = pow(v, 25); BOOST_CHECK_EQUAL(r , boost::math::pow<25>(Real(3.25))); #ifndef BOOST_NO_EXCEPTIONS // // Things that are expected errors: // BOOST_CHECK_THROW(Real("3.14L"), std::runtime_error); if(std::numeric_limits::is_specialized) { if(std::numeric_limits::has_infinity) { BOOST_CHECK((boost::math::isinf)(Real(20) / 0u)); } else { BOOST_CHECK_THROW(Real(Real(20) / 0u), std::overflow_error); } } #endif // // Comparisons of NaN's should always fail: // if(std::numeric_limits::has_quiet_NaN) { r = v = std::numeric_limits::quiet_NaN(); compare_NaNs(r, v); v = 0; compare_NaNs(r, v); r.swap(v); compare_NaNs(r, v); // // Conmpare NaN to int: // compare_NaNs(v, 0); compare_NaNs(0, v); // // Compare to floats: // compare_NaNs(v, 0.5); compare_NaNs(0.5, v); if(std::numeric_limits::has_quiet_NaN) { compare_NaNs(r, std::numeric_limits::quiet_NaN()); compare_NaNs(std::numeric_limits::quiet_NaN(), r); } } // // Operations involving NaN's as one argument: // if(std::numeric_limits::has_quiet_NaN) { v = 20.25; r = std::numeric_limits::quiet_NaN(); BOOST_CHECK((boost::math::isnan)(v + r)); BOOST_CHECK((boost::math::isnan)(r + v)); BOOST_CHECK((boost::math::isnan)(r - v)); BOOST_CHECK((boost::math::isnan)(v - r)); BOOST_CHECK((boost::math::isnan)(r * v)); BOOST_CHECK((boost::math::isnan)(v * r)); BOOST_CHECK((boost::math::isnan)(r / v)); BOOST_CHECK((boost::math::isnan)(v / r)); Real t = v; BOOST_CHECK((boost::math::isnan)(t += r)); t = r; BOOST_CHECK((boost::math::isnan)(t += v)); t = r; BOOST_CHECK((boost::math::isnan)(t -= v)); t = v; BOOST_CHECK((boost::math::isnan)(t -= r)); t = r; BOOST_CHECK((boost::math::isnan)(t *= v)); t = v; BOOST_CHECK((boost::math::isnan)(t *= r)); t = r; BOOST_CHECK((boost::math::isnan)(t /= v)); t = v; BOOST_CHECK((boost::math::isnan)(t /= r)); } // // Operations involving infinities as one argument: // if(std::numeric_limits::has_infinity) { v = 20.25; r = std::numeric_limits::infinity(); BOOST_CHECK((boost::math::isinf)(v + r)); BOOST_CHECK((boost::math::isinf)(r + v)); BOOST_CHECK((boost::math::isinf)(r - v)); BOOST_CHECK((boost::math::isinf)(v - r)); BOOST_CHECK_LT(v - r, 0); BOOST_CHECK((boost::math::isinf)(r * v)); BOOST_CHECK((boost::math::isinf)(v * r)); BOOST_CHECK((boost::math::isinf)(r / v)); BOOST_CHECK_EQUAL(v / r, 0); Real t = v; BOOST_CHECK((boost::math::isinf)(t += r)); t = r; BOOST_CHECK((boost::math::isinf)(t += v)); t = r; BOOST_CHECK((boost::math::isinf)(t -= v)); t = v; BOOST_CHECK((boost::math::isinf)(t -= r)); t = v; BOOST_CHECK(t -= r < 0); t = r; BOOST_CHECK((boost::math::isinf)(t *= v)); t = v; BOOST_CHECK((boost::math::isinf)(t *= r)); t = r; BOOST_CHECK((boost::math::isinf)(t /= v)); t = v; BOOST_CHECK((t /= r) == 0); } // // Operations that should produce NaN as a result: // if(std::numeric_limits::has_quiet_NaN) { v = r = 0; Real t = v / r; BOOST_CHECK((boost::math::isnan)(t)); v /= r; BOOST_CHECK((boost::math::isnan)(v)); t = v / 0; BOOST_CHECK((boost::math::isnan)(v)); if(std::numeric_limits::has_infinity) { v = 0; r = std::numeric_limits::infinity(); t = v * r; if(!boost::multiprecision::is_interval_number::value) { BOOST_CHECK((boost::math::isnan)(t)); t = r * 0; BOOST_CHECK((boost::math::isnan)(t)); } v = r; t = r / v; BOOST_CHECK((boost::math::isnan)(t)); } } test_float_funcs(boost::mpl::bool_::is_specialized>()); } template struct lexical_cast_target_type { typedef typename boost::mpl::if_< boost::is_signed, boost::intmax_t, typename boost::mpl::if_< boost::is_unsigned, boost::uintmax_t, T >::type >::type type; }; template void test_negative_mixed_minmax(boost::mpl::true_ const&) { if(!std::numeric_limits::is_bounded || (std::numeric_limits::digits >= std::numeric_limits::digits)) { Real mx1((std::numeric_limits::max)() - 1); ++mx1; Real mx2((std::numeric_limits::max)()); BOOST_CHECK_EQUAL(mx1, mx2); mx1 = (std::numeric_limits::max)() - 1; ++mx1; mx2 = (std::numeric_limits::max)(); BOOST_CHECK_EQUAL(mx1, mx2); if(!std::numeric_limits::is_bounded || (std::numeric_limits::digits > std::numeric_limits::digits)) { Real mx3((std::numeric_limits::min)() + 1); --mx3; Real mx4((std::numeric_limits::min)()); BOOST_CHECK_EQUAL(mx3, mx4); mx3 = (std::numeric_limits::min)() + 1; --mx3; mx4 = (std::numeric_limits::min)(); BOOST_CHECK_EQUAL(mx3, mx4); } } } template void test_negative_mixed_minmax(boost::mpl::false_ const&) { } template void test_negative_mixed(boost::mpl::true_ const&) { typedef typename lexical_cast_target_type::type target_type; typedef typename boost::mpl::if_< boost::is_convertible, typename boost::mpl::if_c::value && (sizeof(Num) < sizeof(int)), int, Num>::type, Real >::type cast_type; typedef typename boost::mpl::if_< boost::is_convertible, Num, Real >::type simple_cast_type; std::cout << "Testing mixed arithmetic with type: " << typeid(Real).name() << " and " << typeid(Num).name() << std::endl; static const int left_shift = std::numeric_limits::digits - 1; Num n1 = -static_cast(1uLL << ((left_shift < 63) && (left_shift > 0) ? left_shift : 10)); Num n2 = -1; Num n3 = 0; Num n4 = -20; Num n5 = -8; test_comparisons(n1, n2, boost::is_convertible()); test_comparisons(n1, n3, boost::is_convertible()); test_comparisons(n3, n1, boost::is_convertible()); test_comparisons(n2, n1, boost::is_convertible()); test_comparisons(n1, n1, boost::is_convertible()); test_comparisons(n3, n3, boost::is_convertible()); // Default construct: BOOST_CHECK_EQUAL(Real(n1) , static_cast(n1)); BOOST_CHECK_EQUAL(Real(n2) , static_cast(n2)); BOOST_CHECK_EQUAL(Real(n3) , static_cast(n3)); BOOST_CHECK_EQUAL(Real(n4) , static_cast(n4)); BOOST_CHECK_EQUAL(static_cast(n1) , Real(n1)); BOOST_CHECK_EQUAL(static_cast(n2) , Real(n2)); BOOST_CHECK_EQUAL(static_cast(n3) , Real(n3)); BOOST_CHECK_EQUAL(static_cast(n4) , Real(n4)); BOOST_CHECK_EQUAL(Real(n1).template convert_to() , n1); BOOST_CHECK_EQUAL(Real(n2).template convert_to() , n2); BOOST_CHECK_EQUAL(Real(n3).template convert_to() , n3); BOOST_CHECK_EQUAL(Real(n4).template convert_to() , n4); #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS BOOST_CHECK_EQUAL(static_cast(Real(n1)) , n1); BOOST_CHECK_EQUAL(static_cast(Real(n2)) , n2); BOOST_CHECK_EQUAL(static_cast(Real(n3)) , n3); BOOST_CHECK_EQUAL(static_cast(Real(n4)) , n4); #endif // Conversions when source is an expression template: BOOST_CHECK_EQUAL((Real(n1) + 0).template convert_to() , n1); BOOST_CHECK_EQUAL((Real(n2) + 0).template convert_to(), n2); BOOST_CHECK_EQUAL((Real(n3) + 0).template convert_to(), n3); BOOST_CHECK_EQUAL((Real(n4) + 0).template convert_to(), n4); #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS BOOST_CHECK_EQUAL(static_cast((Real(n1) + 0)), n1); BOOST_CHECK_EQUAL(static_cast((Real(n2) + 0)), n2); BOOST_CHECK_EQUAL(static_cast((Real(n3) + 0)), n3); BOOST_CHECK_EQUAL(static_cast((Real(n4) + 0)), n4); #endif #if defined(TEST_MPFR) Num tol = 10 * std::numeric_limits::epsilon(); #else Num tol = 0; #endif std::ios_base::fmtflags f = boost::is_floating_point::value ? std::ios_base::scientific : std::ios_base::fmtflags(0); int digits_to_print = boost::is_floating_point::value && std::numeric_limits::is_specialized ? std::numeric_limits::digits10 + 5 : 0; if(std::numeric_limits::digits <= std::numeric_limits::digits) { BOOST_CHECK_CLOSE(n1, checked_lexical_cast(Real(n1).str(digits_to_print, f)), tol); } BOOST_CHECK_CLOSE(n2, checked_lexical_cast(Real(n2).str(digits_to_print, f)), 0); BOOST_CHECK_CLOSE(n3, checked_lexical_cast(Real(n3).str(digits_to_print, f)), 0); BOOST_CHECK_CLOSE(n4, checked_lexical_cast(Real(n4).str(digits_to_print, f)), 0); // Assignment: Real r(0); BOOST_CHECK(r != static_cast(n1)); r = static_cast(n1); BOOST_CHECK_EQUAL(r , static_cast(n1)); r = static_cast(n2); BOOST_CHECK_EQUAL(r , static_cast(n2)); r = static_cast(n3); BOOST_CHECK_EQUAL(r , static_cast(n3)); r = static_cast(n4); BOOST_CHECK_EQUAL(r , static_cast(n4)); // Addition: r = static_cast(n2); BOOST_CHECK_EQUAL(r + static_cast(n4) , static_cast(n2 + n4)); BOOST_CHECK_EQUAL(Real(r + static_cast(n4)) , static_cast(n2 + n4)); r += static_cast(n4); BOOST_CHECK_EQUAL(r , static_cast(n2 + n4)); // subtraction: r = static_cast(n4); BOOST_CHECK_EQUAL(r - static_cast(n5) , static_cast(n4 - n5)); BOOST_CHECK_EQUAL(Real(r - static_cast(n5)) , static_cast(n4 - n5)); r -= static_cast(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 - n5)); // Multiplication: r = static_cast(n2); BOOST_CHECK_EQUAL(r * static_cast(n4) , static_cast(n2 * n4)); BOOST_CHECK_EQUAL(Real(r * static_cast(n4)) , static_cast(n2 * n4)); r *= static_cast(n4); BOOST_CHECK_EQUAL(r , static_cast(n2 * n4)); // Division: r = static_cast(n1); BOOST_CHECK_EQUAL(r / static_cast(n5) , static_cast(n1 / n5)); BOOST_CHECK_EQUAL(Real(r / static_cast(n5)) , static_cast(n1 / n5)); r /= static_cast(n5); BOOST_CHECK_EQUAL(r , static_cast(n1 / n5)); // // Extra cases for full coverage: // r = Real(n4) + static_cast(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 + n5)); r = static_cast(n4) + Real(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 + n5)); r = Real(n4) - static_cast(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 - n5)); r = static_cast(n4) - Real(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 - n5)); r = static_cast(n4) * Real(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 * n5)); r = static_cast(4 * n4) / Real(4); BOOST_CHECK_EQUAL(r , static_cast(n4)); Real a, b, c; a = 20; b = 30; c = -a + b; BOOST_CHECK_EQUAL(c , 10); c = b + -a; BOOST_CHECK_EQUAL(c , 10); n4 = 30; c = -a + static_cast(n4); BOOST_CHECK_EQUAL(c , 10); c = static_cast(n4) + -a; BOOST_CHECK_EQUAL(c , 10); c = -a + -b; BOOST_CHECK_EQUAL(c , -50); n4 = 4; c = -(a + b) + static_cast(n4); BOOST_CHECK_EQUAL(c , -50+4); n4 = 50; c = (a + b) - static_cast(n4); BOOST_CHECK_EQUAL(c , 0); c = (a + b) - static_cast(n4); BOOST_CHECK_EQUAL(c , 0); c = a - -(b + static_cast(n4)); BOOST_CHECK_EQUAL(c , 20 - -(30 + 50)); c = -(b + static_cast(n4)) - a; BOOST_CHECK_EQUAL(c , -(30 + 50) - 20); c = a - -b; BOOST_CHECK_EQUAL(c , 50); c = -a - b; BOOST_CHECK_EQUAL(c , -50); c = -a - static_cast(n4); BOOST_CHECK_EQUAL(c , -20 - 50); c = static_cast(n4) - -a; BOOST_CHECK_EQUAL(c , 50 + 20); c = -(a + b) - Real(n4); BOOST_CHECK_EQUAL(c , -(20 + 30) - 50); c = static_cast(n4) - (a + b); BOOST_CHECK_EQUAL(c , 0); c = (a + b) * static_cast(n4); BOOST_CHECK_EQUAL(c , 50 * 50); c = static_cast(n4) * (a + b); BOOST_CHECK_EQUAL(c , 50 * 50); c = a * -(b + static_cast(n4)); BOOST_CHECK_EQUAL(c , 20 * -(30 + 50)); c = -(b + static_cast(n4)) * a; BOOST_CHECK_EQUAL(c , 20 * -(30 + 50)); c = a * -b; BOOST_CHECK_EQUAL(c , 20 * -30); c = -a * b; BOOST_CHECK_EQUAL(c , 20 * -30); c = -a * static_cast(n4); BOOST_CHECK_EQUAL(c , -20 * 50); c = static_cast(n4) * -a; BOOST_CHECK_EQUAL(c , -20 * 50); c = -(a + b) + a; BOOST_CHECK(-50 + 20); c = static_cast(n4) - (a + b); BOOST_CHECK_EQUAL(c , 0); Real d = 10; c = (a + b) / d; BOOST_CHECK_EQUAL(c , 5); c = (a + b) / (d + 0); BOOST_CHECK_EQUAL(c , 5); c = (a + b) / static_cast(n4); BOOST_CHECK_EQUAL(c , 1); c = static_cast(n4) / (a + b); BOOST_CHECK_EQUAL(c , 1); d = 50; c = d / -(a + b); BOOST_CHECK_EQUAL(c , -1); c = -(a + b) / d; BOOST_CHECK_EQUAL(c , -1); d = 2; c = a / -d; BOOST_CHECK_EQUAL(c , 20 / -2); c = -a / d; BOOST_CHECK_EQUAL(c , 20 / -2); d = 50; c = -d / static_cast(n4); BOOST_CHECK_EQUAL(c , -1); c = static_cast(n4) / -d; BOOST_CHECK_EQUAL(c , -1); c = static_cast(n4) + a; BOOST_CHECK_EQUAL(c , 70); c = static_cast(n4) - a; BOOST_CHECK_EQUAL(c , 30); c = static_cast(n4) * a; BOOST_CHECK_EQUAL(c , 50 * 20); n1 = -2; n2 = -3; n3 = -4; a = static_cast(n1); b = static_cast(n2); c = static_cast(n3); d = a + b * c; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = static_cast(n1) + b * c; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = a + static_cast(n2) * c; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = a + b * static_cast(n3); BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = static_cast(n1) + static_cast(n2) * c; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = static_cast(n1) + b * static_cast(n3); BOOST_CHECK_EQUAL(d , -2 + -3 * -4); a += static_cast(n2) * c; BOOST_CHECK_EQUAL(a , -2 + -3 * -4); a = static_cast(n1); a += b * static_cast(n3); BOOST_CHECK_EQUAL(a , -2 + -3 * -4); a = static_cast(n1); d = b * c + a; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = b * c + static_cast(n1); BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = static_cast(n2) * c + a; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = b * static_cast(n3) + a; BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = static_cast(n2) * c + static_cast(n1); BOOST_CHECK_EQUAL(d , -2 + -3 * -4); d = b * static_cast(n3) + static_cast(n1); BOOST_CHECK_EQUAL(d , -2 + -3 * -4); a = -20; d = a - b * c; BOOST_CHECK_EQUAL(d , -20 - -3 * -4); n1 = -20; d = static_cast(n1) - b * c; BOOST_CHECK_EQUAL(d , -20 - -3 * -4); d = a - static_cast(n2) * c; BOOST_CHECK_EQUAL(d , -20 - -3 * -4); d = a - b * static_cast(n3); BOOST_CHECK_EQUAL(d , -20 - -3 * -4); d = static_cast(n1) - static_cast(n2) * c; BOOST_CHECK_EQUAL(d , -20 - -3 * -4); d = static_cast(n1) - b * static_cast(n3); BOOST_CHECK_EQUAL(d , -20 - -3 * -4); a -= static_cast(n2) * c; BOOST_CHECK_EQUAL(a , -20 - -3 * -4); a = static_cast(n1); a -= b * static_cast(n3); BOOST_CHECK_EQUAL(a , -20 - -3 * -4); a = -2; d = b * c - a; BOOST_CHECK_EQUAL(d , -3 * -4 - -2); n1 = -2; d = b * c - static_cast(n1); BOOST_CHECK_EQUAL(d , -3 * -4 - -2); d = static_cast(n2) * c - a; BOOST_CHECK_EQUAL(d , -3 * -4 - -2); d = b * static_cast(n3) - a; BOOST_CHECK_EQUAL(d , -3 * -4 - -2); d = static_cast(n2) * c - static_cast(n1); BOOST_CHECK_EQUAL(d , -3 * -4 - -2); d = b * static_cast(n3) - static_cast(n1); BOOST_CHECK_EQUAL(d , -3 * -4 - -2); // // Conversion from min and max values: // test_negative_mixed_minmax(boost::mpl::bool_::is_integer && std::numeric_limits::is_integer>()); } template void test_negative_mixed(boost::mpl::false_ const&) { } template void test_mixed(const boost::mpl::false_&) { } template inline bool check_is_nan(const Real& val, const boost::mpl::true_&) { return (boost::math::isnan)(val); } template inline bool check_is_nan(const Real&, const boost::mpl::false_&) { return false; } template inline Real negate_value(const Real& val, const boost::mpl::true_&) { return -val; } template inline Real negate_value(const Real& val, const boost::mpl::false_&) { return val; } template void test_mixed(const boost::mpl::true_&) { typedef typename lexical_cast_target_type::type target_type; typedef typename boost::mpl::if_< boost::is_convertible, typename boost::mpl::if_c::value && (sizeof(Num) < sizeof(int)), int, Num>::type, Real >::type cast_type; typedef typename boost::mpl::if_< boost::is_convertible, Num, Real >::type simple_cast_type; if(std::numeric_limits::is_specialized && std::numeric_limits::is_bounded && std::numeric_limits::digits < std::numeric_limits::digits) return; std::cout << "Testing mixed arithmetic with type: " << typeid(Real).name() << " and " << typeid(Num).name() << std::endl; static const int left_shift = std::numeric_limits::digits - 1; Num n1 = static_cast(1uLL << ((left_shift < 63) && (left_shift > 0) ? left_shift : 10)); Num n2 = 1; Num n3 = 0; Num n4 = 20; Num n5 = 8; test_comparisons(n1, n2, boost::is_convertible()); test_comparisons(n1, n3, boost::is_convertible()); test_comparisons(n1, n1, boost::is_convertible()); test_comparisons(n3, n1, boost::is_convertible()); test_comparisons(n2, n1, boost::is_convertible()); test_comparisons(n3, n3, boost::is_convertible()); // Default construct: BOOST_CHECK_EQUAL(Real(n1) , static_cast(n1)); BOOST_CHECK_EQUAL(Real(n2) , static_cast(n2)); BOOST_CHECK_EQUAL(Real(n3) , static_cast(n3)); BOOST_CHECK_EQUAL(Real(n4) , static_cast(n4)); BOOST_CHECK_EQUAL(Real(n1).template convert_to() , n1); BOOST_CHECK_EQUAL(Real(n2).template convert_to() , n2); BOOST_CHECK_EQUAL(Real(n3).template convert_to() , n3); BOOST_CHECK_EQUAL(Real(n4).template convert_to() , n4); #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS BOOST_CHECK_EQUAL(static_cast(Real(n1)) , n1); BOOST_CHECK_EQUAL(static_cast(Real(n2)) , n2); BOOST_CHECK_EQUAL(static_cast(Real(n3)) , n3); BOOST_CHECK_EQUAL(static_cast(Real(n4)) , n4); #endif // Again with expression templates: BOOST_CHECK_EQUAL((Real(n1) + 0).template convert_to(), n1); BOOST_CHECK_EQUAL((Real(n2) + 0).template convert_to(), n2); BOOST_CHECK_EQUAL((Real(n3) + 0).template convert_to(), n3); BOOST_CHECK_EQUAL((Real(n4) + 0).template convert_to(), n4); #ifndef BOOST_MP_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS BOOST_CHECK_EQUAL(static_cast(Real(n1) + 0), n1); BOOST_CHECK_EQUAL(static_cast(Real(n2) + 0), n2); BOOST_CHECK_EQUAL(static_cast(Real(n3) + 0), n3); BOOST_CHECK_EQUAL(static_cast(Real(n4) + 0), n4); #endif BOOST_CHECK_EQUAL(static_cast(n1), Real(n1)); BOOST_CHECK_EQUAL(static_cast(n2) , Real(n2)); BOOST_CHECK_EQUAL(static_cast(n3) , Real(n3)); BOOST_CHECK_EQUAL(static_cast(n4) , Real(n4)); #if defined(TEST_MPFR) Num tol = 10 * std::numeric_limits::epsilon(); #else Num tol = 0; #endif std::ios_base::fmtflags f = boost::is_floating_point::value ? std::ios_base::scientific : std::ios_base::fmtflags(0); int digits_to_print = boost::is_floating_point::value && std::numeric_limits::is_specialized ? std::numeric_limits::digits10 + 5 : 0; if(std::numeric_limits::digits <= std::numeric_limits::digits) { BOOST_CHECK_CLOSE(n1, checked_lexical_cast(Real(n1).str(digits_to_print, f)), tol); } BOOST_CHECK_CLOSE(n2, checked_lexical_cast(Real(n2).str(digits_to_print, f)), 0); BOOST_CHECK_CLOSE(n3, checked_lexical_cast(Real(n3).str(digits_to_print, f)), 0); BOOST_CHECK_CLOSE(n4, checked_lexical_cast(Real(n4).str(digits_to_print, f)), 0); // Assignment: Real r(0); BOOST_CHECK(r != static_cast(n1)); r = static_cast(n1); BOOST_CHECK_EQUAL(r , static_cast(n1)); r = static_cast(n2); BOOST_CHECK_EQUAL(r , static_cast(n2)); r = static_cast(n3); BOOST_CHECK_EQUAL(r , static_cast(n3)); r = static_cast(n4); BOOST_CHECK_EQUAL(r , static_cast(n4)); // Addition: r = static_cast(n2); BOOST_CHECK_EQUAL(r + static_cast(n4) , static_cast(n2 + n4)); BOOST_CHECK_EQUAL(Real(r + static_cast(n4)) , static_cast(n2 + n4)); r += static_cast(n4); BOOST_CHECK_EQUAL(r , static_cast(n2 + n4)); // subtraction: r = static_cast(n4); BOOST_CHECK_EQUAL(r - static_cast(n5) , static_cast(n4 - n5)); BOOST_CHECK_EQUAL(Real(r - static_cast(n5)) , static_cast(n4 - n5)); r -= static_cast(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 - n5)); // Multiplication: r = static_cast(n2); BOOST_CHECK_EQUAL(r * static_cast(n4) , static_cast(n2 * n4)); BOOST_CHECK_EQUAL(Real(r * static_cast(n4)) , static_cast(n2 * n4)); r *= static_cast(n4); BOOST_CHECK_EQUAL(r , static_cast(n2 * n4)); // Division: r = static_cast(n1); BOOST_CHECK_EQUAL(r / static_cast(n5) , static_cast(n1 / n5)); BOOST_CHECK_EQUAL(Real(r / static_cast(n5)) , static_cast(n1 / n5)); r /= static_cast(n5); BOOST_CHECK_EQUAL(r , static_cast(n1 / n5)); // // special cases for full coverage: // r = static_cast(n5) + Real(n4); BOOST_CHECK_EQUAL(r , static_cast(n4 + n5)); r = static_cast(n4) - Real(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 - n5)); r = static_cast(n4) * Real(n5); BOOST_CHECK_EQUAL(r , static_cast(n4 * n5)); r = static_cast(4 * n4) / Real(4); BOOST_CHECK_EQUAL(r , static_cast(n4)); typedef boost::mpl::bool_< (!std::numeric_limits::is_specialized || std::numeric_limits::is_signed) && (!std::numeric_limits::is_specialized || std::numeric_limits::is_signed)> signed_tag; test_negative_mixed(signed_tag()); n1 = 2; n2 = 3; n3 = 4; Real a(n1), b(n2), c(n3), d; d = a + b * c; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = static_cast(n1) + b * c; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = a + static_cast(n2) * c; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = a + b * static_cast(n3); BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = static_cast(n1) + static_cast(n2) * c; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = static_cast(n1) + b * static_cast(n3); BOOST_CHECK_EQUAL(d , 2 + 3 * 4); a += static_cast(n2) * c; BOOST_CHECK_EQUAL(a , 2 + 3 * 4); a = static_cast(n1); a += b * static_cast(n3); BOOST_CHECK_EQUAL(a , 2 + 3 * 4); a = static_cast(n1); d = b * c + a; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = b * c + static_cast(n1); BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = static_cast(n2) * c + a; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = b * static_cast(n3) + a; BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = static_cast(n2) * c + static_cast(n1); BOOST_CHECK_EQUAL(d , 2 + 3 * 4); d = b * static_cast(n3) + static_cast(n1); BOOST_CHECK_EQUAL(d , 2 + 3 * 4); a = 20; d = a - b * c; BOOST_CHECK_EQUAL(d , 20 - 3 * 4); n1 = 20; d = static_cast(n1) - b * c; BOOST_CHECK_EQUAL(d , 20 - 3 * 4); d = a - static_cast(n2) * c; BOOST_CHECK_EQUAL(d , 20 - 3 * 4); d = a - b * static_cast(n3); BOOST_CHECK_EQUAL(d , 20 - 3 * 4); d = static_cast(n1) - static_cast(n2) * c; BOOST_CHECK_EQUAL(d , 20 - 3 * 4); d = static_cast(n1) - b * static_cast(n3); BOOST_CHECK_EQUAL(d , 20 - 3 * 4); a -= static_cast(n2) * c; BOOST_CHECK_EQUAL(a , 20 - 3 * 4); a = static_cast(n1); a -= b * static_cast(n3); BOOST_CHECK_EQUAL(a , 20 - 3 * 4); a = 2; d = b * c - a; BOOST_CHECK_EQUAL(d , 3 * 4 - 2); n1 = 2; d = b * c - static_cast(n1); BOOST_CHECK_EQUAL(d , 3 * 4 - 2); d = static_cast(n2) * c - a; BOOST_CHECK_EQUAL(d , 3 * 4 - 2); d = b * static_cast(n3) - a; BOOST_CHECK_EQUAL(d , 3 * 4 - a); d = static_cast(n2) * c - static_cast(n1); BOOST_CHECK_EQUAL(d , 3 * 4 - 2); d = b * static_cast(n3) - static_cast(n1); BOOST_CHECK_EQUAL(d , 3 * 4 - 2); if(std::numeric_limits::has_infinity && std::numeric_limits::has_infinity) { d = static_cast(std::numeric_limits::infinity()); BOOST_CHECK_GT(d, (std::numeric_limits::max)()); d = static_cast(negate_value(std::numeric_limits::infinity(), boost::mpl::bool_::is_signed>())); BOOST_CHECK_LT(d, negate_value((std::numeric_limits::max)(), boost::mpl::bool_::is_signed>())); } if(std::numeric_limits::has_quiet_NaN && std::numeric_limits::has_quiet_NaN) { d = static_cast(std::numeric_limits::quiet_NaN()); BOOST_CHECK(check_is_nan(d, boost::mpl::bool_::has_quiet_NaN>())); d = static_cast(negate_value(std::numeric_limits::quiet_NaN(), boost::mpl::bool_::is_signed>())); BOOST_CHECK(check_is_nan(d, boost::mpl::bool_::has_quiet_NaN>())); } } template void test_members(Real) { // // Test sign and zero functions: // Real a = 20; Real b = 30; BOOST_CHECK(a.sign() > 0); BOOST_CHECK(!a.is_zero()); if(std::numeric_limits::is_signed) { a = -20; BOOST_CHECK(a.sign() < 0); BOOST_CHECK(!a.is_zero()); } a = 0; BOOST_CHECK_EQUAL(a.sign() , 0); BOOST_CHECK(a.is_zero()); a = 20; b = 30; a.swap(b); BOOST_CHECK_EQUAL(a , 30); BOOST_CHECK_EQUAL(b , 20); } template void test_members(boost::rational) { } template void test_signed_ops(const boost::mpl::true_&) { Real a(8); Real b(64); Real c(500); Real d(1024); Real ac; BOOST_CHECK_EQUAL(-a , -8); ac = a; ac = ac - b; BOOST_CHECK_EQUAL(ac , 8 - 64); ac = a; ac -= a + b; BOOST_CHECK_EQUAL(ac , -64); ac = a; ac -= b - a; BOOST_CHECK_EQUAL(ac , 16 - 64); ac = -a; BOOST_CHECK_EQUAL(ac , -8); ac = a; ac -= -a; BOOST_CHECK_EQUAL(ac , 16); ac = a; ac += -a; BOOST_CHECK_EQUAL(ac , 0); ac = b; ac /= -a; BOOST_CHECK_EQUAL(ac , -8); ac = a; ac *= -a; BOOST_CHECK_EQUAL(ac , -64); ac = a + -b; BOOST_CHECK_EQUAL(ac , 8 - 64); ac = -a + b; BOOST_CHECK_EQUAL(ac , -8+64); ac = -a + -b; BOOST_CHECK_EQUAL(ac , -72); ac = a + - + -b; // lots of unary operators!! BOOST_CHECK_EQUAL(ac , 72); test_conditional(Real(-a), -a); } template void test_signed_ops(const boost::mpl::false_&) { } template void test_basic_conditionals(Real a, Real b) { if(a) { BOOST_ERROR("Unexpected non-zero result"); } if(!a){} else { BOOST_ERROR("Unexpected zero result"); } b = 2; if(!b) { BOOST_ERROR("Unexpected zero result"); } if(b){} else { BOOST_ERROR("Unexpected non-zero result"); } if(a && b) { BOOST_ERROR("Unexpected zero result"); } if(!(a || b)) { BOOST_ERROR("Unexpected zero result"); } if(a + b){} else { BOOST_ERROR("Unexpected zero result"); } if(b - 2) { BOOST_ERROR("Unexpected non-zero result"); } } template void test() { #if !defined(NO_MIXED_OPS) && !defined(SLOW_COMPILER) boost::multiprecision::is_number tag; test_mixed(tag); test_mixed(tag); test_mixed(tag); test_mixed(tag); test_mixed(tag); test_mixed(tag); test_mixed(tag); test_mixed(tag); test_mixed(tag); #ifdef BOOST_HAS_LONG_LONG test_mixed(tag); test_mixed(tag); #endif test_mixed(tag); test_mixed(tag); test_mixed(tag); typedef typename related_type::type related_type; boost::mpl::bool_::value && !boost::is_same::value> tag2; test_mixed(tag2); #endif // // Integer only functions: // test_integer_ops(typename boost::multiprecision::number_category::type()); // // Real number only functions: // test_float_ops(typename boost::multiprecision::number_category::type()); // // Test basic arithmetic: // Real a(8); Real b(64); Real c(500); Real d(1024); BOOST_CHECK_EQUAL(a + b , 72); a += b; BOOST_CHECK_EQUAL(a , 72); BOOST_CHECK_EQUAL(a - b , 8); a -= b; BOOST_CHECK_EQUAL(a , 8); BOOST_CHECK_EQUAL(a * b , 8*64L); a *= b; BOOST_CHECK_EQUAL(a , 8*64L); BOOST_CHECK_EQUAL(a / b , 8); a /= b; BOOST_CHECK_EQUAL(a , 8); Real ac(a); BOOST_CHECK_EQUAL(ac , a); ac = a * c; BOOST_CHECK_EQUAL(ac , 8*500L); ac = 8*500L; ac = ac + b + c; BOOST_CHECK_EQUAL(ac , 8*500L+64+500); ac = a; ac = b + c + ac; BOOST_CHECK_EQUAL(ac , 8+64+500); ac = ac - b + c; BOOST_CHECK_EQUAL(ac , 8+64+500-64+500); ac = a; ac = b + c - ac; BOOST_CHECK_EQUAL(ac , -8+64+500); ac = a; ac = ac * b; BOOST_CHECK_EQUAL(ac , 8*64); ac = a; ac *= b * ac; BOOST_CHECK_EQUAL(ac , 8*8*64); ac = b; ac = ac / a; BOOST_CHECK_EQUAL(ac , 64/8); ac = b; ac /= ac / a; BOOST_CHECK_EQUAL(ac , 64 / (64/8)); ac = a; ac = b + ac * a; BOOST_CHECK_EQUAL(ac , 64 * 2); ac = a; ac = b - ac * a; BOOST_CHECK_EQUAL(ac , 0); ac = a; ac = b * (ac + a); BOOST_CHECK_EQUAL(ac , 64 * (16)); ac = a; ac = b / (ac * 1); BOOST_CHECK_EQUAL(ac , 64 / 8); ac = a; ac = ac + b; BOOST_CHECK_EQUAL(ac , 8 + 64); ac = a; ac = a + ac; BOOST_CHECK_EQUAL(ac , 16); ac = a; ac = a - ac; BOOST_CHECK_EQUAL(ac , 0); ac = a; ac += a + b; BOOST_CHECK_EQUAL(ac , 80); ac = a; ac += b + a; BOOST_CHECK_EQUAL(ac , 80); ac = +a; BOOST_CHECK_EQUAL(ac , 8); ac = 8; ac = a * ac; BOOST_CHECK_EQUAL(ac , 8*8); ac = a; ac = a; ac += +a; BOOST_CHECK_EQUAL(ac , 16); ac = a; ac += b - a; BOOST_CHECK_EQUAL(ac , 8 + 64-8); ac = a; ac += b*c; BOOST_CHECK_EQUAL(ac , 8 + 64 * 500); ac = a; ac = a; ac -= +a; BOOST_CHECK_EQUAL(ac , 0); ac = a; if(std::numeric_limits::is_signed || is_twos_complement_integer::value) { ac = a; ac -= c - b; BOOST_CHECK_EQUAL(ac , 8 - (500-64)); ac = a; ac -= b*c; BOOST_CHECK_EQUAL(ac , 8 - 500*64); } ac = a; ac += ac * b; BOOST_CHECK_EQUAL(ac , 8 + 8 * 64); if(std::numeric_limits::is_signed || is_twos_complement_integer::value) { ac = a; ac -= ac * b; BOOST_CHECK_EQUAL(ac , 8 - 8 * 64); } ac = a * 8; ac *= +a; BOOST_CHECK_EQUAL(ac , 64 * 8); ac = a; ac *= b * c; BOOST_CHECK_EQUAL(ac , 8 * 64 * 500); ac = a; ac *= b / a; BOOST_CHECK_EQUAL(ac , 8 * 64 / 8); ac = a; ac *= b + c; BOOST_CHECK_EQUAL(ac , 8 * (64 + 500)); ac = b; ac /= +a; BOOST_CHECK_EQUAL(ac , 8); ac = b; ac /= b / a; BOOST_CHECK_EQUAL(ac , 64 / (64/8)); ac = b; ac /= a + Real(0); BOOST_CHECK_EQUAL(ac , 8); // // simple tests with immediate values, these calls can be optimised in many backends: // ac = a + b; BOOST_CHECK_EQUAL(ac , 72); ac = a + +b; BOOST_CHECK_EQUAL(ac , 72); ac = +a + b; BOOST_CHECK_EQUAL(ac , 72); ac = +a + +b; BOOST_CHECK_EQUAL(ac , 72); ac = a; ac = b / ac; BOOST_CHECK_EQUAL(ac , b / a); // // Comparisons: // BOOST_CHECK_EQUAL((a == b) , false); BOOST_CHECK_EQUAL((a != b) , true); BOOST_CHECK_EQUAL((a <= b) , true); BOOST_CHECK_EQUAL((a < b) , true); BOOST_CHECK_EQUAL((a >= b) , false); BOOST_CHECK_EQUAL((a > b) , false); BOOST_CHECK_EQUAL((a+b == b) , false); BOOST_CHECK_EQUAL((a+b != b) , true); BOOST_CHECK_EQUAL((a+b >= b) , true); BOOST_CHECK_EQUAL((a+b > b) , true); BOOST_CHECK_EQUAL((a+b <= b) , false); BOOST_CHECK_EQUAL((a+b < b) , false); BOOST_CHECK_EQUAL((a == b+a) , false); BOOST_CHECK_EQUAL((a != b+a) , true); BOOST_CHECK_EQUAL((a <= b+a) , true); BOOST_CHECK_EQUAL((a < b+a) , true); BOOST_CHECK_EQUAL((a >= b+a) , false); BOOST_CHECK_EQUAL((a > b+a) , false); BOOST_CHECK_EQUAL((a+b == b+a) , true); BOOST_CHECK_EQUAL((a+b != b+a) , false); BOOST_CHECK_EQUAL((a+b <= b+a) , true); BOOST_CHECK_EQUAL((a+b < b+a) , false); BOOST_CHECK_EQUAL((a+b >= b+a) , true); BOOST_CHECK_EQUAL((a+b > b+a) , false); BOOST_CHECK_EQUAL((8 == b+a) , false); BOOST_CHECK_EQUAL((8 != b+a) , true); BOOST_CHECK_EQUAL((8 <= b+a) , true); BOOST_CHECK_EQUAL((8 < b+a) , true); BOOST_CHECK_EQUAL((8 >= b+a) , false); BOOST_CHECK_EQUAL((8 > b+a) , false); BOOST_CHECK_EQUAL((800 == b+a) , false); BOOST_CHECK_EQUAL((800 != b+a) , true); BOOST_CHECK_EQUAL((800 >= b+a) , true); BOOST_CHECK_EQUAL((800 > b+a) , true); BOOST_CHECK_EQUAL((800 <= b+a) , false); BOOST_CHECK_EQUAL((800 < b+a) , false); BOOST_CHECK_EQUAL((72 == b+a) , true); BOOST_CHECK_EQUAL((72 != b+a) , false); BOOST_CHECK_EQUAL((72 <= b+a) , true); BOOST_CHECK_EQUAL((72 < b+a) , false); BOOST_CHECK_EQUAL((72 >= b+a) , true); BOOST_CHECK_EQUAL((72 > b+a) , false); BOOST_CHECK_EQUAL((b + a == 8), false); BOOST_CHECK_EQUAL((b + a != 8), true); BOOST_CHECK_EQUAL((b + a >= 8), true); BOOST_CHECK_EQUAL((b + a > 8), true); BOOST_CHECK_EQUAL((b + a <= 8), false); BOOST_CHECK_EQUAL((b + a < 8), false); BOOST_CHECK_EQUAL((b + a == 800), false); BOOST_CHECK_EQUAL((b + a != 800), true); BOOST_CHECK_EQUAL((b + a <= 800), true); BOOST_CHECK_EQUAL((b + a < 800), true); BOOST_CHECK_EQUAL((b + a >= 800), false); BOOST_CHECK_EQUAL((b + a > 800), false); BOOST_CHECK_EQUAL((b + a == 72), true); BOOST_CHECK_EQUAL((b + a != 72), false); BOOST_CHECK_EQUAL((b + a >= 72), true); BOOST_CHECK_EQUAL((b + a > 72), false); BOOST_CHECK_EQUAL((b + a <= 72), true); BOOST_CHECK_EQUAL((b + a < 72), false); test_members(a); // // Use in Boolean context: // a = 0; b = 2; test_basic_conditionals(a, b); // // Test iostreams: // std::stringstream ss; a = 20; b = 2; ss << a; ss >> c; BOOST_CHECK_EQUAL(a , c); ss.clear(); ss << a + b; ss >> c; BOOST_CHECK_EQUAL(c , 22); BOOST_CHECK_EQUAL(c , a + b); // // More cases for complete code coverage: // a = 20; b = 30; swap(a, b); BOOST_CHECK_EQUAL(a , 30); BOOST_CHECK_EQUAL(b , 20); a = 20; b = 30; std::swap(a, b); BOOST_CHECK_EQUAL(a , 30); BOOST_CHECK_EQUAL(b , 20); a = 20; b = 30; a = a + b * 2; BOOST_CHECK_EQUAL(a , 20 + 30 * 2); a = 100; a = a - b * 2; BOOST_CHECK_EQUAL(a , 100 - 30 * 2); a = 20; a = a * (b + 2); BOOST_CHECK_EQUAL(a , 20 * (32)); a = 20; a = (b + 2) * a; BOOST_CHECK_EQUAL(a , 20 * (32)); a = 90; b = 2; a = a / (b + 0); BOOST_CHECK_EQUAL(a , 45); a = 20; b = 30; c = (a * b) + 22; BOOST_CHECK_EQUAL(c , 20 * 30 + 22); c = 22 + (a * b); BOOST_CHECK_EQUAL(c , 20 * 30 + 22); c = 10; ac = a + b * c; BOOST_CHECK_EQUAL(ac , 20 + 30 * 10); ac = b * c + a; BOOST_CHECK_EQUAL(ac , 20 + 30 * 10); a = a + b * c; BOOST_CHECK_EQUAL(a , 20 + 30 * 10); a = 20; b = a + b * c; BOOST_CHECK_EQUAL(b , 20 + 30 * 10); b = 30; c = a + b * c; BOOST_CHECK_EQUAL(c , 20 + 30 * 10); c = 10; c = a + b / c; BOOST_CHECK_EQUAL(c , 20 + 30 / 10); // // Test conditionals: // a = 20; test_conditional(a, +a); test_conditional(a, (a + 0)); test_signed_ops(boost::mpl::bool_::is_signed>()); // // Test move: // #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES Real m(static_cast(a)); BOOST_CHECK_EQUAL(m, 20); // Move from already moved from object: Real m2(static_cast(a)); // assign from moved from object // (may result in "a" being left in valid state as implementation artifact): c = static_cast(a); // assignment to moved-from objects: c = static_cast(m); BOOST_CHECK_EQUAL(c, 20); m2 = c; BOOST_CHECK_EQUAL(c, 20); // Destructor of "a" checks destruction of moved-from-object... Real m3(static_cast(a)); #endif // // min and max overloads: // #if !defined(min) && !defined(max) using std::max; using std::min; a = 2; b = 5; c = 6; BOOST_CHECK_EQUAL(min(a, b), a); BOOST_CHECK_EQUAL(min(b, a), a); BOOST_CHECK_EQUAL(max(a, b), b); BOOST_CHECK_EQUAL(max(b, a), b); BOOST_CHECK_EQUAL(min(a, b + c), a); BOOST_CHECK_EQUAL(min(b + c, a), a); BOOST_CHECK_EQUAL(min(a, c - b), 1); BOOST_CHECK_EQUAL(min(c - b, a), 1); BOOST_CHECK_EQUAL(max(a, b + c), 11); BOOST_CHECK_EQUAL(max(b + c, a), 11); BOOST_CHECK_EQUAL(max(a, c - b), a); BOOST_CHECK_EQUAL(max(c - b, a), a); BOOST_CHECK_EQUAL(min(a + b, b + c), 7); BOOST_CHECK_EQUAL(min(b + c, a + b), 7); BOOST_CHECK_EQUAL(max(a + b, b + c), 11); BOOST_CHECK_EQUAL(max(b + c, a + b), 11); BOOST_CHECK_EQUAL(min(a + b, c - a), 4); BOOST_CHECK_EQUAL(min(c - a, a + b), 4); BOOST_CHECK_EQUAL(max(a + b, c - a), 7); BOOST_CHECK_EQUAL(max(c - a, a + b), 7); long l1(2), l2(3), l3; l3 = min(l1, l2) + max(l1, l2) + max(l1, l2) + min(l1, l2); BOOST_CHECK_EQUAL(l3, 10); #endif // // Bug cases, self assignment first: // a = 20; a = a; BOOST_CHECK_EQUAL(a, 20); a = 2; a = a * a * a; BOOST_CHECK_EQUAL(a, 8); a = 2; a = a + a + a; BOOST_CHECK_EQUAL(a, 6); a = 2; a = a - a + a; BOOST_CHECK_EQUAL(a, 2); a = 2; a = a + a - a; BOOST_CHECK_EQUAL(a, 2); a = 2; a = a * a - a; BOOST_CHECK_EQUAL(a, 2); a = 2; a = a + a * a; BOOST_CHECK_EQUAL(a, 6); a = 2; a = (a + a) * a; BOOST_CHECK_EQUAL(a, 8); }