当您使用 float (float
、double
、long 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++11 或 gnu++11 特定功能也接受(如果有的话)。
最佳答案
C++ 标准对浮点精度的概念非常模糊。没有完全符合标准的方法来检测精度损失。
GNU provides an extension启用浮点异常。您想要捕获的异常是 FE_INEXACT
。
关于c++ - 检测纯 C++ 中的精度损失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14418692/