C++ boost::多精度::cpp\u dec\u float\u 50溢出检查
我正在尝试将boost::multiprecision库用于浮点(或者在这种情况下是定点)算法。 但是,我无法通过以下方式检测潜在溢出:C++ boost::多精度::cpp\u dec\u float\u 50溢出检查,c++,boost,floating-point,overflow,numeric-limits,C++,Boost,Floating Point,Overflow,Numeric Limits,我正在尝试将boost::multiprecision库用于浮点(或者在这种情况下是定点)算法。 但是,我无法通过以下方式检测潜在溢出: typedef boost::multiprecision::number< boost::multiprecision::cpp_dec_float<50> > flp_type; ty
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;
typedef boost::multiprecision::number<
boost::多精度::cpp\u dec\u浮点
>flp_型;
typedef boost::多精度::数字<
boost::多精度::cpp\u dec\u浮点
>安全型;
flp_类型_1=std::numeric_limits::max();
flp_类型2(“1”);
flp_类型_3=std::numeric_limits::max();
flp_型dtNew;
//这是支票
safe_flp_type _res=safe_flp_type(+u 1)+u 2;
//**此条件适用于添加_1和_3**
//但对于_1+_2失败
如果((\u res>std::numeric\u limits::max())//溢出
||(\u res BOOST_THROW_异常(OverUnderflow()好的,我已经调试到库中,并且“错误”发生在这一行:
// 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;
}
将1添加到类型的数值_限制可以忽略不计,因此该添加将被丢弃。因此它不是>=
我的印象是,该类型是作为固定点实现的(考虑到名称,我知道它是哑的),而事实并非如此。
这是来自助推
涉及cpp_dec_float的操作总是被截断。但是,请注意,由于它们实际上是保护数字,因此在实践中,对于大多数用例,这对准确性没有实际影响
多精度库似乎没有固定的精度类型,这让人恼火
但是,为了检查cpp_dec_float中的溢出,可以执行以下操作:
dtNew = _1 * _2;
if(dtNew.backend().isinf())
{
BOOST_THROW_EXCEPTION(OverUnderflow() << SpecificErrInfo(L"Attempted floating point over/underflow"));
}
dtNew=\u1*\u2;
if(dtNew.backend().isinf())
{
BOOST\u THROW\u异常(上下溢()