Re: strtod ("nan") returns negative NaN

Masamichi Hosoda <[email protected]>
Newsgroups gmane.comp.lib.newlib
Message-ID <[email protected]>
[...]
> By the way, here are the results of the test prints from an embedded
> 64-bit ARM:
> 
> strtof ("nan", NULL) = 0.000000
> strtof ("-nan", NULL) = 0.000000
> strtod ("nan", NULL) = nan
> strtod ("-nan", NULL) = nan
> strtold ("nan", NULL) = inf
> strtold ("-nan", NULL) =
> 2315841784746322880031071374419981810209660594968745666
> 57923565917084799991808.000000

If I understand correctly, qNaN definition for 64-bit ARM is wrong
like x86_64 and i386 definition was wrong.

The following code can generate the correct definition.

```
/* gcc arch-nans.c */

#include <inttypes.h>
#include <math.h>
#include <stdio.h>
#include <stdint.h>

int main (void)
{
  union
  {
    float f;
    uint32_t u;
  } fb;

  union
  {
    double d;
    uint32_t u[2];
  } db;

  union
  {
    long double ld;
    uint32_t u[4];
    uint16_t us[5];
  } ldb;

  fb.u = 0;
  fb.f = nanf ("");
  printf ("#define f_QNAN 0x%"PRIx32"\n", fb.u);

  db.u[0] = 0;
  db.u[1] = 0;
  db.d = nan ("");
  printf ("#define d_QNAN0 0x%"PRIx32"\n"
	  "#define d_QNAN1  0x%"PRIx32"\n", db.u[0], db.u[1]);

  ldb.u[0] = 0;
  ldb.u[1] = 0;
  ldb.u[2] = 0;
  ldb.u[3] = 0;
  ldb.ld = nanl ("");
  printf ("#define ld_QNAN0 0x%"PRIx32"\n"
	  "#define ld_QNAN1 0x%"PRIx32"\n"
	  "#define ld_QNAN2 0x%"PRIx32"\n"
	  "#define ld_QNAN2 0x%"PRIx32"\n",
	  ldb.u[0], ldb.u[1], ldb.u[2], ldb.u[3]);
  printf ("#define ldus_QNAN0 0x%"PRIx16"\n"
	  "#define ldus_QNAN1 0x%"PRIx16"\n"
	  "#define ldus_QNAN2 0x%"PRIx16"\n"
	  "#define ldus_QNAN3 0x%"PRIx16"\n"
	  "#define ldus_QNAN4 0x%"PRIx16"\n",
	  ldb.us[0], ldb.us[1], ldb.us[2], ldb.us[3], ldb.us[4]);
}
```

The result on Cygwin 64 bit (x86_64) and 32 bit (i386):
```
#define f_QNAN 0x7fc00000
#define d_QNAN0 0x0
#define d_QNAN1  0x7ff80000
#define ld_QNAN0 0x0
#define ld_QNAN1 0xc0000000
#define ld_QNAN2 0x7fff
#define ld_QNAN2 0x0
#define ldus_QNAN0 0x0
#define ldus_QNAN1 0x0
#define ldus_QNAN2 0x0
#define ldus_QNAN3 0xc000
#define ldus_QNAN4 0x7fff
```

My patch
0001-Fix-strtod-nan-returns-negative-NaN.patch
v2-0001-Fix-strtod-nan-returns-negative-NaN.patch
uses the generated definition.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.