0

If I have a floating point operation that yields a NaN, does that NaN have a defined signedness according to the parameters, or is there a default defined by the CPU or IEEE 754?

For example, if I have -0.0 / +0.0, is the resulting NaN negative because the signs invert?

The isnan() function works with positive as well as negative negative NaNs as an input value for my platform.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • I think most of your questions can be answered by looking at Wikipedia and running a couple of tests on your machine. No need for SO. – Mad Physicist Sep 30 '22 at 09:23
  • I've checked Wikipedia here and I couldn't get an answer. –  Sep 30 '22 at 09:31
  • What did you check, and which question? – Mad Physicist Sep 30 '22 at 09:39
  • The Wikipedia just states that both reprentations mean the same. I wrote a libary function on my own that can give NaN results according to the paramters and I want to behave exactly like the standard libary, the CPU or IEEE-754 mandates, whatever is more appropriate. The special thing why this function doesn't yield such results according to the FPU-results is that it doesn't do any FP-calculations at all but operates on the bit patterns of the floating point operands. –  Sep 30 '22 at 09:59

1 Answers1

4

IEEE 754 2019 6.3 says:

When either an input or result is a NaN, this standard does not interpret the sign of a NaN. However, operations on bit strings—copy, negate, abs, copySign—specify the sign bit of a NaN result, sometimes based upon the sign bit of a NaN operand. The logical predicates totalOrder and isSignMinus are also affected by the sign bit of a NaN operand. For all other operations, this standard does not specify the sign bit of a NaN result, even when there is only one input NaN, or when the NaN is produced from an invalid operation…

The 2008 version has much the same wording, but the 1985 version says:

This standard does not interpret the sign of an NaN. Otherwise, the sign of a product or quotient is the exclusive or of the operands’ signs; the sign of a sum, or of a difference x − y regarded as a sum x + (−y), differs from at most one of the addends signs, and the sign of the result of the round floating-point number to integral value operation is the sign of the operand. These rules shall apply even when operands or results are zero or infinite…

Thus, from the 1985 wording, one might expect the sign bit of a NaN produced from +0/−0 to be set, as this sign bit is produced, not interpreted, by the operation. But the committee apparently clarified or changed this so that “this standard does not specify the sign bit of a NaN result” except for the listed operations.

For x86 and x86-64, Intel 64 and IA-32 Architectures Software Developer’s Manual, December 2017, shows that the “QNaN floating-point indefinite” produced for invalid operations (Table 8-10) has its sign bit set (Table 4-3).

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312