c++ - 检测纯 C++ 中的精度损失

标签 c++ gcc g++

当您使用 float (floatdoublelong double类型)?说:

template< typename F >
F const sum(F const & a, F const & b)
{
    F const sum_(a + b);
    // The loss of precision must be detected (here or one line above) if some fraction bit is lost due to rounding
    return sum_;
}

特别关注目标架构上存在 x87 FPU,但没有 asm 例程干预纯 C++ 代码的情况。 C++11gnu++11 特定功能也接受(如果有的话)。

最佳答案

C++ 标准对浮点精度的概念非常模糊。没有完全符合标准的方法来检测精度损失。

GNU provides an extension启用浮点异常。您想要捕获的异常是 FE_INEXACT

关于c++ - 检测纯 C++ 中的精度损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14418692/

相关文章:

c - 这是否违反了严格的别名规则?

C++ 函数属性指示返回值的生命周期与参数相同

c++ - 链表的链表的析构函数

c++ - 为什么 std::string 的 back() 应该返回对 char 的引用?

C++ 11在缺少时提供空的非成员函数

C++98 花括号 const 标量初始化

c - 哪个 C99 编译器(Clang 与 GCC)更接近 const 结构字段的标准?

debugging - 找不到 -lasan 和 libasan_preinit.o

c++ - 使用用较新版本的 g++ 编译的共享对象

c++ - 在编译时解析 (u)int_fastX_t