我试图弄清楚如何使用 Boost Multi precision 中的舍入模式来格式化 mpfr_float 数字。在下面的示例中,我希望 1.55 舍入为 1.5 或 1.6,具体取决于使用的舍入模式,但对于所有情况,它都会输出 1.5。如何使用 MPFR 在 Boost 中实现这个简单的功能?
#include <iostream>
#include <boost/multiprecision/mpfr.hpp>
void setRoundingMode(boost::multiprecision::mpfr_float m, mpfr_rnd_t r)
{
mpfr_t tmp;
mpfr_init(tmp);
mpfr_set(tmp, m.backend().data(), r);
mpfr_clear(tmp);
}
int main()
{
using namespace boost::multiprecision;
using std::cout;
using std::endl;
using std::setprecision;
mpfr_float::default_precision(50);
mpfr_float a("1.55");
setRoundingMode(a, MPFR_RNDN); /* round to nearest, with ties to even */
cout << setprecision(2) << a << endl;
setRoundingMode(a, MPFR_RNDZ); /* round toward zero */
cout << setprecision(2) << a …Run Code Online (Sandbox Code Playgroud)