www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2013/03/16/18:29:41

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: V01U2FsdGVkX19WVDwafBFWjA2m3iy4/Fb70CEHcp925PaoIp0tD9
qOj7XZSg+tDzFr
Message-ID: <5144F255.3010402@gmx.de>
Date: Sat, 16 Mar 2013 23:29:41 +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
Subject: Behavior of NaN when returned by a function and assigned to a variable
X-Y-GMX-Trusted: 0
Reply-To: djgpp-workers AT delorie DOT com

Please see the code snippet below.
The 3 functions create a SNaN.  The first digit after
the decimal point is 0 and at least one other digit is
not 0.  The assignment like this:
  fv.f = ff();
converts the SNaN returned by ff() into a QNaN stored in fv.
This is true for float and double but not for long double
that remains as SNaN.
Can someone explain me what the reason for this behavior is?
I have run the same code on my linux box and in this case
all SNaN remain SNaN after the assignment.

If more info is needed please let me know.

Regards,
Juan M. Guerrero


Output of the coode snippet:

nan:  s:0  e:ff  m:400001             QNaN.  Integer bit not printed
nan:  s:0  e:7ff  mh:80000  ml:1      QNaN.  Integer bit not printed
nan:  s:0  e:7fff  mh:80000000  ml:1  SNaN.  Integer bit printed



#include <stdio.h>
#include <math.h>
#include <libc/ieee.h>


float ff(void)
{
   _float_union_t v;
   v.f = NAN;
   v.ft.mantissa = 0x01U;  /* SNaN. Set the integer bit. */
   return v.f;
}

double fd(void)
{
   _double_union_t v;
   v.d = NAN;
   v.dt.mantissal = 0x01U;
   v.dt.mantissah = 0x00U;  /* SNaN. */
   return v.d;
}

long double fld(void)
{
   _longdouble_union_t v;
   v.ld = NAN;
   v.ldt.mantissal = 0x01U;
   v.ldt.mantissah = 0x80000000U;  /* SNaN. Set the integer bit. */
   return v.ld;
}

int main(void)
{
   _float_union_t fv;
   _double_union_t dv;
   _longdouble_union_t ldv;

   fv.f = ff();
   printf("%g:  s:%u  e:%x  m:%x\n", fv.f, fv.ft.sign, fv.ft.exponent, 
fv.ft.mantissa);

   dv.d = fd();
   printf("%g:  s:%u  e:%x  mh:%x  ml:%x\n", dv.d, dv.dt.sign, 
dv.dt.exponent, dv.dt.mantissah, dv.dt.mantissal);

   ldv.ld = fld();
   printf("%Lg:  s:%u  e:%x  mh:%x  ml:%x\n", ldv.ld, ldv.ldt.sign, 
ldv.ldt.exponent, ldv.ldt.mantissah, ldv.ldt.mantissal);

   return 0;
}

- Raw text -


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