创建使用Boost mpfr_float变量precision的数学常数,如PI或电子邮件 [英] creating math constants of variable precision using Boost mpfr_float, such as pi or e

查看:321
本文介绍了创建使用Boost mpfr_float变量precision的数学常数,如PI或电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Boost.Multi precision周围的MPFR后端封装和我有我想要的precision一些麻烦创建PI(和电子或任何其他数学常数)。我觉得我想要做的应该是可能的,因为使用的 Boost.Math常量在<的一href=\"http://www.boost.org/doc/libs/1_57_0/libs/multi$p$pcision/doc/html/boost_multi$p$pcision/tut/floats/fp_eg/aos.html\"相对=nofollow>为Boost.Multi precision 教程页面。在本教程中,他们使用的各类固定precision数字,如 cpp_dec_float_50 - 我想和variable_ precision做 mpfr_float 。看看下面的code:

I am using Boost.Multiprecision for wrappers around the mpfr backend, and I am having some trouble creating pi (and e or any other math constant) to my desired precision. I feel like what I want to do should be possible, because of the use of Boost.Math for constants on a tutorial page for Boost.Multiprecision. In the tutorial, they use fixed-precision numbers of types such as cpp_dec_float_50 -- I want to do it with variable_precision 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;
}

其结果是数字, PI ,其中有precision 1000,但只有丕〜165位,就证明了输出语句的结果,这证实了我的 PI 是precision 1000,约165正确数字,835〜打印零。这显然​​是错误的。

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

是有可能使一个boost ::多precision :: mpfr_float在高precision填充来自升压常数::数学::常数?

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

请注意,我的需求的使用变量precision类型,以及其他高但-固定precision类型是不是一种选择。我必须能够在运行时动态更改precision。

Note that I need to use a variable precision type, and that other high-but-fixed-precision types are not an option. I must be able to change precision on the fly at runtime.

推荐答案

嵌入在Boost头文件中的常量有一个有限precision,你已经打了极限。如果你需要PI(例如)的higher- precision版本,你需要自己带了。

The constants embedded in the Boost headers have a limited precision, and you've hit that limit. If you need a higher-precision version of pi (for instance), you'll need to bring it yourself.

例如,在标题中的的boost ::数学::常量:: PI 的定义是:

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

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

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

(这个定义只给出了110位过去小数位,顺便说一句。你要超越有可能的任何数字是不正确的!)

(This definition only gives 110 digits past the decimal place, by the way. Any digits you're getting beyond that are likely to be incorrect!)

这篇关于创建使用Boost mpfr_float变量precision的数学常数,如PI或电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆