5

Based on "IEEE" spec : "When either an input or result is NaN, this standard does not interpret the sign of a NaN." However the printf prints NaN values as signed:nan or -nan Can someone point me the set of rules(from spec?) when nan and when -nan is printed For example , I checked that printf(-1.0f) prints -nan Thank you

YAKOVM
  • 9,805
  • 31
  • 116
  • 217

1 Answers1

9

The underlying representation of a NaN contains a sign bit, and this is what printf looks at when deciding if it should print the minus or not.

The reason why the standard says that the sign bit should be ignored is to allow things like negate or absolute to simply modify the sign bit, without being forced to check if the input value was NaN.

Lindydancer
  • 25,428
  • 4
  • 49
  • 68
  • Can you please direct me where in the spec or (some) manual it is written. – YAKOVM Jan 11 '12 at 10:06
  • Sure, the Wikipedia article covers the underlying representation in great detail: http://en.wikipedia.org/wiki/IEEE_754-1985 – Lindydancer Jan 11 '12 at 10:22
  • I don`t see any attitude there about -nan vs nan.The document deals with arithmetic ,not print – YAKOVM Jan 11 '12 at 10:28
  • 2
    If the sign bit in the underlying representation is set, this particular implementation of `printf` prints `-nan`. There is no standard that says that it has to, or that it shouldn't. The C standard does not specify that a particular floating-point implementation is used, or that NaN:s behave in any special way. – Lindydancer Jan 11 '12 at 10:39