News for hackers. Stuff that matters.
Re:One does not simply calculate the absolute value by JadeNB on Sunday August 22, 2021 @03:26PM (#28268646)
> Does this correctly handle all the different NaN values, weird corner cases, etc. properly?

Of course the answer to "have you covered all weird corner cases" is always 'no', but the article addresses the issue of different NaNs:

> […] However, it turns out that doubleToLongBits is also not entirely trivial, because it canonicalizes NaNs. There are many ways to encode a not-a-number as a double, but only one of them is canonical. These different NaNs are even more similar twins, they can be distinguished neither via Double.compare, nor via any operation. Even string representation is the same. But they look different in computer memory. To avoid surprises, doubleToLongBits converts any NaN to the canonical form, which is encoded in long as 0x7ff8000000000000L. Of course, this procedure adds more conditions, which we do not need here.

> What can we do? It appears that there's another method doubleToRawLongBits. It doesn't do any smart conversion of NaN and just returns exactly the same bit representation: […]

Re:One does not simply calculate the absolute value by sp332 on Sunday August 22, 2021 @03:41PM (#28268808)
The question is whether that last technique, of just flipping the sign bit, handles NaNs and infinities.
Re:One does not simply calculate the absolute value by pm215 on Sunday August 22, 2021 @04:09PM (#28269105)
It's the required semantics of the IEEE floating point spec's absolute-value operation. This does mean that abs() doesn't behave like eg addition in that it won't set fp exception flags for signaling NaNs, or canonicalize NaNs, or set a flag to note that this was a denormal number. But at the language level "follows IEEE semantics" is probably overall less surprising.

chs() ("flip the sign bit") is the other operation that's defined as effectively just an operation on the representation rather than a "do a real floating point computation".

Re:One does not simply calculate the absolute value by MontyCarloHall on Sunday August 22, 2021 @03:59PM (#28269011)
Yes.

The sign bit is totally independent of the value being encoded, whether that’s a number, q/sNaN, or infinity.

Nitpick: we are not flipping the sign bit, but rather setting it to 0.

Re:One does not simply calculate the absolute value by klyrs on Sunday August 22, 2021 @04:28PM (#28269290)
Yeah, the title is a head-scratcher. It is simple to calculate the absolute value. Only, it doesn't look simple in a high-level language.