cpp_int_snips.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2011 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. #include <boost/multiprecision/cpp_int.hpp>
  6. #include <iostream>
  7. void t1()
  8. {
  9. //[cpp_int_eg
  10. //=#include <boost/multiprecision/cpp_int.hpp>
  11. //=#include <iostream>
  12. //=
  13. //=int main()
  14. //={
  15. using namespace boost::multiprecision;
  16. int128_t v = 1;
  17. // Do some fixed precision arithmetic:
  18. for(unsigned i = 1; i <= 20; ++i)
  19. v *= i;
  20. std::cout << v << std::endl; // prints 2432902008176640000 (i.e. 20!)
  21. // Repeat at arbitrary precision:
  22. cpp_int u = 1;
  23. for(unsigned i = 1; i <= 100; ++i)
  24. u *= i;
  25. // prints 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000 (i.e. 100!)
  26. std::cout << u << std::endl;
  27. //= return 0;
  28. //=}
  29. //]
  30. }
  31. void t3()
  32. {
  33. //[cpp_rational_eg
  34. //=#include <boost/multiprecision/cpp_int.hpp>
  35. //=#include <iostream>
  36. //=
  37. //=int main()
  38. //={
  39. using namespace boost::multiprecision;
  40. cpp_rational v = 1;
  41. // Do some arithmetic:
  42. for(unsigned i = 1; i <= 1000; ++i)
  43. v *= i;
  44. v /= 10;
  45. std::cout << v << std::endl; // prints 1000! / 10
  46. std::cout << numerator(v) << std::endl;
  47. std::cout << denominator(v) << std::endl;
  48. cpp_rational w(2, 3); // component wise constructor
  49. std::cout << w << std::endl; // prints 2/3
  50. //= return 0;
  51. //=}
  52. //]
  53. }
  54. int main()
  55. {
  56. t1();
  57. t3();
  58. return 0;
  59. }