如何在运行时数precision用的boost ::多precision改变 [英] how to change at runtime number precision with boost::multiprecision
问题描述
我从<读href=\"http://www.boost.org/doc/libs/1_59_0/libs/multi$p$pcision/doc/html/boost_multi$p$pcision/intro.html\"相对=nofollow>的boost ::多precision文档:
根据不同的号码类型,precision可能是任意大(仅受可用内存的限制),固定在由成员函数在运行时控制的编译时间(例如50或100位十进制数),或变量。该类型是前启用$ pssion模板-P $超过天真的用户定义类型更好的性能。
块引用>我读过一些文档,但我还没有发现有关在运行时更改precision什么。我见过的唯一的模板,让我在编译的时候设置precision,这不是我想要的(我想创建使用非常高的变焦倍率缩放分形的程序)。
如何创建一个double类型,让我在运行时改变它的precision?
只有解决方案大多数数字后端有一个固定容量的可选的
另外还要注意一些后端(显着的CPP的)接受一个allocator,所以他们/隐含/成长必需的(直到定限制):
通常
cpp_bin_float
分配没有记忆:所有为它的数字所需要的空间是在类中直接分配。因此,应注意不使用类太高的数字算不算堆栈空间的需求可以增长失控。如果再presents问题,那么提供一个allocator作为模板参数导致cpp_bin_float
来动态分配它所需要的内存
块引用>让我检查的文档,最流行的后端:
GMP的
mpf_float
坚持这样的:的typedef号&LT; gmp_float℃的&GT; &GT; mpf_float;
键入gmp_float可以在固定的precision通过指定一个非零Digits10模板参数,或在变量precision通过设置模板参数用于零。
的
的typedef mpf_float
提供了一个变量precision类型,其
precision可以通过数字成员函数来控制。
块引用>有关MPFR后端类似:
键入
mpfr_float_backend
可以在固定的precision通过指定一个非零Digits10模板参数由模板参数设置为使用或变量precision零。
的类型定义mpfr_float提供了一个变量precision类型,其precision可以通过数字成员函数来控制。
块引用>动态precision用法的示例:
//操作在变量precision没有numeric_limits支持:
mpfr_float一个= 2;
mpfr_float :: DEFAULT_ precision(1000);
性病::法院LT&;&LT; mpfr_float :: DEFAULT_ precision()&LT;&LT;的std :: ENDL;
性病::法院LT&;&LT; SQRT(a)及所述;&下;的std :: ENDL; //打印根2这将离开
numeric_limits
未定义I've read from the boost::multiprecision documentation:
Depending upon the number type, precision may be arbitrarily large (limited only by available memory), fixed at compile time (for example 50 or 100 decimal digits), or a variable controlled at run-time by member functions. The types are expression-template-enabled for better performance than naive user-defined types.
I've read some more documentation but I've not found anything regarding changing precision at runtime. I've seen only templates that allows me to set precision at compile time, that's not what I want (I want to create a program for zooming fractals using very high zoom factor).
How can I create a double type that allows me to change its precision at runtime?
解决方案Most of the number backends have a fixed capacity only optionally.
Also note some backends (notable the cpp ones) take an allocator, so they /implicitly/ grow as required (until the given limit):
Normally
cpp_bin_float
allocates no memory: all of the space required for its digits are allocated directly within the class. As a result care should be taken not to use the class with too high a digit count as stack space requirements can grow out of control. If that represents a problem then providing an allocator as a template parameter causescpp_bin_float
to dynamically allocate the memory it needsLet me check the documentation for the most popular backends:
gmp's
mpf_float
adheres to this:typedef number<gmp_float<0> > mpf_float;
Type gmp_float can be used at fixed precision by specifying a non-zero Digits10 template parameter, or at variable precision by setting the template argument to zero.
The
typedef mpf_float
provides a variable precision type whose precision can be controlled via the numbers member functions.Similar for MPFR backends:
Type
mpfr_float_backend
can be used at fixed precision by specifying a non-zero Digits10 template parameter, or at variable precision by setting the template argument to zero.The typedef mpfr_float provides a variable precision type whose precision can be controlled via the numbers member functions.
A sample of the dynamic precision usage:
// Operations at variable precision and no numeric_limits support: mpfr_float a = 2; mpfr_float::default_precision(1000); std::cout << mpfr_float::default_precision() << std::endl; std::cout << sqrt(a) << std::endl; // print root-2
It will leave
numeric_limits
undefined这篇关于如何在运行时数precision用的boost ::多precision改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!