tommath_snips.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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/tommath.hpp>
  6. #include <iostream>
  7. void t1()
  8. {
  9. //[tommath_eg
  10. //=#include <boost/multiprecision/tommath.hpp>
  11. //=#include <iostream>
  12. //=
  13. //=int main()
  14. //={
  15. boost::multiprecision::tom_int v = 1;
  16. // Do some arithmetic:
  17. for(unsigned i = 1; i <= 1000; ++i)
  18. v *= i;
  19. std::cout << v << std::endl; // prints 1000!
  20. std::cout << std::hex << v << std::endl; // prints 1000! in hex format
  21. try{
  22. std::cout << std::hex << -v << std::endl; // Ooops! can't print a negative value in hex format!
  23. }
  24. catch(const std::runtime_error& e)
  25. {
  26. std::cout << e.what() << std::endl;
  27. }
  28. try{
  29. // v is not a 2's complement type, bitwise operations are only supported
  30. // on positive values:
  31. v = -v & 2;
  32. }
  33. catch(const std::runtime_error& e)
  34. {
  35. std::cout << e.what() << std::endl;
  36. }
  37. //= return 0;
  38. //=}
  39. //]
  40. }
  41. void t3()
  42. {
  43. //[mp_rat_eg
  44. //=#include <boost/multiprecision/tommath.hpp>
  45. //=#include <iostream>
  46. //=
  47. //=int main()
  48. //={
  49. using namespace boost::multiprecision;
  50. tom_rational v = 1;
  51. // Do some arithmetic:
  52. for(unsigned i = 1; i <= 1000; ++i)
  53. v *= i;
  54. v /= 10;
  55. std::cout << v << std::endl; // prints 1000! / 10
  56. std::cout << numerator(v) << std::endl;
  57. std::cout << denominator(v) << std::endl;
  58. tom_rational w(2, 3); // Component wise constructor
  59. std::cout << w << std::endl; // prints 2/3
  60. //= return 0;
  61. //=}
  62. //]
  63. }
  64. int main()
  65. {
  66. t1();
  67. t3();
  68. return 0;
  69. }