我正在将 boost::multiprecision 库用于十进制浮点类型,并希望将两个浮点数与指定的精度进行比较。
但是, cpp_dec_float 似乎将数字与指定的精度进行比较,但还包括保护数字:
#include <iostream>
#include <boost/multiprecision/cpp_dec_float.hpp>
//#include <boost/math/special_functions.hpp>
typedef boost::multiprecision::number<boost::multiprecision::cpp_dec_float<50> > flp_type;
int main(int argc, char* argv[])
{
// 50 decimal digits
flp_type sqrt2("1.4142135623730950488016887242096980785696718753769");
// Contains calculated guard digits
flp_type result(boost::multiprecision::sqrt(flp_type("2")));
// The 50 digits of precision actually ompare equal
std::cout << std::setprecision(50) << sqrt2 << std::endl;
std::cout << std::setprecision(50) << result << std::endl;
// I want this to compare to the specified precision of the type, not the guard digits
std::cout << (result==sqrt2) << std::endl;
return 0;
}
输出:
1.4142135623730950488016887242096980785696718753769
1.4142135623730950488016887242096980785696718753769
0
预期的:
1.4142135623730950488016887242096980785696718753769
1.4142135623730950488016887242096980785696718753769
1
我试图用precision()“截断”,但无济于事。有没有办法在不诉诸 epsilon 比较的情况下比较这两个数字?