【发布时间】:2016-01-07 17:30:34
【问题描述】:
查看名称和 Boost Multiprecision documentation 我预计 cpp_dec_float_50 数据类型的精度为 50 位小数:
使用 typedef cpp_dec_float_50 隐藏了多精度的复杂性,允许我们定义具有 50 位精度的变量,就像内置的 double 一样。
(虽然我不明白与 double 的比较——我的意思是 double 通常实现二进制浮点运算,而不是十进制浮点运算。)
这也与以下代码的输出相匹配(除了双精度部分,但这是预期的):
cout << std::numeric_limits<boost::multiprecision::cpp_dec_float_50>::digits10
<< '\n';
// -> 50
cout << std::numeric_limits<double>::digits10 << '\n';
// -> 15
但是为什么下面的代码会打印 74 位呢?
#include <boost/multiprecision/cpp_dec_float.hpp>
// "12" repeated 50 times, decimal point after the 10th digit
boost::multiprecision::cpp_dec_float_50 d("1212121212.121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212121212");
cout << d.convert_to<string>() << '\n';
// Expected output: 50 digits
// Actual output: 74 digits
// -> 1212121212.1212121212121212121212121212121212121212121212121212121212121212
str() 成员函数按预期工作,例如
cout << d.str(50) << '\n';
只打印 50 位数字 - 其中it is documented as:
返回格式化为字符串的数字,至少包含精度数字,如果科学为真,则返回科学格式。
【问题讨论】:
标签: c++ boost floating-point precision multiprecision