Re: sprintf() and negative zero Otto Moerbeek wrote:
> On Tue, Jul 01, 2008 at 08:15:05PM +0100, Edd Barrett wrote:
>> Here is a simple test for repro:
>>
>> #include <stdio.h>
>>
>> int
>> main(void)
>> {
>> double a = -0;
>> printf("%x = %e\n",a, a);
>> return 0;
>> }
>>
>> puff% ./a.out
>> 0 = 0.000000e+00
>>
>> Thoughts appreciated.
>
> This test program is bogus. You cannnot pass a double to printf and
> print it as an int. Also, the converison on -0 will first be to
> integer 0 and then to double 0.0.
Yup! I had a "doh!" moment whilst in the shower a few minutes ago.
Try this:
#include <stdio.h>
int
main(void)
{
double a;
/* Raw */
printf("%f\n", 0x8000000000000000);
/* Force Sign */
printf("%+f\n", 0x8000000000000000);
/* Test on real -ve number */
a = -0.666;
printf("%+f\n", a);
return 0;
}
puff% ./a.out
0.000000
+0.000000
-0.666000
Also FWIW we md5'd the binary made from the assembler on linux and
OpenBSD and they match.
>
> That said, I actually don't know if printf should make a distinction between
> +0.0 and -0.0.
*shrugs* |