jukaukor

boost_mpfr.cpp

Jun 30th, 2020
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <chrono>
  3. #include <cmath>
  4.  
  5. #include <boost/multiprecision/cpp_int.hpp>
  6. #include <boost/multiprecision/mpfr.hpp>
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.  
  16. int main()
  17. {
  18. namespace mp = boost::multiprecision;
  19. using namespace boost::multiprecision;
  20.  
  21. // mpfr_float
  22. mpfr_float a = 2;
  23. std::cout << "Operations at variable precision and no numeric_limits support:" << std::endl;
  24. mpfr_float::default_precision(1000);
  25. std::cout << "mpfr_float::default_precision() = " << mpfr_float::default_precision() << std::endl;
  26. std::cout << "sqrt(2) = " << sqrt(a) << std::endl;
  27. // fixed precision mpfr_float_100 and full numeric_limits support:
  28.  
  29. mpfr_float_100 testmpfr = 24153319581254312065344.0;
  30. std::cout << "digits : " << std::numeric_limits<mpfr_float_100>::digits << std::endl;
  31. std::cout << "digit10 : " << std::numeric_limits<mpfr_float_100>::digits10 << std::endl;
  32. // We can use any C++ std lib function:
  33. std::cout << "root-2 std sqrt(a) = " << sqrt(a) << std::endl; // print root-2
  34. int256_t taxicabnumber;
  35. std::cout << "Anna taxicabnumber = 24153319581254312065344 " << std::endl;
  36. std::cin >> taxicabnumber;
  37. std::cout << "boost::math:cbrt(testmpfr) = " << boost::math::cbrt(testmpfr) << std::endl;
  38. std::cout << "mp:cbrt(testmpfr) = " << mp::cbrt(testmpfr) << std::endl;
  39. mpfr_float_100 taxi = taxicabnumber.convert_to<mp::mpfr_float_100>();
  40. std::cout << "Operations at fixed precision and full numeric_limits support:" << std::endl;
  41. std::cout << "wikipedia sqrt(2) = 1.41421356237309504880168872420969807856967187537694807317667973799..." << std::endl;
  42. std::cout << "wolfram: cubitroot(2) = 1.259921049894873164767210607278228350570251464701507980081..." << std::endl;
  43. mpfr_float_100 bfr = 2;
  44.  
  45. // tulostus maksimitarkuudella eli 100-bit
  46.  
  47. // We can use any C++ std lib function:
  48. std::cout << "tulostus 100 numeron tarkkuudella " << std::endl;
  49. std::cout << std::setprecision(std::numeric_limits<mpfr_float_100>::max_digits10) << std::endl;
  50. std::cout << "sqrt(testmpfr) = " << sqrt(testmpfr) << std::endl;
  51. std::cout << "sqrt(bfr) = " << sqrt(bfr) << std::endl;
  52. std::cout << "log(bfr) = " << log(bfr) << std::endl; // print log(2)
  53. // We can also use any function from Boost.Math:
  54. std::cout << "mp::sqrt(testmpfr) = " << mp::sqrt(testmpfr) << std::endl;
  55. std::cout << "mp::sqrt(bfr) = " << mp::sqrt(bfr) << std::endl;
  56. std::cout << "mp::log(bfr) = " << mp::log(bfr) << std::endl;
  57. std::cout << "mp::cbrt(bfr) = " << mp::cbrt(bfr) << std::endl;
  58. std::cout << "boost::math::tgamma(bfr) = " << boost::math::tgamma(bfr) << std::endl;
  59. // These even work when the argument is an expression template:
  60. std::cout << "boost::math::tgamma(bfr*bfr) = " << boost::math::tgamma(bfr * bfr) << std::endl;
  61. std::cout << "mp::pow(taxi,1/3.0) = " << mp::pow(taxi,1/3.0) << std::endl;
  62. int128_t raja;
  63. raja = (mp::pow(taxi,1/3.0) +0.5).convert_to<int128_t>();
  64. std::cout << "raja = " << raja << std::endl;
  65.  
  66.  
  67.  
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment