Subject: Re: floating point negative zero
To: Alan Barrett <apb@cequrux.com>
From: Terry Moore <tmm@mcci.com>
List: tech-userlevel
Date: 08/24/2007 17:19:50
According to ISO C-1999 page 275, section 7.19.6.1, note 233:
"The results of all floating conversions of a negative zero, and of
negative values that round to zero, include a minus sign".
So if you want to comply with standards, printf() etc have to produce
the minus sign.
I suspect that this is also true of awk based on 'transitive closure
of the specs', since usually awk manuals define %g, %f, etc., in
terms of C printf -- but I haven't checked.
Best regards,
--Terry
At 08:14 PM 8/24/2007 +0200, Alan Barrett wrote:
>On Fri, 24 Aug 2007, David Laight wrote:
> > I believe the current code is correct (but I can't remember why I
> > think that!) ie the difference between +0 and -0 should be visible to
> > programs.
>
>Oh, I believe that programs that really care should be able to tell the
>difference. For example, they could use code like this:
>
> double poszero = 0.0;
> double negzero = -poszero;
> if (copysign(1.0, poszero) != copysign(1.0, negzero) {
> /* signs of poszero and negzero are different */
> }
>
>My questions are more about what should happen to programs that just
>want to do some arithmetic and print the results using printf or
>similar, without taking special care to differentiate between positive
>and negative zero.
>
>--apb (Alan Barrett)