Print sign of NaN values.

This commit is contained in:
Ulrich Drepper 2009-08-23 11:57:52 -07:00
parent 659a63fb7b
commit 003c9895a8
4 changed files with 124 additions and 20 deletions

View File

@ -1,5 +1,10 @@
2009-08-23 Ulrich Drepper <drepper@redhat.com> 2009-08-23 Ulrich Drepper <drepper@redhat.com>
* stdio-common/printf_fp.c: ISO C expects to print the sign of NaN
as well.
* stdio-common/printf_fphex.c: Likewise.
* stdio-common/tstdiomisc.c: Add more tests.
* locale/locale.h: Include xlocale.h and the thread-local locale * locale/locale.h: Include xlocale.h and the thread-local locale
declarations for XPG7, not XPG6. declarations for XPG7, not XPG6.

View File

@ -28,6 +28,7 @@
#include <float.h> #include <float.h>
#include <gmp-mparam.h> #include <gmp-mparam.h>
#include <gmp.h> #include <gmp.h>
#include <ieee754.h>
#include <stdlib/gmp-impl.h> #include <stdlib/gmp-impl.h>
#include <stdlib/longlong.h> #include <stdlib/longlong.h>
#include <stdlib/fpioconst.h> #include <stdlib/fpioconst.h>
@ -335,6 +336,8 @@ ___printf_fp (FILE *fp,
/* Check for special values: not a number or infinity. */ /* Check for special values: not a number or infinity. */
if (__isnanl (fpnum.ldbl)) if (__isnanl (fpnum.ldbl))
{ {
union ieee854_long_double u = { .d = fpnum.ldbl };
is_neg = u.ieee.negative != 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "NAN"; special = "NAN";
@ -345,10 +348,10 @@ ___printf_fp (FILE *fp,
special = "nan"; special = "nan";
wspecial = L"nan"; wspecial = L"nan";
} }
is_neg = 0;
} }
else if (__isinfl (fpnum.ldbl)) else if (__isinfl (fpnum.ldbl))
{ {
is_neg = fpnum.ldbl < 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "INF"; special = "INF";
@ -359,7 +362,6 @@ ___printf_fp (FILE *fp,
special = "inf"; special = "inf";
wspecial = L"inf"; wspecial = L"inf";
} }
is_neg = fpnum.ldbl < 0;
} }
else else
{ {
@ -379,7 +381,8 @@ ___printf_fp (FILE *fp,
/* Check for special values: not a number or infinity. */ /* Check for special values: not a number or infinity. */
if (__isnan (fpnum.dbl)) if (__isnan (fpnum.dbl))
{ {
is_neg = 0; union ieee754_double u = { .d = fpnum.dbl };
is_neg = u.ieee.negative != 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "NAN"; special = "NAN";

View File

@ -172,6 +172,7 @@ __printf_fphex (FILE *fp,
/* Check for special values: not a number or infinity. */ /* Check for special values: not a number or infinity. */
if (__isnanl (fpnum.ldbl.d)) if (__isnanl (fpnum.ldbl.d))
{ {
negative = fpnum.ldbl.ieee.negative != 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "NAN"; special = "NAN";
@ -182,7 +183,6 @@ __printf_fphex (FILE *fp,
special = "nan"; special = "nan";
wspecial = L"nan"; wspecial = L"nan";
} }
negative = 0;
} }
else else
{ {
@ -211,6 +211,7 @@ __printf_fphex (FILE *fp,
/* Check for special values: not a number or infinity. */ /* Check for special values: not a number or infinity. */
if (__isnan (fpnum.dbl.d)) if (__isnan (fpnum.dbl.d))
{ {
negative = fpnum.dbl.ieee.negative != 0;
if (isupper (info->spec)) if (isupper (info->spec))
{ {
special = "NAN"; special = "NAN";
@ -221,7 +222,6 @@ __printf_fphex (FILE *fp,
special = "nan"; special = "nan";
wspecial = L"nan"; wspecial = L"nan";
} }
negative = 0;
} }
else else
{ {

View File

@ -47,33 +47,129 @@ t2 (void)
} }
volatile double nanval; volatile double nanval;
volatile double infval;
volatile long double lnanval;
volatile long double linfval;
static int static int
F (void) F (void)
{ {
char buf[20]; char buf[80];
wchar_t wbuf[10]; wchar_t wbuf[40];
int result; int result;
nanval = NAN; nanval = NAN;
snprintf (buf, sizeof buf, "%f %F", nanval, nanval); snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
result = strcmp (buf, "nan NAN") != 0; nanval, nanval, nanval, nanval, nanval, nanval, nanval, nanval);
printf ("expected \"nan NAN\", got \"%s\"\n", buf); result = strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);
snprintf (buf, sizeof buf, "%f %F", DBL_MAX * DBL_MAX, DBL_MAX * DBL_MAX); snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
result |= strcmp (buf, "inf INF") != 0; -nanval, -nanval, -nanval, -nanval,
printf ("expected \"inf INF\", got \"%s\"\n", buf); -nanval, -nanval, -nanval, -nanval);
result = strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
buf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%f %F", nanval, nanval); infval = DBL_MAX * DBL_MAX;
result |= wcscmp (wbuf, L"nan NAN") != 0;
printf ("expected L\"nan NAN\", got L\"%S\"\n", wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%f %F", snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
DBL_MAX * DBL_MAX, DBL_MAX * DBL_MAX); infval, infval, infval, infval, infval, infval, infval, infval);
result |= wcscmp (wbuf, L"inf INF") != 0; result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0;
printf ("expected L\"inf INF\", got L\"%S\"\n", wbuf); printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf);
snprintf (buf, sizeof buf, "%a %A %e %E %f %F %g %G",
-infval, -infval, -infval, -infval,
-infval, -infval, -infval, -infval);
result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n",
buf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
nanval, nanval, nanval, nanval, nanval, nanval, nanval, nanval);
result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
-nanval, -nanval, -nanval, -nanval,
-nanval, -nanval, -nanval, -nanval);
result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
infval, infval, infval, infval, infval, infval, infval, infval);
result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0;
printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]), L"%a %A %e %E %f %F %g %G",
-infval, -infval, -infval, -infval,
-infval, -infval, -infval, -infval);
result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n",
wbuf);
lnanval = NAN;
snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
lnanval, lnanval, lnanval, lnanval,
lnanval, lnanval, lnanval, lnanval);
result = strcmp (buf, "nan NAN nan NAN nan NAN nan NAN") != 0;
printf ("expected \"nan NAN nan NAN nan NAN nan NAN\", got \"%s\"\n", buf);
snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
-lnanval, -lnanval, -lnanval, -lnanval,
-lnanval, -lnanval, -lnanval, -lnanval);
result = strcmp (buf, "-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
printf ("expected \"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got \"%s\"\n",
buf);
linfval = LDBL_MAX * LDBL_MAX;
snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
linfval, linfval, linfval, linfval,
linfval, linfval, linfval, linfval);
result |= strcmp (buf, "inf INF inf INF inf INF inf INF") != 0;
printf ("expected \"inf INF inf INF inf INF inf INF\", got \"%s\"\n", buf);
snprintf (buf, sizeof buf, "%La %LA %Le %LE %Lf %LF %Lg %LG",
-linfval, -linfval, -linfval, -linfval,
-linfval, -linfval, -linfval, -linfval);
result |= strcmp (buf, "-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
printf ("expected \"-inf -INF -inf -INF -inf -INF -inf -INF\", got \"%s\"\n",
buf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
L"%La %LA %Le %LE %Lf %LF %Lg %LG",
lnanval, lnanval, lnanval, lnanval,
lnanval, lnanval, lnanval, lnanval);
result |= wcscmp (wbuf, L"nan NAN nan NAN nan NAN nan NAN") != 0;
printf ("expected L\"nan NAN nan NAN nan NAN nan NAN\", got L\"%S\"\n", wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
L"%La %LA %Le %LE %Lf %LF %Lg %LG",
-lnanval, -lnanval, -lnanval, -lnanval,
-lnanval, -lnanval, -lnanval, -lnanval);
result |= wcscmp (wbuf, L"-nan -NAN -nan -NAN -nan -NAN -nan -NAN") != 0;
printf ("expected L\"-nan -NAN -nan -NAN -nan -NAN -nan -NAN\", got L\"%S\"\n",
wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
L"%La %LA %Le %LE %Lf %LF %Lg %LG",
linfval, linfval, linfval, linfval,
linfval, linfval, linfval, linfval);
result |= wcscmp (wbuf, L"inf INF inf INF inf INF inf INF") != 0;
printf ("expected L\"inf INF inf INF inf INF inf INF\", got L\"%S\"\n", wbuf);
swprintf (wbuf, sizeof wbuf / sizeof (wbuf[0]),
L"%La %LA %Le %LE %Lf %LF %Lg %LG",
-linfval, -linfval, -linfval, -linfval,
-linfval, -linfval, -linfval, -linfval);
result |= wcscmp (wbuf, L"-inf -INF -inf -INF -inf -INF -inf -INF") != 0;
printf ("expected L\"-inf -INF -inf -INF -inf -INF -inf -INF\", got L\"%S\"\n",
wbuf);
return result; return result;
} }