[LCP]Negative zero problem

Chuck Martin nrocinu at myrealbox.com
Wed Oct 31 20:15:08 UTC 2001


On Mon, Oct 29, 2001 at 07:55:08AM -0500, Mike & Penny Novack wrote:
> 
> Probably a representational issue. While mathematically "sign" has no
> meaning for zero in most computer representations all numbers have at
> least an implicit sign. Which gets assigned to zero may vary with
> implementaion. Or even within an implementation in which case there may
> be both a +0 and a -0 (and horrors, these might not test as equal).
> 
> See what happens if you reverse the logic (ie: if > 0.0 do nothing, else
> set "negative" and reverse the sign). Note that this may not correct how
> the value prints "after" (ie: there may be no +0.0 representation) but
> at least you will have confirmed your problem.

Thanks for the reply, Mike.  I should probably mention that this problem
doesn't occur every time val is zero, and I haven't been able to figure
out a pattern so far, but when it fails, it fails consistently, so it's
easy to reproduce.

Anyway, I tried reversing the logic, testing for val >= 0.0, since that's
technically a more accurate reversal of the logic than testing for
val > 0.0, and the results were the same.  If I test for val > 0.0
instead, I get exactly opposite results.  In other words, in cases that
produced a negative zero before, no sign was present, but in cases where
there was no sign before, a sign was present.  Whether or not a sign is
present makes no difference in calculations, however.  They're both
exactly zero.

I still don't know how to fix it, other than to do a test write to a
string with sprintf() and look for the sign, and if it's there, negate
it with val = -val, and don't negate it if the sign isn't present.  This
seems like a horrible kludge, though, and I'd like to avoid it if at all
possible.  Do you have any other ideas?

Chuck




More information about the linuxCprogramming mailing list