boost :: multiprecision :: cpp_dec_float_50溢出检查 [英] boost::multiprecision::cpp_dec_float_50 overflow checking

查看:214
本文介绍了boost :: multiprecision :: cpp_dec_float_50溢出检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用boost :: multiprecision库进行浮点(或在这种情况下为固定的)点算术. 但是,我无法通过以下方式检测到潜在的溢出:

I am trying to use the boost::multiprecision library for floating (or in that case, fixed) point arithmetic. However, I am having trouble detecting potential overflow in the following way:

typedef boost::multiprecision::number<
                                      boost::multiprecision::cpp_dec_float<50>
                                     > flp_type;
typedef boost::multiprecision::number<
                                      boost::multiprecision::cpp_dec_float<100>
                                     > safe_flp_type;

flp_type _1 = std::numeric_limits<flp_type>::max();
flp_type _2("1");
flp_type _3 = std::numeric_limits<flp_type>::max();
flp_type dtNew;

// Here is the check
safe_flp_type _res = safe_flp_type(_1) + _2;

// **This condition is true for addition of _1 and _3,**
// but fails for _1 + _2
if(  (_res > std::numeric_limits<flp_type>::max())  // overflow
   ||(_res < std::numeric_limits<flp_type>::min())) // underflow
{
    BOOST_THROW_EXCEPTION(OverUnderflow() << SpecificErrInfo(L"Attempted floating point over/underflow"));
}

dtNew = _1 + _2;

甚至不应该为类型的max()加1触发抛出异常? 我还检查了溢出后的基础类型,它不是cpp_dec_float_inf,仍然是cpp_dec_float_finite. 另外,dtNew的值等于std :: numeric_limits :: max()

Shouldn't even adding 1 to max() for the type trigger the throw of the exception? I have also checked the underlying type after the overflow, and it's not cpp_dec_float_inf, still cpp_dec_float_finite. Also, the value of dtNew is equal to std::numeric_limits::max()

我在这里对概念有完全的误解吗?如果是这样,防止boost :: multiprecision :: cpp_dec_float< 50>溢出的正确方法是什么?

Am I under a complete conceptual misapprehension here? If so, what would be the correct way to prevent a boost::multiprecision::cpp_dec_float<50> from overflowing?

推荐答案

好,我已调试到库中,并且在此行上发生了错误":

Ok, I have debugged into the library, and the "error" happens on this line:

// Check if the operation is out of range, requiring special handling.
if(v.iszero() || (ofs_exp > max_delta_exp))
{
   // Result is *this unchanged since v is negligible compared to *this.
   return *this;
}

在类型的numeric_limit中加1可以忽略不计,因此加法被丢弃.因此不是> =.

Adding 1 to the numeric_limit of the type is negligible, so the addition is discarded. Hence it is not >=.

我给人的印象是,该类型被实现为定点(愚蠢,考虑到名称,我知道),但事实并非如此. 这来自增强 doc

I was under the impression that the type was implemented as fixed point (dumb considering the name, I know), which it is not. This is from the boost doc

涉及cpp_dec_float的操作总是被截断.但是,请注意,由于它们实际上是保护位,因此实际上在大多数使用情况下,这对准确性没有真正的影响.

Operations involving cpp_dec_float are always truncating. However, note that since their are guard digits in effect, in practice this has no real impact on accuracy for most use cases.

遗憾的是,多精度库似乎没有固定的精度类型.

Bummer that the multiprecision library doesn't seem to have a fixed precision type.

但是,为了检查cpp_dec_float中的溢出,可以执行以下操作:

However, in order to check for overflow in cpp_dec_float, one can do this:

dtNew = _1 * _2;
if(dtNew.backend().isinf())
{
    BOOST_THROW_EXCEPTION(OverUnderflow() << SpecificErrInfo(L"Attempted floating point over/underflow"));
}

这篇关于boost :: multiprecision :: cpp_dec_float_50溢出检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆