delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2013/03/06/14:16:44

X-Authentication-Warning: delorie.com: mail set sender to djgpp-workers-bounces using -f
X-Recipient: djgpp-workers AT delorie DOT com
X-Authenticated: #27081556
X-Provags-ID: V01U2FsdGVkX1/Sw6Jo5L3Sq0yAsqcZs1rp4dxXG5mXWKujb/+xdu
fPSEOlfJarm/d6
Message-ID: <5137961A.4000505@gmx.de>
Date: Wed, 06 Mar 2013 20:16:42 +0100
From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:16.0) Gecko/20121025 Thunderbird/16.0.2
MIME-Version: 1.0
To: djgpp-workers AT delorie DOT com
CC: Eli Zaretskii <eliz AT gnu DOT org>
Subject: Re: Printing sign of NaN.
References: <51364BCD DOT 1030807 AT gmx DOT de> <83y5e11pn7 DOT fsf AT gnu DOT org>
In-Reply-To: <83y5e11pn7.fsf@gnu.org>
X-Y-GMX-Trusted: 0
Reply-To: djgpp-workers AT delorie DOT com

Am 05.03.2013 22:51, schrieb Eli Zaretskii:
>> Date: Tue, 05 Mar 2013 20:47:25 +0100
>> From: Juan Manuel Guerrero <juan DOT guerrero AT gmx DOT de>
>>
>> ONFY while I was testing trunc() I noted that printf did not print the
>> sign of NaN.  The committed small patch  below fixes the issue.
> Isn't NaN always negative?  Do you succeed in printing both negative
> and positive NaN with this patch?
>
> Apologies if my failing memory just failed me again.
I noted this issue when testing the test programs for trunc[fl] on my 
linux box.
Please see the code snippet below:
----  code start  ----
#include <stdio.h>


typedef struct {
   unsigned mantissal:32;
   unsigned mantissah:32;
   unsigned exponent:15;
   unsigned sign:1;
} long_double_t;

typedef union
{
   long double ld;
   long_double_t ldt;
} _longdouble_union_t;


int main(void)
{
   /*  NaN definitions according 253665.pdf
       Table 4-3. Floating-Point Number and NaN Encodings  */

   _longdouble_union_t snan_p, snan_n;


   /* SNaN.  */
   snan_p.ldt.mantissal = 0x00000001U;
   snan_p.ldt.mantissah = 0x80000000U;
   snan_p.ldt.exponent  = 0x7FFFU;
   snan_p.ldt.sign      = 0;
   snan_n.ld = snan_p.ld;
   snan_n.ldt.sign      = 1;

   printf("SNaN:\n"
          "%+Lg   %-Lg\n"
          "%+Lg   %-Lg\n", snan_p.ld, snan_p.ld, snan_n.ld, snan_n.ld);


   /* QNaN.  */
   snan_p.ldt.mantissah = 0xC0000000U;
   snan_n.ldt.mantissah = 0xC0000000U;
   printf("QNaN:\n"
          "%+Lg   %-Lg\n"
          "%+Lg   %-Lg\n", snan_p.ld, snan_p.ld, snan_n.ld, snan_n.ld);


   /* QNaN  floating-point indefinite.  */
   snan_p.ldt.mantissal = 0x00000000U;
   snan_n.ldt.mantissal = 0x00000000U;
   printf("QNaN  floating-point indefinite:\n"
          "%+Lg   %-Lg\n"
          "%+Lg   %-Lg\n", snan_p.ld, snan_p.ld, snan_n.ld, snan_n.ld);
   return 0;
}

----  code end  ----


This produces the following output on my linux box (gcc (SUSE Linux) 4.6.2,
glibc 2.14.1):

SNaN:
+nan   nan
-nan   -nan
QNaN:
+nan   nan
-nan   -nan
QNaN  floating-point indefinite:
+nan   nan
-nan   -nan


I do not know if this is a bug or a feature, but my intention was to imitate
this behavior.  Neither less according to 253665.pdf 4.8.3.4 NaNs, the sign
of NaNs are ignored.  I do not know what consequences this should have when
writing code.


 > Isn't NaN always negative?  Do you succeed in printing both negative
 > and positive NaN with this patch?

Yes, I can print both signs with this change.
In this line:

       if (_ldouble < 0 || (IS_NAN(ieee_value) && ieee_value.ldt.sign))

_ldouble < 0 is always false if _ldouble is a NaN.  I was not aware of this.
Iff _ldouble is a NaN then value of ieee_value.ldt.sign determinates the
sign to be printed.


Regards,
Juan M. Guerrero

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019