1

Any way for me to set a constexpr to negative 0?

A legacy library uses negative 0 to indicate the calculation failed so I want to create a constexpr float fNEGZERO = -0.0.

Normally I would create this using a union between a uint32 and a float and set the uint32 bits to 0x8000'0000. But constexpr doesn't allow having a union access the non-active member. In theory the compiler does the same thing for NaN, +Inf, etc. but looking at numeric_limits it seems like they rely on built in compiler values.

Rocinante8
  • 13
  • 3
  • 1
    BTW, you probably know this since you're asking this question, but 0.0 and -0.0 compare as equal. So once you have this negative zero constant, checking whether a number is negative zero couldn't be done by comparing against it (not with a normal comparison anyway). – harold Dec 07 '22 at 05:36
  • How about ```constexpr float fNEGZERO = -1.f / HUGE_VALF```? – Homer512 Dec 07 '22 at 05:47

0 Answers0