cpp_dec_float_50的精度是多少?
What is the precision of cpp_dec_float_50?
查看名称和 Boost Multiprecision documentation 我希望 cpp_dec_float_50
数据类型的精度为 50 位小数:
Using typedef cpp_dec_float_50 hides the complexity of multiprecision to allow us to define variables with 50 decimal digit precision just like built-in 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:
Returns the number formatted as a string, with at least precision digits, and in scientific format if scientific is true.
您所看到的可能与内部使用的保护数字有关。原因是即使是十进制表示也具有有限的准确性(想想(“100.0”/“3.0”)*“3.0”)。
为了在计算时得到合理的舍入误差,存储精度会大于保证精度。
总而言之:始终具体说明您的预期精度。在你的例子中 d.str(50)
就可以了。
(In realistic scenarios, you should want to track the precision of your inputs and deduce the precision on your outputs. Most often, people just reserve surplus precision and only print the part they're interested in)
查看名称和 Boost Multiprecision documentation 我希望 cpp_dec_float_50
数据类型的精度为 50 位小数:
Using typedef cpp_dec_float_50 hides the complexity of multiprecision to allow us to define variables with 50 decimal digit precision just like built-in 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:
Returns the number formatted as a string, with at least precision digits, and in scientific format if scientific is true.
您所看到的可能与内部使用的保护数字有关。原因是即使是十进制表示也具有有限的准确性(想想(“100.0”/“3.0”)*“3.0”)。
为了在计算时得到合理的舍入误差,存储精度会大于保证精度。
总而言之:始终具体说明您的预期精度。在你的例子中 d.str(50)
就可以了。
(In realistic scenarios, you should want to track the precision of your inputs and deduce the precision on your outputs. Most often, people just reserve surplus precision and only print the part they're interested in)