mpfr_snips.cpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  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. //[mpfr_eg
  6. #include <boost/multiprecision/mpfr.hpp>
  7. #include <boost/math/special_functions/gamma.hpp>
  8. #include <iostream>
  9. int main()
  10. {
  11. using namespace boost::multiprecision;
  12. // Operations at variable precision and no numeric_limits support:
  13. mpfr_float a = 2;
  14. mpfr_float::default_precision(1000);
  15. std::cout << mpfr_float::default_precision() << std::endl;
  16. std::cout << sqrt(a) << std::endl; // print root-2
  17. // Operations at fixed precision and full numeric_limits support:
  18. mpfr_float_100 b = 2;
  19. std::cout << std::numeric_limits<mpfr_float_100>::digits << std::endl;
  20. // We can use any C++ std lib function:
  21. std::cout << log(b) << std::endl; // print log(2)
  22. // We can also use any function from Boost.Math:
  23. std::cout << boost::math::tgamma(b) << std::endl;
  24. // These even work when the argument is an expression template:
  25. std::cout << boost::math::tgamma(b * b) << std::endl;
  26. // Access the underlying data:
  27. mpfr_t r;
  28. mpfr_init(r);
  29. mpfr_set(r, b.backend().data(), GMP_RNDN);
  30. mpfr_clear(r);
  31. return 0;
  32. }
  33. //]