使用 boost lib 的更高精度浮点数(高于 16 位数字)
higher precision floating point using boost lib (higher then 16 digits)
我正在运行模拟物理实验,所以我需要非常高的浮点精度(超过 16 位)。我使用 Boost.Multiprecision,但是无论我尝试什么,我都无法获得高于 16 位的精度。我运行用C++和eclipse编译器模拟,例如:
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <limits>
using boost::multiprecision::cpp_dec_float_50;
void main()
{
cpp_dec_float_50 my_num= cpp_dec_float_50(0.123456789123456789123456789);
std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
std::cout << my_num << std::endl;
}
输出为:
0.12345678912345678379658409085095627233386039733887
^
但应该是:
0.123456789123456789123456789
如您所见,16 位数字后是不正确的。为什么?
问题是 C++ 编译器在编译时将数字转换为双精度数(我也在 this a while ago). You have to use special functions to handle more decimal points. See the Boost documentation or other 这里举个例子。
也就是说,几乎不可能真正需要如此高的精度。如果您正在失去精度,您应该考虑其他浮点算法,而不是盲目地增加小数位数。
您的问题在这里:
cpp_dec_float_50 my_num = cpp_dec_float_50(0.123456789123456789123456789);
^ // This number is a double!
编译器不使用任意精度的浮点文字,而是使用具有有限精度的 IEEE-754 doubles。在这种情况下,最接近您所写数字的 double
是:
0.1234567891234567837965840908509562723338603973388671875
并将其打印到小数点后第 50 位确实给出了您正在观察的输出。
您想要的是从字符串构造任意精度浮点数 (demo):
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <limits>
using boost::multiprecision::cpp_dec_float_50;
int main() {
cpp_dec_float_50 my_num = cpp_dec_float_50("0.123456789123456789123456789");
std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
std::cout << my_num << std::endl;
}
输出:
0.123456789123456789123456789
我正在运行模拟物理实验,所以我需要非常高的浮点精度(超过 16 位)。我使用 Boost.Multiprecision,但是无论我尝试什么,我都无法获得高于 16 位的精度。我运行用C++和eclipse编译器模拟,例如:
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <limits>
using boost::multiprecision::cpp_dec_float_50;
void main()
{
cpp_dec_float_50 my_num= cpp_dec_float_50(0.123456789123456789123456789);
std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
std::cout << my_num << std::endl;
}
输出为:
0.12345678912345678379658409085095627233386039733887
^
但应该是:
0.123456789123456789123456789
如您所见,16 位数字后是不正确的。为什么?
问题是 C++ 编译器在编译时将数字转换为双精度数(我也在
也就是说,几乎不可能真正需要如此高的精度。如果您正在失去精度,您应该考虑其他浮点算法,而不是盲目地增加小数位数。
您的问题在这里:
cpp_dec_float_50 my_num = cpp_dec_float_50(0.123456789123456789123456789);
^ // This number is a double!
编译器不使用任意精度的浮点文字,而是使用具有有限精度的 IEEE-754 doubles。在这种情况下,最接近您所写数字的 double
是:
0.1234567891234567837965840908509562723338603973388671875
并将其打印到小数点后第 50 位确实给出了您正在观察的输出。
您想要的是从字符串构造任意精度浮点数 (demo):
#include <boost/math/constants/constants.hpp>
#include <boost/multiprecision/cpp_dec_float.hpp>
#include <iostream>
#include <limits>
using boost::multiprecision::cpp_dec_float_50;
int main() {
cpp_dec_float_50 my_num = cpp_dec_float_50("0.123456789123456789123456789");
std::cout.precision(std::numeric_limits<cpp_dec_float_50>::digits10);
std::cout << my_num << std::endl;
}
输出:
0.123456789123456789123456789