Is it possible to detect the loss of precision, when you operate with floating-point numbers (of float, double, long double types)? Say:
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_;
}
Especially intrested in case of when x87 FPU is present on target architecture, but without the intervention of the asm routines into the pure C++ code. C++11 or gnu++11 specific features also accepted if any.
The C++ standard is very vague on the concept of floating point precision. There is no fully standard-conforming way to detect precision loss.
GNU provides an extension to enable floating-point exceptions. The exception you would want to trap is
FE_INEXACT.