gmp_snips.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/gmp.hpp>
  6. #include <boost/math/special_functions/gamma.hpp>
  7. #include <iostream>
  8. void t1()
  9. {
  10. //[mpz_eg
  11. //=#include <boost/multiprecision/gmp.hpp>
  12. //=#include <iostream>
  13. //=
  14. //=int main()
  15. //={
  16. using namespace boost::multiprecision;
  17. mpz_int v = 1;
  18. // Do some arithmetic:
  19. for(unsigned i = 1; i <= 1000; ++i)
  20. v *= i;
  21. std::cout << v << std::endl; // prints 1000!
  22. // Access the underlying representation:
  23. mpz_t z;
  24. mpz_init(z);
  25. mpz_set(z, v.backend().data());
  26. mpz_clear(z);
  27. //= return 0;
  28. //=}
  29. //]
  30. }
  31. void t2()
  32. {
  33. //[mpf_eg
  34. //=#include <boost/multiprecision/gmp.hpp>
  35. //=#include <boost/math/special_functions/gamma.hpp>
  36. //=#include <iostream>
  37. //=
  38. //=int main()
  39. //={
  40. using namespace boost::multiprecision;
  41. // Operations at variable precision and limited standard library support:
  42. mpf_float a = 2;
  43. mpf_float::default_precision(1000);
  44. std::cout << mpf_float::default_precision() << std::endl;
  45. std::cout << sqrt(a) << std::endl; // print root-2
  46. // Operations at fixed precision and full standard library support:
  47. mpf_float_100 b = 2;
  48. std::cout << std::numeric_limits<mpf_float_100>::digits << std::endl;
  49. // We can use any C++ std lib function:
  50. std::cout << log(b) << std::endl; // print log(2)
  51. // We can also use any function from Boost.Math:
  52. std::cout << boost::math::tgamma(b) << std::endl;
  53. // These even work when the argument is an expression template:
  54. std::cout << boost::math::tgamma(b * b) << std::endl;
  55. // Access the underlying representation:
  56. mpf_t f;
  57. mpf_init(f);
  58. mpf_set(f, a.backend().data());
  59. mpf_clear(f);
  60. //= return 0;
  61. //=}
  62. //]
  63. }
  64. void t3()
  65. {
  66. //[mpq_eg
  67. //=#include <boost/multiprecision/gmp.hpp>
  68. //=#include <boost/multiprecision/gmp.hpp>
  69. //=#include <iostream>
  70. //=
  71. //=int main()
  72. //={
  73. using namespace boost::multiprecision;
  74. mpq_rational v = 1;
  75. // Do some arithmetic:
  76. for(unsigned i = 1; i <= 1000; ++i)
  77. v *= i;
  78. v /= 10;
  79. std::cout << v << std::endl; // prints 1000! / 10
  80. std::cout << numerator(v) << std::endl;
  81. std::cout << denominator(v) << std::endl;
  82. mpq_rational w(2, 3); // component wise constructor
  83. std::cout << w << std::endl; // prints 2/3
  84. // Access the underlying data:
  85. mpq_t q;
  86. mpq_init(q);
  87. mpq_set(q, v.backend().data());
  88. mpq_clear(q);
  89. //= return 0;
  90. //=}
  91. //]
  92. }
  93. int main()
  94. {
  95. t1();
  96. t2();
  97. t3();
  98. return 0;
  99. }