望着名稱和Boost Multiprecision documentation我會想到的是,cpp_dec_float_50
數據類型具有50位十進制數字的精度:cpp_dec_float_50的精度是多少?
使用的typedef cpp_dec_float_50隱藏了多倍的複雜性,使我們能夠用50十進制數字精度定義變量只是像內置雙。
(雖然我不明白以兩倍的比較 - 我的意思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:
返回格式化爲一個字符串的數字,至少有精確的數字,並在科學格式,如果科學是真實的。
順便說一句,你可以添加該報價的參考? – maxschlepzig
@maxschlepzig我自己說過。應該是一個「側欄」的東西。我指出的邏輯是具有明確誤差的科學測量。 – sehe