如何在使用 MPFR 时在提升多精度中设置舍入模式

How to set rounding mode in Boost Multiprecision when using MPFR

本文关键字:精度 舍入 模式 设置 MPFR      更新时间:2023-10-16

我正在尝试弄清楚如何使用 Boost 多精度中的舍入模式格式化mpfr_float数字。在下面的示例中,我希望 1.55 会四舍五入为 1.5 或 1.6,具体取决于所使用的舍入模式,但对于所有情况,它都会输出 1.5。如何在 Boost with MPFR 中实现这个简单的功能?

#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 << endl;
setRoundingMode(a, MPFR_RNDU); /* round toward +Inf */
cout << setprecision(2) << a << endl;
setRoundingMode(a, MPFR_RNDD); /* round toward -Inf */
cout << setprecision(2) << a << endl;
setRoundingMode(a, MPFR_RNDA); /* round away from zero */
cout << setprecision(2) << a << endl;
setRoundingMode(a, MPFR_RNDF); /* faithful rounding */
cout << setprecision(2) << a << endl;
setRoundingMode(a, MPFR_RNDNA); /* round to nearest, with ties away from zero (mpfr_round) */
cout << setprecision(2) << a << endl;
return 0;
}

文档说:

所有转换均由底层 MPFR 库执行

但是,mpfr 浮点后端的文档指出:

使用此类型时应了解的事项:

  • 默认构造mpfr_float_backend设置为零(请注意 这不是默认的 MPFR 行为(。
  • 所有操作都使用舍入到最接近

(强调我的(

我发现 MPFR 没有全局默认舍入覆盖,因此只有特定的操作(赋值和精度更改(需要mpfr_rnd_t

你意识到了这一点,因此你的:

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);
}

但是,这不会"设置"舍入模式"。相反,它会将值复制到临时值,如果需要,请使用该舍入模式,然后忘记临时值。

那是。。。没有有效地做任何事情。

所以。。。

IO 如何工作?

operator<<调用:

template <class Backend, expression_template_option ExpressionTemplates>
inline std::ostream& operator<<(std::ostream& os, const number<Backend, ExpressionTemplates>& r)
{
std::streamsize d  = os.precision();
std::string     s  = r.str(d, os.flags());
std::streamsize ss = os.width();
if (ss > static_cast<std::streamsize>(s.size()))
{
char fill = os.fill();
if ((os.flags() & std::ios_base::left) == std::ios_base::left)
s.append(static_cast<std::string::size_type>(ss - s.size()), fill);
else
s.insert(static_cast<std::string::size_type>(0), static_cast<std::string::size_type>(ss - s.size()), fill);
}
return os << s;
}

目前为止,一切都好。肉在

std::string     s  = r.str(d, os.flags());

str(...)实现从前端(number<>(中继到后端,并最终完成(在大量不同的事情中(:

char* ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);

所以我们有了它。它只是硬编码。

如果你愿意做一些假设,你可以编写自己的简化输出例程(见下文(,但实际上我怀疑你是否如此关心舍入,如果它只是为了显示。

假设性能不是主要关注点(因为它无论如何都是字符串 IO,很少是快速或必需的(,我将假设能够实际获得四舍五入的数字更有价值,因此您可以使用现有的库工具打印它。

什么是四舍五入,到底是什么?

MPFR 中的舍入不是您想象的那样:它不会舍入到小数位。它在表示形式中舍入为二进制数字。

让我们通过实现 MPFR 舍入方式来演示这一点:

住在科里鲁

#include <iostream>
#include <boost/multiprecision/mpfr.hpp>
namespace bmp = boost::multiprecision;
namespace detail {
template <
unsigned srcDigits10, bmp::mpfr_allocation_type srcAlloc,
unsigned dstDigits10, bmp::mpfr_allocation_type dstAlloc
>
void round(
bmp::mpfr_float_backend<srcDigits10, srcAlloc> const& src, 
mpfr_rnd_t r,
bmp::mpfr_float_backend<dstDigits10, dstAlloc>& dst)
{
mpfr_set(dst.data(), src.data(), r);
}
template <unsigned dstDigits10, unsigned srcDigits10, bmp::mpfr_allocation_type alloc>
auto round(bmp::mpfr_float_backend<srcDigits10, alloc> const& src, mpfr_rnd_t r) {
bmp::mpfr_float_backend<dstDigits10, alloc> dst;
round(src, r, dst);
return dst;
}
}
template <unsigned dstDigits10, typename Number>
auto round(Number const& src, mpfr_rnd_t r) {
auto dst = detail::round<dstDigits10>(src.backend(), r);
return bmp::number<decltype(dst)>(dst);
}
int main() {
using bmp::mpfr_float;
mpfr_float::default_precision(50);
mpfr_float const a("1.55");
std::cout << std::setprecision(20) << std::fixed;
for (mpfr_rnd_t r : {
MPFR_RNDN, /* round to nearest, with ties to even */
MPFR_RNDZ, /* round toward zero */
MPFR_RNDU, /* round toward +Inf */
MPFR_RNDD, /* round toward -Inf */
MPFR_RNDA, /* round away from zero */
MPFR_RNDF, /* faithful rounding */
MPFR_RNDNA, /* round to nearest, with ties away from zero (mpfr_round) */
})
{
std::cout << round<2>(a, r) << std::endl;
}
}

指纹

1.54687500000000000000
1.54687500000000000000
1.55468750000000000000
1.54687500000000000000
1.55468750000000000000
1.55468750000000000000
1.55468750000000000000

那么,我们期待什么呢?

让我们重新实现字符串化:

住在科里鲁

#include <iostream>
#include <boost/multiprecision/mpfr.hpp>
namespace bmp = boost::multiprecision;
template <unsigned srcDigits10, bmp::mpfr_allocation_type alloc>
auto to_string(bmp::mpfr_float_backend<srcDigits10, alloc> const& src,
unsigned digits, mpfr_rnd_t r, std::ios::fmtflags fmtflags) {
std::streamsize org_digits(digits);
std::string result;
mpfr_exp_t e = 0;
char* ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits),
src.data(), r);
--e; // To match with what our formatter expects.
if (e != -1) {
// Oops we actually need a different number of digits to what we asked
// for:
mpfr_free_str(ps);
digits += e + 1;
if (digits == 0) {
// We need to get *all* the digits and then possibly round up,
// we end up with either "0" or "1" as the result.
ps = mpfr_get_str(0, &e, 10, 0, src.data(), r);
--e;
unsigned offset = *ps == '-' ? 1 : 0;
if (ps[offset] > '5') {
++e;
ps[offset] = '1';
ps[offset + 1] = 0;
} else if (ps[offset] == '5') {
unsigned i = offset + 1;
bool round_up = false;
while (ps[i] != 0) {
if (ps[i] != '0') {
round_up = true;
break;
}
++i;
}
if (round_up) {
++e;
ps[offset] = '1';
ps[offset + 1] = 0;
} else {
ps[offset] = '0';
ps[offset + 1] = 0;
}
} else {
ps[offset] = '0';
ps[offset + 1] = 0;
}
} else if (digits > 0) {
mp_exp_t old_e = e;
ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits),
src.data(), r);
--e; // To match with what our formatter expects.
if (old_e > e) {
// in some cases, when we ask for more digits of precision, it
// will change the number of digits to the left of the decimal,
// if that happens, account for it here. example: cout << fixed
// << setprecision(3) << mpf_float_50("99.9809")
digits -= old_e - e;
ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits),
src.data(), r);
--e; // To match with what our formatter expects.
}
} else {
ps = mpfr_get_str(0, &e, 10, 1, src.data(), r);
--e;
unsigned offset = *ps == '-' ? 1 : 0;
ps[offset] = '0';
ps[offset + 1] = 0;
}
}
result = ps ? ps : "0";
if (ps)
mpfr_free_str(ps);
bmp::detail::format_float_string(result, e, org_digits, fmtflags,
0 != mpfr_zero_p(src.data()));
return result;
}
template <unsigned srcDigits10, bmp::mpfr_allocation_type alloc>
auto to_string(
bmp::number<bmp::mpfr_float_backend<srcDigits10, alloc>> const& src,
unsigned digits, mpfr_rnd_t r,
std::ios::fmtflags fmtflags = std::ios::fixed) {
return to_string(src.backend(), digits, r, fmtflags);
}
int main() {
using bmp::mpfr_float;
mpfr_float::default_precision(50);
mpfr_float const a("1.55");
std::cout << std::setprecision(20) << std::fixed;
for (mpfr_rnd_t r : {
MPFR_RNDN, /* round to nearest, with ties to even */
MPFR_RNDZ, /* round toward zero */
MPFR_RNDU, /* round toward +Inf */
MPFR_RNDD, /* round toward -Inf */
MPFR_RNDA, /* round away from zero */
MPFR_RNDF, /* faithful rounding */
MPFR_RNDNA, /* round to nearest, with ties away from zero (mpfr_round) */
})
{
std::cout
<< " -- " << to_string(a, 2, r)
<< ", " << to_string(a, 1, r)
<< " -- "  <<  to_string(a, 2, r, std::ios::scientific)
<< ", "  <<  to_string(a, 1, r, std::ios::scientific) << std::endl;
}
}

指纹

-- 1.55, 1.5 -- 1.55e+00, 1.5e+00
-- 1.54, 1.5 -- 1.54e+00, 1.5e+00
-- 1.55, 1.6 -- 1.55e+00, 1.6e+00
-- 1.54, 1.5 -- 1.54e+00, 1.5e+00
-- 1.55, 1.6 -- 1.55e+00, 1.6e+00
-- 1.55, 1.5 -- 1.55e+00, 1.5e+00
-- 1.55, 1.5 -- 1.55e+00, 1.5e+00

免责声明:我最初删除了一些代码以放弃科学记数法支持,因此事情可能不是 100% 达到标准。此外,未测试亚正常,无穷大,nan。扬子晚报

结语

如果确实不是关于表示而是对内存中的数字进行舍入,则可以从字符串表示构造一个新mpfr_float。

在这种情况下,我的期望实际上是您首先想要十进制浮点数(例如cpp_dec_float(。