Mail Archives: djgpp-workers/2013/03/06/14:16:44
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 -