【发布时间】:2014-05-31 00:37:17
【问题描述】:
我正在测试构造 gmp_float 的字符串。
这段代码
#include <boost/multiprecision/number.hpp>
#include <boost/multiprecision/gmp.hpp>
#include <iostream>
using namespace boost::multiprecision;
typedef number<gmp_float<15>> mp_type;
int main()
{
mp_type total("1.01");
cout << total.str(0) << endl;
mp_type first_addition(".01");
cout << first_addition.str(0) << endl;
total += first_addition;
cout << total.str(0) << endl;
}
打印
1.01
0.01
1.01999999999999999998
为什么?我进行了更多测试,在这种特殊情况下,只要一个数字的大小是 >0 和 1,那么操作是什么并不重要。
从上面的链接
不可能将这种类型的对象往返于字符串之间并返回完全相同的值。这似乎是 GMP 的限制。
是否还有其他区域会丢失准确性?
【问题讨论】:
-
即使
gmp_float被宣传为具有“更高的精度”,我怀疑精度是无限的。尤其是在0附近。 -
Here我已经为那些感兴趣的人复制了这个问题。
-
离 [0.0,1.0] 越远,浮点数越容易受到精度问题的影响。如果您希望在可表示的整个值范围内具有相同的精度,请改用定点。顺便说一句,您甚至不能将 0.01 表示为浮点中的二次幂。
标签: c++ string boost gmp arbitrary-precision