Boost logo

Boost :

From: Niall Douglas (s_sourceforge_at_[hidden])
Date: 2021-01-26 11:27:58


On 26/01/2021 09:08, Dominique Devienne via Boost wrote:
> On Tue, Jan 26, 2021 at 9:53 AM Niall Douglas via Boost <
> boost_at_[hidden]> wrote:
>
>> if(foo.x == FLOAT_UNSET) ...
>>
>
> Since two NaNs never compare equal, at runtime at least, isn't this code
> wrong in the first place?

Apologies, I wrote that example code when drinking first coffee of the
morning. You're right it's wrong in the literal sense, so consider it
meaning the figurative sense.

If it were to be literally correct:

if(0 == memcmp(&foo.x, &FLOAT_UNSET, sizeof(float))) ...

So here if FLOAT_UNSET is instanced by the compiler via consteval, it
appears as all bits one. But if it is instanced at runtime, you get who
know's what.

(As a guess, on ARMv7 not all the bits would be 1)

To write correctly portable code if the architecture is two's complement
(now required in recent C++), you might instead write:

static constexpr uint32_t ALL_BITS_ONE = (uint32_t) -1;

if(0 == memcmp(&foo.x, &ALL_BITS_ONE, sizeof(float))) ...

Or depending on semantics, isnan(foo.x) might be sufficient.

Niall


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk