IEEE-754 Floating-point Exceptions in C -
i writing floating-point calculator-interface, in c, allows mathematical functions defined in math.h accessed @ runtime. interface implemented function behaves strtold(). based on ascii , supposed portable ascii in order true need handle floating-points in portable way possible. happy limiting support ieee-754 floating-points not sure how handle exceptions defined ieee-754 (overflow, underflow, etc.). first of pretty sure way check exceptions work in rounding modes checking status flags themselves; in order need fenv.h (defined in annex f of c99) want know how portable fenv.h in practice. not understand how fenv.h supposed work; appears me status flags centralized whatever reason under impression each floating-point had flags built-in. know c99 says functions defined in math.h may overflow , underflow not understand how supposed check these exceptions. summarize looking example of how use fenv.h check overflow caused multiplication , explanation of how error check functions defined in math.h.
in theory, following function multiplies 2 numbers, returning true
if overflow occurred , false
if not:
bool mul(double &a, double b) { feclearexcept(fe_overflow); *= b; return fetestexcept(fe_overflow) != 0; }
the standard states need #pragma fenv_access on
in order use this. however, have yet use compiler cares pragma or compiler knows floating-point multiplication has side effect reflected in exception flags---both gcc , clang happily "optimise away" "dead" floating-point operation. gcc bug 34678 concerns behaviour , imagine there's similar bug against clang. caveat applies use of rounding modes other round-to-nearest-breaking-ties-to-even in program.
Comments
Post a Comment