bin/109692: printing -NaN
David Schultz
das at FreeBSD.org
Wed Jan 9 21:50:03 PST 2008
The following reply was made to PR kern/109692; it has been noted by GNATS.
From: David Schultz <das at FreeBSD.ORG>
To: Denis Koreshkov <dynamic-wind at mail.ru>
Cc: freebsd-gnats-submit at FreeBSD.ORG
Subject: Re: bin/109692: printing -NaN
Date: Thu, 10 Jan 2008 00:46:37 -0500
On Wed, Feb 28, 2007, Denis Koreshkov wrote:
> The vfprintf() function correctly formats +Inf and -Inf but it
> has no provision for distinguishing between +NaN and -NaN. Hence
> all functions of printf(3) family output all -NaNs as NaNs.
Thanks for pointing this out. I believe that this is historical
and deliberate behavior, and I do not think it violates C99 or
IEEE P754. To quote from IEEE P754:
Conversion of a quiet NaN in a supported format to an external
character sequence shall produce a language-defined one of
"nan" or a sequence that is equivalent except for case (e.g.,
"NaN"), with an optional preceding sign. The sign of a NaN has
no meaning. (5.12.1)
In an abstract sense, NaNs aren't signed; none of the usual
mathematical operators can be used to distinguish a "positive NaN"
from a "negative NaN", even though the underlying representation
includes a sign bit. Only operations that operate directly on the
bit strings (IEEE P754 defines assignment, negation, fabs, and
copysign) have a well-defined effect on the sign bit. For all
other operations, the hardware is granted free license to set the
sign bit of a NaN however it wants (6.3). Thus, printing the sign
for NaNs would probably reduce portability, not to mention
violating POLA.
More information about the freebsd-bugs
mailing list