Web lists-archives.com

Re: strtod ("nan") returns negative NaN




On 08/14/2018 04:31 PM, Stephen John Smoogen wrote:

The C standard disagrees with you [ISO:IEC 9899:2011, section 5.2.4.2.2]:

"An implementation may give zero and values that are not floating-point
numbers (such as infinities and NaNs) a sign or may leave them unsigned.
Wherever such values are unsigned, any requirement in this International
Standard to retrieve the sign shall produce an unspecified sign, and any
requirement to set the sign shall be ignored."


Does it disagree? I would say it did if it said MUST.. but it says MAY
I thought usually meant "be consistent with what you think is right
for your environment but yeah whatever".

Read conversely, if the implementation lets NaN have a sign (which Cygwin does), then retrieving and setting the sign is defined.


  And one can read the "any requirement to set the sign shall be
ignored" as being -NaN should come back as NaN. I don't know how this
is also affected by https://en.wikipedia.org/wiki/ISO/IEC_10967 which
goes to the IEEE 754 NaN is not a number (except when it is). [

It looks from my layman point of view that Cygwin is ok with sending
whatever they want back if it is implementation defined. However the
bigger case is whether they want to be similar to how the other
environments report things. Currently Cygwin reports -NaN for positive
NaN and 'NaN' for '-NaN'.. which may be ok but doesn't match the other
environments which all report NaN. [Does that make sense?]

Cygwin is indeed buggy for turning "NaN" into -NaN; that's easy enough to fix. The remaining question is whether it should turn "-NaN" into -NaN; and the argument that glibc JUST fixed their bug 23007 to make strtod("-nan") return -NaN means that Cygwin should, indeed, preserve the negative sign bit when parsing "-nan".

https://sourceware.org/bugzilla/show_bug.cgi?id=23007

So, the desired behavior:

strtod("nan") -> NaN
strtod("-nan") -> -NaN
printf("%f", NaN) -> "NaN"
printf("%f", -NaN) -> "-NaN"

and similarly for float and long double.

--
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

--
Problem reports:       http://cygwin.com/problems.html
FAQ:                   http://cygwin.com/faq/
Documentation:         http://cygwin.com/docs.html
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple