From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 90316 invoked by alias); 14 Aug 2018 15:54:19 -0000 Mailing-List: contact cygwin-help@cygwin.com; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner@cygwin.com Mail-Followup-To: cygwin@cygwin.com Received: (qmail 90305 invoked by uid 89); 14 Aug 2018 15:54:19 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-2.1 required=5.0 tests=AWL,BAYES_00,KAM_LAZY_DOMAIN_SECURITY autolearn=no version=3.3.2 spammy=H*Ad:D*jp X-HELO: msc14.plala.or.jp Received: from msc14.plala.or.jp (HELO msc14.plala.or.jp) (60.36.166.24) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 14 Aug 2018 15:54:17 +0000 Received: from localhost ([223.217.20.9]) by msc14.plala.or.jp with ESMTP id <20180814155415.NGUD8002.msc14.plala.or.jp@localhost>; Wed, 15 Aug 2018 00:54:15 +0900 Date: Tue, 14 Aug 2018 16:05:00 -0000 Message-Id: <20180815.005410.1198201163154042143.trueroad@trueroad.jp> To: cygwin@cygwin.com Cc: trueroad@trueroad.jp Subject: Re: strtod ("nan") returns negative NaN From: Masamichi Hosoda In-Reply-To: <20180814132301.GX3747@calimero.vinschen.de> References: <20180814.211757.2066454831159853501.trueroad@trueroad.jp> <20180814_dot_211757_dot_2066454831159853501_dot_trueroad_at_trueroad_dot_jp> <20180814132301.GX3747@calimero.vinschen.de> Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-VirusScan: Outbound; mvir-ac14; Wed, 15 Aug 2018 00:54:15 +0900 X-SW-Source: 2018-08/txt/msg00183.txt.bz2 [...] >> > 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. > > Your patch improves the situation, that's a sure thing and I did not > question that. > > I just wonder why returning -NaN when the input is "-nan" isn't the > better approach. After all: > > printf ("nan (\"\") = %f\n", nan ("")); > printf ("-nan (\"\") = %f\n", -nan ("")); > > ==> > > nan ("") = nan > -nan ("") = -nan > > So, shouldn't the ideal outcome be this: > > strtod ("nan", NULL) = nan > strtod ("-nan", NULL) = -nan > strtold ("nan", NULL) = nan > strtold ("-nan", NULL) = -nan > > ? On Linux, strtof ("nan"), strtof ("-nan"), strtod ("nan"), strtod ("-nan"), strtold ("nan"), and strtold ("-nan") all return positive NaN. My patch is for closing to the behavior of Linux. I don't know why Linux's strtod ("-nan") does not return negative NaN. But, probably because both positive and negative NaN behave in the same way, I think. Here's sample code. ``` #include #include #include int main (void) { double pnan = nan (""); double nnan = -pnan; printf ("positive NaN == positive NaN: "); if (pnan == pnan) printf ("true\n"); else printf ("false\n"); printf ("negative NaN == negative NaN: "); if (nnan == nnan) printf ("true\n"); else printf ("false\n"); printf ("0 < positive NaN: "); if (0 < pnan) printf ("true\n"); else printf ("false\n"); printf ("0 > positive NaN: "); if (0 > pnan) printf ("true\n"); else printf ("false\n"); printf ("0 < negative NaN: "); if (0 < nnan) printf ("true\n"); else printf ("false\n"); printf ("0 > negative NaN: "); if (0 > nnan) printf ("true\n"); else printf ("false\n"); } ``` Result: ``` positive NaN == positive NaN: false negative NaN == negative NaN: false 0 < positive NaN: false 0 > positive NaN: false 0 < negative NaN: false 0 > negative NaN: false ``` -- 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