The behaviour of floating point division by zero

C++ standard does not force the IEEE 754 standard, because that depends mostly on hardware architecture.

If the hardware/compiler implement correctly the IEEE 754 standard, the division will provide the expected INF, -INF and NaN, otherwise… it depends.

Undefined means, the compiler implementation decides, and there are many variables to that like the hardware architecture, code generation efficiency, compiler developer laziness, etc..


The C++ standard state that a division by 0.0 is undefined

C++ Standard 5.6.4

… If the second operand of / or % is zero the behavior is undefined

C++ Standard

…static constexpr bool is_iec559;

…56. True if and only if the type adheres to IEC 559 standard.217

…57. Meaningful for all floating point types.

C++ detection of IEEE754:

The standard library includes a template to detect if IEEE754 is supported or not:

static constexpr bool is_iec559;

#include <numeric>
bool isFloatIeee754 = std::numeric_limits<float>::is_iec559();

What if IEEE754 is not supported?

It depends, usually a division by 0 trigger a hardware exception and make the application terminate.

Leave a Comment