Integer division by zero
suggest changeint x = 5 / 0; // Undefined behavior
Division by 0 is mathematically undefined, and as such it makes sense that this is undefined behavior.
However:
float x = 5.0f / 0.0f; // +infinity on IEEE-754; otherwise undefined
Most implementations use IEEE-754. Under that format, floating-point division by zero returns NaN when the numerator is 0.0f, infinity when the numerator is positive, or -infinity when the numerator is negative. The C++ standard still classifies division by zero as undefined behavior when that format is not in effect.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents