/////////////////////////////////////////////////////////////// // Copyright 2012 John Maddock. Distributed under the Boost // Software License, Version 1.0. (See accompanying file // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt // // Compare arithmetic results using fixed_int to GMP results. // #ifdef _MSC_VER #define _SCL_SECURE_NO_WARNINGS #endif #include #include "test.hpp" template void test() { using namespace boost::multiprecision; typedef Number test_type; test_type h = (std::numeric_limits::max)(); test_type l = (std::numeric_limits::max)(); BigNumber r; add(r, h, h); BOOST_CHECK_EQUAL(r, cpp_int(h) + cpp_int(h)); multiply(r, h, h); BOOST_CHECK_EQUAL(r, cpp_int(h) * cpp_int(h)); if (std::numeric_limits::is_signed) { subtract(r, l, h); BOOST_CHECK_EQUAL(r, cpp_int(l) - cpp_int(h)); subtract(r, h, l); BOOST_CHECK_EQUAL(r, cpp_int(h) - cpp_int(l)); multiply(r, l, l); BOOST_CHECK_EQUAL(r, cpp_int(l) * cpp_int(l)); } // // Try again with integer types as the source: // enum { max_digits = std::numeric_limits::is_signed ? std::numeric_limits::digits : std::numeric_limits::digits }; enum { require_digits = std::numeric_limits::digits <= 2 * max_digits ? std::numeric_limits::digits / 2 : max_digits }; typedef typename boost::uint_t::least uint_least; typedef typename boost::int_t::least int_least; typedef typename boost::mpl::if_c::is_signed, int_least, uint_least>::type i_type; i_type ih = (std::numeric_limits::max)(); i_type il = (std::numeric_limits::max)(); add(r, ih, ih); BOOST_CHECK_EQUAL(r, cpp_int(ih) + cpp_int(ih)); multiply(r, ih, ih); BOOST_CHECK_EQUAL(r, cpp_int(ih) * cpp_int(ih)); if (std::numeric_limits::is_signed) { subtract(r, il, ih); BOOST_CHECK_EQUAL(r, cpp_int(il) - cpp_int(ih)); subtract(r, ih, il); BOOST_CHECK_EQUAL(r, cpp_int(ih) - cpp_int(il)); multiply(r, il, il); BOOST_CHECK_EQUAL(r, cpp_int(il) * cpp_int(il)); } } void test_rational_mixed() { using namespace boost::multiprecision; cpp_int a(2); cpp_rational r(10); BOOST_CHECK_EQUAL(a + -r, -8); BOOST_CHECK_EQUAL(-r + a, -8); BOOST_CHECK_EQUAL(-a + r, 8); BOOST_CHECK_EQUAL(r + -a, 8); BOOST_CHECK_EQUAL(a - -r, 12); BOOST_CHECK_EQUAL(-r - a, -12); BOOST_CHECK_EQUAL(-a - r, -12); BOOST_CHECK_EQUAL(r - -a, 12); BOOST_CHECK_EQUAL(a * -r, -20); BOOST_CHECK_EQUAL(-r * a, -20); BOOST_CHECK_EQUAL(-a * r, -20); BOOST_CHECK_EQUAL(r * -a, -20); BOOST_CHECK_EQUAL(a / -r, cpp_rational(-2, 10)); BOOST_CHECK_EQUAL(-r / a, -5); BOOST_CHECK_EQUAL(cpp_rational(-a / r), cpp_rational(-2, 10)); BOOST_CHECK_EQUAL(r / -a, -5); } int main() { using namespace boost::multiprecision; test_rational_mixed(); test(); test(); test, et_off>, checked_int128_t>(); test(); test(); test(); test, et_off>, checked_uint128_t>(); test(); return boost::report_errors(); }