/////////////////////////////////////////////////////////////// // 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_ // // 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: // static const unsigned max_digits = std::numeric_limits::is_signed ? std::numeric_limits::digits : std::numeric_limits::digits; static const unsigned require_digits = std::numeric_limits::digits <= 2 * max_digits ? std::numeric_limits::digits / 2 : max_digits; typedef typename boost::mpl::if_c::is_signed, typename boost::int_t::least, typename boost::uint_t::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)); } } int main() { using namespace boost::multiprecision; test(); test(); test, et_off>, checked_int128_t>(); test(); test(); test(); test, et_off>, checked_uint128_t>(); test(); return boost::report_errors(); }