floating point division by 0.0 and -ftrapv

Hi all,

I know that integer div by 0 is undefined behaviour, but is fp div by zero also undefined? I've STFW a lot but find conflicting info.

CERT says:


"Floating-point division by zero also results in undefined behavior" but then their "Compliant Solution" example does it anyway and then tests with fetestexcept().

Mind you, building their example also says: "warning: pragma STDC FENV_ACCESS ON is not supported, ignoring pragma".

I'm asking because clang with -ftrapv dies upon fp div by 0. Is this correct or not?


C++98 5.6 Multiplicative operators [expr.mul]p4 says , "If the second operand of / or % is zero the behavior is undefined". It's the same in C++11 N3242. Floating point division is not distinguished from integral division, and as floating point operations' precision and such aren't defined, it's not surprising (although it's certainly unfortunate at times) that division by 0.0f or 0.0d isn't distinguished.


Our intent is to implement IEEE floating point semantics. Giving FP-divide-by-zero undefined behavior would be inconsistent with that. LLVM does not optimize based on this, and -ftrapv should not trap on it.

This can be considered a case of intentionally defining the behavior of something left undefined by the standard.



I have created this bug:
<11854 – floating point division by 0.0 vs -ftrapv vs IEEE floating point semantics;