[LCP]Negative zero problem

Mike & Penny Novack stepbystepfarm at shaysnet.com
Mon Oct 29 23:59:28 UTC 2001


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.

Mike




----- Original Message -----
From: Chuck Martin <nrocinu at myrealbox.com>
To: Linux C Programming <linuxCprogramming at lists.linux.org.au>
Sent: Monday, October 29, 2001 3:52 AM
Subject: [LCP]Negative zero problem


> I tried mailing this several times before, but it never seemed to get
> through.  I now have a new address, so maybe it will get through from
> here (I'm wondering if it was being blocked due to a misguided attempt
> to block spam).
>
> I'm working on a program (the public domain spreadsheet, sc, if
anyone's
> interested) that contains the following snippet:
>
>     if (val < 0.0) {
>         negative = true;
>         val = -val;
>     }
>
> This appears to be related to a bug that I'm trying to track down, so
> I inserted some diagnostic code both immediately before and
immediately
> after this snippet that uses fprintf to print the value of val to
stderr,
> which I redirect to another virtual console when I start the program.
> The bug only seems to occur when val (which is a double) should be 0,
> and in the case where the bug occurs, for some reason, the value is
> displayed as -0 both before and after (using a conversion specifier of
> %.15g).  How is this possible?  Could someone explain what's going on
> here?  If I insert diagnostic code inside the snippet, it never gets
> executed, so the test must be false, so why does the fprintf (or any
> of the other functions in the printf family) prefix the "-"?
>
> Chuck
>
> _______________________________________________
> This is the Linux C Programming List
> :  http://lists.linux.org.au/listinfo/linuxcprogramming List
>




More information about the linuxCprogramming mailing list