[cfe-dev] floating point division by 0.0 and -ftrapv

John McCall rjmccall at apple.com
Sat Dec 31 20:19:36 PST 2011


On Dec 23, 2011, at 7:00 PM, Jeff Walden wrote:
> On 12/23/2011 12:56 PM, Sean McBride wrote:
>> I know that integer div by 0 is undefined behaviour, but is fp div by zero also undefined?
> 
> 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.

John.



More information about the cfe-dev mailing list