4 ms·
C++14: floats, bits, and constant expressions
- deleted 11y ago[deleted]
- wnoise 11y agoIf signbit() were compatible with constexpr, it would work for negative zero. Similarly, frexp() could determine the exponent, though the interface itself is a problem.
- satbyy 11y agoI think the first example is sorta invalid because there's a typo: return *(uint32_t*)f; should read as: return *(uint32_t*)(&f); With this change, the code indeed compiles fine (with g++ 5.3.1; not sure of 5.3.0, which the author used)
- Kristine1975 11y agoWith clang 3.7.0 this results in "constexpr function never produces a constant expression", since the C-style pointer cast is essentially a reinterpret_cast (C++14 Standard §5.4p4.4), which is not a "core constant expression" (C++14 Standard §5.20p2.13) and thus must not appear in a constexpr function: C++14 Standard §7.1.5p5: ... if no argument values exist such that an invocation of the function or constructor could be an evaluated subexpression of a core constant expression, the program is ill-formed; no diagnostic required. clang seems to go the extra mile and produces a diagnostic. Incidentally the pointer cast would violate C++'s aliasing rules and thus invoke undefined behavior. P.S: The C++14 Standard is available here: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4296.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n429... (never mind the "draft" status)
- satbyy 11y agoInterestingly, g++ gives out a warning (correctly) when compiled with -O2, but not by default (-O0): warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing] So, you're right indeed.
- sebastic 11y agoPlease stop using C-style casts. It's 2016. C++ != C. Unlearn what you have learned.
- TickleSteve 11y ago'idiomatic C++' != C
- coliveira 11y agoIdiomatic C++ is not well defined. There are so many possible ways to write C++ code that you should be skeptical if somebody promotes one version is the true one.
- mike_hock 11y agoThis is illegal (undefined), but casting to char* and reading out the values is legal.
- eps 11y ago> Maybe you want to run a fast inverse square root at compile time. Or maybe you want to do something that is actually useful. Careful now. Making questionable jokes about the work of the great Carmack? Thin ice here, very thin.
- sebastic 11y agoI'm glad you're aware of Carmack's association with the fast inverse square root (hopefully you also know that it doesn't originate from him) but the joke here isn't the denigration of the code itself. The joke is that the original need for a fast inv sqrt is moot at compile-time.
- eps 11y agoAh, compile-time... missed that, sorry. And, no, I didn't know it wasn't a Carmack's code (looked it up just now).
- Dylan16807 11y agoAlso, chips have a dedicated fast inverse square root function these days, much faster than doing the steps yourself.
- twoodfin 11y agoLove this kind of hacking at the edges of the standard. Which of the attempted schemes are legal (implementation-defined) C++ when used at runtime? All of them? I'm pretty sure the memcpy() approach has to be kosher, at least. EDIT: Don't have a copy of the standard handy, but cppreference[1] suggests using reinterpret_cast<> is UB. [1] http://en.cppreference.com/w/cpp/language/reinterpret_cast http://en.cppreference.com/w/cpp/language/reinterpret_cast
- conistonwater 11y agoThis was confusing: > and divide by zero is explicitly named as undefined behavior in 5.6.4 I thought, how can this possibly be true if IEEE-754 specifically defines 1.0/-0.0 == -inf. So I looked it up, and the actual rule seems to be > If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined. So divide-by-zero is only undefined behaviour if the result is not representable. But it is representable in IEEE-754 (adherence to which is implementation-defined), so it should be fine. That said, I still don't understand how it's not a constant expression. Perhaps it's because in IEEE-754 it can sometimes cause a divide-by-zero exception, depending on flag settings? Because to check something is negative zero it's enough to do something like x == 0 && 1/x == -inf
- twoodfin 11y agoSo divide-by-zero is only undefined behaviour if the result is not representable. But it is representable in IEEE-754 (adherence to which is implementation-defined), so it should be fine. That's not how I read the line of the standard you quoted. ISTM it's saying: (Result not mathematically defined) ∨ (Result not in the range of representable values) ⇒ Behavior is undefined In this case, 1.0/-0.0 is mathematically undefined as C++ specifies such things (I believe division-by-zero is called out specifically as such), and it doesn't help that IEEE-754 can represent it anyway. It's still a useful representation in the language because you can reach it through a series of calculations that are all included in the range of defined behavior. I'm also skeptical that x == 0 && 1/x == -inf will work reliably for just that reason: I believe a compiler would be perfectly within its rights to unconditionally treat this expression as false, since x == 0 would imply UB. Not at all an expert here, so would enjoy having a standards lawyer set me straight.
- conistonwater 11y agoPerhaps you're right. I just find IEEE-754's approach clear, and C++'s rule confusing. I would say that 1.0/-0.0 is mathematically defined in the pure sense: IEEE-754 defines a specific model of real numbers closed under such operations [-inf,inf]\union{NaN}. The thing is that that test should definitely work under IEEE-754, so I would be very surprised if C++ somehow forbids you from getting the IEEE-standard behaviour. For reference, here's a proof that it's correct: $ stack repl λ> import Data.SBV λ> prove $ do x <- sFloat "x"; return $ fpIsNegativeZero x <=> (x .== 0 &&& 1/x .== -infinity) Q.E.D.
- Someone 11y agoI would expect one can export a constexpr function from a library (that seems necessary to me if one wants to write (most of) the C++ standard library in C++). If so, a workaround would be to hack together a library that exports a constexpr function that converts floats to their bit representation (that might require assembly or binary editing of a library containing a function that does that by casting) and calling it from the C++ constexpr. Such a workaround would support negative zero and NaNs.