简体   繁体   English

boost :: multiprecision :: cpp_dec_float_50溢出检查

[英]boost::multiprecision::cpp_dec_float_50 overflow checking

I am trying to use the boost::multiprecision library for floating (or in that case, fixed) point arithmetic. 我正在尝试使用boost :: multiprecision库进行浮点(或在这种情况下为固定的)点算术。 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;

Shouldn't even adding 1 to max() for the type trigger the throw of the exception? 甚至不应该为类型的max()加1引发异常抛出? I have also checked the underlying type after the overflow, and it's not cpp_dec_float_inf, still cpp_dec_float_finite. 我还检查了溢出后的基础类型,它不是cpp_dec_float_inf,仍然是cpp_dec_float_finite。 Also, the value of dtNew is equal to std::numeric_limits::max() 同样,dtNew的值等于std :: numeric_limits :: max()

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? 如果是这样,防止boost :: multiprecision :: cpp_dec_float <50>溢出的正确方法是什么?

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

Adding 1 to the numeric_limit of the type is negligible, so the addition is discarded. 将1与类型的numeric_limit相加可以忽略不计,因此该相加将被丢弃。 Hence it is not >=. 因此它不是> =。

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 这是来自boost 文档

Operations involving cpp_dec_float are always truncating. 涉及cpp_dec_float的操作总是被截断。 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. 可以理解,多精度库似乎没有固定的精度类型。

However, in order to check for overflow in cpp_dec_float, one can do this: 但是,为了检查cpp_dec_float中的溢出,可以执行以下操作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Boost.Multiprecision cpp_dec_float_50 - 转换成字节数组并返回? - Boost.Multiprecision cpp_dec_float_50 - convert into an array of bytes and back? 如何将boost :: multiprecision :: cpp_int转换为cpp_dec_float <0>(而不是cpp_dec_float_50等)? - How to convert from boost::multiprecision::cpp_int to cpp_dec_float<0> (rather than to cpp_dec_float_50, etc.)? cpp_dec_float_50 的精度是多少? - What is the precision of cpp_dec_float_50? 将 boost::multiprecision::cpp_dec_float_100 转换为具有精度的字符串 - Convert boost::multiprecision::cpp_dec_float_100 to string with precision 从整数转换为cpp_dec_float的boost :: multiprecision编译错误 - boost::multiprecision cast from integer to cpp_dec_float compile error 不使用 cpp_bin_float_(50/100) 时提升多精度 cpp_float 无法正常工作 - Boost multiprecision cpp_float not working properly when not using cpp_bin_float_(50/100) 在cpp_dec_float中使用pow函数进行增强 - Boost Using pow function with cpp_dec_float C ++:Boost cpp_dec_float独立吗? - C++: Boost cpp_dec_float Independent? 浮点数运算使用boost :: multiprecision中的cpp_bin_float出错 - Floating point math going wrong using cpp_bin_float from boost::multiprecision 如何有效地转换大量的提升多精度(cpp_bin_float)? - How to convert huge numbers of boost multiprecision (cpp_bin_float) efficiently?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM