[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: printf "%010a" Inf/NaN and FreeBSD
From: |
Bruno Haible |
Subject: |
Re: printf "%010a" Inf/NaN and FreeBSD |
Date: |
Fri, 6 Apr 2007 01:04:04 +0200 |
User-agent: |
KMail/1.5.4 |
Eric Blake wrote:
> That's now three reasons why I think FreeBSD's behavior is wrong.
OK, let's summarize (in case you want to bring it to the Austin group):
Arguments in favour of " nan", " inf":
- For NaN, there is no indication of a sign or base; for Inf, there is
no indication of a base. Even in hexadecimal mode ('a', 'A') no "0x"
is output for NaN and Inf.
- It's useful if strtod of the output returns (approximately) the original
value. And
strtod (" -inf", NULL) = -Infinity
whereas
strtod ("-000000inf", NULL) = NaN
Arguments in favour of "0000000nan", "0000000inf":
- The explanation of the flag '0' says:
"leading zeros (following any indication of sign or base) are used
to pad to the field width; no space padding is performed".
I don't think the sentence "A double argument representing an infinity or
NaN shall be converted in the style of an f or F conversion specifier" is
an argument in either direction, because all tested systems satisfy this
sentence. See below for a test.
Bruno
=========================== Program =============================
#include <stdio.h>
#include <stdlib.h>
double nan = 0.0 / 0.0;
double inf = 1.0 / 0.0;
double minf = -1.0 / 0.0;
int main()
{
char buf[20];
sprintf (buf, "%010f", -1.0);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010a", -1.0);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010f", nan);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010a", nan);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010f", inf);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010a", inf);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010f", minf);
printf ("%s|%f\n", buf, strtod (buf, NULL));
sprintf (buf, "%010a", minf);
printf ("%s|%f\n", buf, strtod (buf, NULL));
return 0;
}
================== Result on glibc-2.3.6, glibc-2.5 ===============
-01.000000|-1.000000
-0x0001p+0|-1.000000
nan|nan
nan|nan
inf|inf
inf|inf
-inf|-inf
-inf|-inf
============================ Result on FreeBSD 6.1 ================
-01.000000|-1.000000
-0x0001p+0|-1.000000
0000000nan|0.000000
0000000nan|0.000000
0000000inf|0.000000
0000000inf|0.000000
-000000inf|-0.000000
-000000inf|-0.000000
===================================================================