I wanted to test the behavior of floats near infinity. For that I naively wrote the following code:
#include <limits>
#include <iostream>
int main() {
constexpr float foo = std::numeric_limits<float>::infinity() - std::numeric_limits<float>::epsilon();
std::cout << foo << std::endl;
return foo;
}
The interesting part to me was that this compiles fine in GCC 7.2 but fails on Clang 5 (complaining about non-constexpr assign of foo
).
AFAIK, since C++11, std::numeric_limits<float>::infinity()
and infinity()
are constexpr
, so I am wondering where the problem lies for Clang.
EDIT 1:
Removed unnecessary static_assert
.
Thanks for pointing to division by 0. IMO the quoted standards text there does not apply here!?
And the obligatory godbolt link: https://godbolt.org/g/Nd5yF9
EDIT 2:
Note that the same behavior applies to:
constexpr float foo = std::numeric_limits<float>::infinity() - 100.0f;