When compiling this:
constexpr double x {123.0};
constexpr double y = x / 0.0;
std::cout << x << " / 0 = " << y << "\n";
The compiler (gcc 4.9.2, -std=c++11 or c++14) fails, giving error:
(1.23e+2 / 0.0)' is not a constant expression
constexpr double y = x / 0.0;
How is the result (Inf) relevant when deciding if y
can be a constexpr or not?
For reference, this seems to be the way to do it:
static constexpr double z = std::numeric_limits<double>::quiet_NaN();
static constexpr double w = std::numeric_limits<double>::infinity();
Infinity is an implementation defined result, the standard does not require IEEE floating point and division by zero is formally undefined behavior and constant expression have an exclusion for undefined behavior.
From the draft C++ standard section 5.6
[expr.mul]:
The binary / operator yields the quotient, and the binary % operator yields the remainder from the division of the first expression by the second. If the second operand of / or % is zero the behavior is undefined.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With