mixed_integer_arithmetic.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2012 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. //
  6. // Compare arithmetic results using fixed_int to GMP results.
  7. //
  8. #ifdef _MSC_VER
  9. # define _SCL_SECURE_NO_WARNINGS
  10. #endif
  11. //[mixed_eg
  12. #include <boost/multiprecision/cpp_int.hpp>
  13. int main()
  14. {
  15. using namespace boost::multiprecision;
  16. boost::uint64_t i = (std::numeric_limits<boost::uint64_t>::max)();
  17. boost::uint64_t j = 1;
  18. uint128_t ui128;
  19. uint256_t ui256;
  20. //
  21. // Start by performing arithmetic on 64-bit integers to yield 128-bit results:
  22. //
  23. std::cout << std::hex << std::showbase << i << std::endl;
  24. std::cout << std::hex << std::showbase << add(ui128, i, j) << std::endl;
  25. std::cout << std::hex << std::showbase << multiply(ui128, i, i) << std::endl;
  26. //
  27. // The try squaring a 128-bit integer to yield a 256-bit result:
  28. //
  29. ui128 = (std::numeric_limits<uint128_t>::max)();
  30. std::cout << std::hex << std::showbase << multiply(ui256, ui128, ui128) << std::endl;
  31. return 0;
  32. }
  33. //]
  34. /*
  35. Program output:
  36. //[mixed_output
  37. 0xffffffffffffffff
  38. 0x10000000000000000
  39. 0xFFFFFFFFFFFFFFFE0000000000000001
  40. 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFE00000000000000000000000000000001
  41. //]
  42. */