Creating variable precision math constants using Boost mpfr_float like pi or e

I use Boost.Multiprecision to wrap around the mpfr backend, and I'm having trouble creating pi (and e or any other math constant) for my desired accuracy. I feel that what I want to do should be possible due to the use of Boost.Math for constants on the tutorial page for Boost.Multiprecision . In the tutorial, they use fixed-type numbers such as cpp_dec_float_50 - I want to do this using mpfr_float variable mpfr_float . Check out the following code:

 #include <boost/multiprecision/mpfr.hpp> #include <boost/math/constants/constants.hpp> #include <iostream> ... int main() { boost::multiprecision::mpfr_float::default_precision(1000); boost::multiprecision::mpfr_float pi = boost::math::constants::pi<boost::multiprecision::mpfr_float>(); std::cout << std::fixed; std::cout.precision(1000); std::cout << pi.precision() << " " << pi << std::endl; } 

The result is a number, pi , which has an accuracy of 1000 but only ~ 165 digits of pi, as evidenced by the result of the output statements, which confirm that my pi has an accuracy of 1000, and prints about 165 correct digits and ~ 835 zeros. this is clearly wrong.

is it possible to make boost :: multiprecision :: mpfr_float with high precision filled with constants from boost :: math :: constants?

Please note that I need to use a variable precision type and that other types with a high degree of precision are not an option. I should be able to change accuracy on the fly at runtime.

0
source share
1 answer

Constants built into Boost headers have limited precision, and you have reached that limit. If you need a higher version of pi (for example), you will need to bring it yourself.

For example, the definition of boost::math::constants::pi in the headers:

  BOOST_DEFINE_MATH_CONSTANT(pi, 3.141592653589793238462643383279502884e+00, "3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651e+00") 

- https://github.com/boostorg/math/blob/master/include/boost/math/constants/constants.hpp

(This definition only gives 110 digits per decimal place, by the way. Any numbers you get outside of this are likely to be wrong!)

+2
source

Source: https://habr.com/ru/post/1235102/


All Articles