查看名称和Boost Multiprecision 文档,我希望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 位数字 -它被记录为:
返回格式化为字符串的数字,至少包含精度数字,如果科学为真,则返回科学格式。