3 ms·
The question is whether that last technique, of just flipping the sign bit, handles NaNs and infinities.
by sp332 5y ago
The question is whether that last technique, of just flipping the sign bit, handles NaNs and infinities.
- MontyCarloHall 5y agoYes. 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.
- pm215 5y agoIt'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".
- klyrs 5y agoYeah, 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.