public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Masamichi Hosoda <trueroad@trueroad.jp>
To: cygwin@cygwin.com
Cc: trueroad@trueroad.jp
Subject: Re: strtod ("nan") returns negative NaN
Date: Tue, 14 Aug 2018 12:18:00 -0000	[thread overview]
Message-ID: <20180814.211757.2066454831159853501.trueroad@trueroad.jp> (raw)
In-Reply-To: <20180814103900.GU3747@calimero.vinschen.de>

> On Aug 14 11:56, Corinna Vinschen wrote:
>> On Aug 14 13:45, Masamichi Hosoda wrote:
>> > >From a50ee5a4747a99c70469a53fe959f3dc22d3b79a Mon Sep 17 00:00:00 2001
>> > From: Masamichi Hosoda <trueroad@trueroad.jp>
>> > Date: Tue, 14 Aug 2018 12:50:32 +0900
>> > Subject: [PATCH] Fix strtod ("nan") returns qNaN
>> > 
>> > The definition of qNaN for x86_64 and x86 was wrong.
>> > So strtod ("nan") returned sNaN instead of qNaN.
>> > 
>> > Furthermore, it was inverted the sign bit with the presence of `-` character.
>> > So strtod ("-nan") returned qNaN.
>> > 
>> > This commit fixes definition of qNaN
>> > and removes the sign bit inversion when evaluating "nan".
>> > ---
>> >  newlib/libc/stdlib/gd_qnan.h | 8 ++++----
>> >  newlib/libc/stdlib/strtod.c  | 1 +
>> >  2 files changed, 5 insertions(+), 4 deletions(-)
>> 
>> Can you please send this patch to the newlib AT sourceware DOT org
>> mailing list?  As soon as something in newlib gets changed, a lot of
>> other targets are affected and the guys working on those targets should
>> have a chance to chime in.

I'll improve the patch and send it.
It did not consider environments excluding x86 and x86_64.

> Looks like strtold is affected as well, just differently:
> 
>   printf ("strtod (\"nan\", NULL) = %f\n", strtod ("nan", NULL));
>   printf ("strtod (\"-nan\", NULL) = %f\n", strtod ("-nan", NULL));
>   printf ("strtold (\"nan\", NULL) = %Lf\n", strtold ("nan", NULL));
>   printf ("strtold (\"-nan\", NULL) = %Lf\n", strtold ("-nan", NULL));
>   printf ("nan (\"\") = %f\n", nan (""));
> 
> ==>
> 
>   strtod ("nan", NULL) = -nan
>   strtod ("-nan", NULL) = nan
>   strtold ("nan", NULL) = -nan
>   strtold ("-nan", NULL) = -nan
>   nan ("") = nan
> 
> so it prints always -nan.
> 
> With your patch, strtold looks more correct, but it still prints the
> sign of NaN:
> 
>   strtod ("nan", NULL) = nan
>   strtod ("-nan", NULL) = nan
>   strtold ("nan", NULL) = nan
>   strtold ("-nan", NULL) = -nan
>   nan ("") = nan
> 
> Question: What's wrong with that?  Wouldn't it be more correct if
> strtod returns -NaN for "-nan" as well?

In my investigate,
strtold sets sign bit when parameter has '-' character.
The wrong long double NaN definition is negative NaN that is set sign bit.
So without my patch, both strtold ("nan") and
strtold ("-nan") return negative NaN.

On the other hand, strtod inverts the sign when parameter has '-' character.
The wrong double NaN definition is negative NaN.
So without my patch, strtod ("nan") returns negative NaN
and strtod ("-nan") returns positive NaN.

My previous patch removes the sign inversion in strtod when NaN.
But I did not fix strtold.

Perhaps, the following patch removes the sign bit setting of strtold when NaN.

```
--- a/newlib/libc/stdlib/strtodg.c
+++ b/newlib/libc/stdlib/strtodg.c
@@ -585,6 +585,7 @@ _strtodg_l (struct _reent *p, const char *s00, char **se, FPI *fpi, Long *exp,
 					if (*s == '(') /*)*/
 						irv = hexnan(&s, fpi, bits);
 #endif
+					sign = 0;
 					goto infnanexp;
 					}
 			  }
```

--
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

  reply	other threads:[~2018-08-14 12:18 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-08-13 15:16 `std::stod ("nan")` " Masamichi Hosoda
2018-08-13 16:53 ` Stephen John Smoogen
2018-08-13 23:46   ` Duncan Roe
2018-08-14  0:46     ` Stephen John Smoogen
2018-08-14  1:10       ` Masamichi Hosoda
2018-08-14  2:31         ` strtod ("nan") returns negative NaN (was `std::stod ("nan")` returns negative NaN) Masamichi Hosoda
2018-08-14  3:25           ` strtod ("nan") returns negative NaN Steven Penny
2018-08-14  4:46           ` Masamichi Hosoda
2018-08-14  9:56             ` Corinna Vinschen
2018-08-14 10:39               ` Corinna Vinschen
2018-08-14 12:18                 ` Masamichi Hosoda [this message]
2018-08-14 13:23                   ` Corinna Vinschen
2018-08-14 13:41                     ` Stephen John Smoogen
2018-08-14 15:25                     ` Heavenly Avenger
2018-08-14 15:54                       ` Corinna Vinschen
2018-08-14 17:08                         ` Heavenly Avenger
2018-08-14 16:05                     ` Masamichi Hosoda
2018-08-14 19:24                     ` Steven Penny
2018-08-14 21:45                       ` Andy Moreton
2018-08-14 22:23                         ` Stephen John Smoogen
2018-08-15  0:02                           ` Eric Blake
2018-08-15  7:36                             ` Steven Penny
2018-08-21  3:18                             ` Brian Inglis
2018-08-14 19:44                 ` Achim Gratz
2018-08-21 13:28                   ` Brian Inglis
2018-08-14 15:05               ` Masamichi Hosoda
2018-08-14 15:21                 ` Masamichi Hosoda
2018-08-14 15:35                 ` Heavenly Avenger
2018-08-14 20:05                 ` Joseph Myers
2018-08-14 20:19                   ` Joseph Myers
2018-08-15  8:51                   ` Masamichi Hosoda
2018-08-15 12:55                     ` Masamichi Hosoda

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180814.211757.2066454831159853501.trueroad@trueroad.jp \
    --to=trueroad@trueroad.jp \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).