Jay*_*Jay 2 c++ boost mpfr gmp boost-multiprecision
我试图弄清楚如何使用 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 << 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;
}
Run Code Online (Sandbox Code Playgroud)
文档说:
所有转换均由底层 MPFR 库执行
然而 mpfr float 后端的文档指出:
使用此类型时您应该了解的事项:
- 默认构造的 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);
}
Run Code Online (Sandbox Code Playgroud)
但是,这不会“设置”“RoundingMode”。相反,它将一个值复制到临时值,如果需要,使用该舍入模式,然后忘记临时值。
那是……没有有效地做任何事情。
所以...
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;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,一切都很好。肉在
std::string s = r.str(d, os.flags());
Run Code Online (Sandbox Code Playgroud)
该str(...)实现从前端 ( number<>) 中继到后端,并最终执行以下操作(以及大量不同的操作):
char* ps = mpfr_get_str(0, &e, 10, static_cast<std::size_t>(digits), m_data, GMP_RNDN);
Run Code Online (Sandbox Code Playgroud)
因此,我们有它。它只是硬编码的。
如果您愿意做出一些假设,您可以编写自己的简化输出例程(见下文),但实际上,如果只是为了显示,我怀疑您是否会如此关心舍入。
假设性能不是首要考虑的问题(因为无论如何它都是字符串 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;
}
}
Run Code Online (Sandbox Code Playgroud)
印刷
1.54687500000000000000
1.54687500000000000000
1.55468750000000000000
1.54687500000000000000
1.55468750000000000000
1.55468750000000000000
1.55468750000000000000
Run Code Online (Sandbox Code Playgroud)
让我们重新实现字符串化:
#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;
}
}
Run Code Online (Sandbox Code Playgroud)
印刷
-- 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
Run Code Online (Sandbox Code Playgroud)
免责声明:我最初删除了一些代码来删除科学记数法支持,因此事情可能不会 100% 达到标准。另外,未使用次正规、无穷大、nan 进行测试。青年MMV
如果确实不是关于表示而是对内存中的数字进行四舍五入,那么您可以从字符串表示构造一个新的 mpfr_float 。
在这种情况下,我的期望实际上是您首先想要一个十进制浮点数(cpp_dec_float例如)。
| 归档时间: |
|
| 查看次数: |
520 次 |
| 最近记录: |