digitalmars.D - -0 assigned to a FP variable
- bearophile (11/11) Jan 09 2011 A bug I've introduced once in my D code, are you able to spot it?
- Jonathan M Davis (10/28) Jan 09 2011 I didn't even know that there _was_ such as thing as + or - 0 (or 0.0). ...
- Robert Jacques (6/36) Jan 09 2011 -0.0 is an artifact of the floating point sign bit: i.e. there is a + an...
- Jim (4/20) Jan 09 2011 This is not really a bug.
- Walter Bright (4/16) Jan 09 2011 This is not a bug in the language or compiler. While there is such a thi...
- Justin Johansson (7/18) Jan 10 2011 Hi bearophile,
- Justin Johansson (13/29) Jan 10 2011 The butterfly answers back to you.
- Olivier Pisano (8/19) Jan 10 2011 Hi,
A bug I've introduced once in my D code, are you able to spot it?
void main() {
double x = +0;
double y = -0;
}
The bug: 'y' isn't the desired double -0.0
To avoid this bug DMD may keep the -0 and +0 integer literals represented in
two distinct ways, so they have two different values when/if assigned to a
floating point. (But here D will behave a little differently from C).
An alternative is to just add a warning to DMD.
A third possibility is to just ignore this probably uncommon bug :-)
Bye,
bearophile
Jan 09 2011
On Sunday 09 January 2011 16:27:11 bearophile wrote:
A bug I've introduced once in my D code, are you able to spot it?
void main() {
double x = +0;
double y = -0;
}
The bug: 'y' isn't the desired double -0.0
To avoid this bug DMD may keep the -0 and +0 integer literals represented
in two distinct ways, so they have two different values when/if assigned
to a floating point. (But here D will behave a little differently from C).
An alternative is to just add a warning to DMD.
A third possibility is to just ignore this probably uncommon bug :-)
Bye,
bearophile
I didn't even know that there _was_ such as thing as + or - 0 (or 0.0). I would
have considered it a no-op, being identical to 0 or 0.0, and expected it to be
compiled out completely. I haven't a clue what -0.0 would even mean. But I'm
not
exactly an expert on floating point values, so presumably, there's some weird
floating point thing that - affects. Honestly, the more I deal with floating
points, the more I wish that we were dealing with fixed points. Granted,
there's
probably a number of reasons why floating points are better than fixed points,
but
in general, I just don't see it.
- Jonathan M Davis
Jan 09 2011
On Sun, 09 Jan 2011 20:39:12 -0500, Jonathan M Davis <jmdavisProg gmx.com> wrote:On Sunday 09 January 2011 16:27:11 bearophile wrote:-0.0 is an artifact of the floating point sign bit: i.e. there is a + and - for each value, so naturally there's also + and - 0. The difference isn't generally something you care about, as they are practically identical except in bit pattern (i.e. assert( 0.0 == -0.0 )).A bug I've introduced once in my D code, are you able to spot it? void main() { double x = +0; double y = -0; } The bug: 'y' isn't the desired double -0.0 To avoid this bug DMD may keep the -0 and +0 integer literals represented in two distinct ways, so they have two different values when/if assigned to a floating point. (But here D will behave a little differently from C). An alternative is to just add a warning to DMD. A third possibility is to just ignore this probably uncommon bug :-) Bye, bearophileI didn't even know that there _was_ such as thing as + or - 0 (or 0.0). I would have considered it a no-op, being identical to 0 or 0.0, and expected it to be compiled out completely. I haven't a clue what -0.0 would even mean. But I'm not exactly an expert on floating point values, so presumably, there's some weird floating point thing that - affects.
Jan 09 2011
This is not really a bug.
I'd expect -0 to be an integer 0. Negative zero doesn't make sense for
integers, and the behavior is consistent with C/C++.
A warning, like you proposed, could possibly help avoiding a mistake.
bearophile Wrote:
A bug I've introduced once in my D code, are you able to spot it?
void main() {
double x = +0;
double y = -0;
}
The bug: 'y' isn't the desired double -0.0
To avoid this bug DMD may keep the -0 and +0 integer literals represented in
two distinct ways, so they have two different values when/if assigned to a
floating point. (But here D will behave a little differently from C).
An alternative is to just add a warning to DMD.
A third possibility is to just ignore this probably uncommon bug :-)
Bye,
bearophile
Jan 09 2011
bearophile wrote:
A bug I've introduced once in my D code, are you able to spot it?
void main() { double x = +0; double y = -0; }
The bug: 'y' isn't the desired double -0.0
To avoid this bug DMD may keep the -0 and +0 integer literals represented in
two distinct ways, so they have two different values when/if assigned to a
floating point. (But here D will behave a little differently from C). An
alternative is to just add a warning to DMD. A third possibility is to just
ignore this probably uncommon bug :-)
This is not a bug in the language or compiler. While there is such a thing as
-0.0, there is no such thing as -0 as being distinct from 0. If you want to
flag
mathematical no-ops as errors, what's next? a+0 is an error, too?
Jan 09 2011
On 10/01/11 11:27, bearophile wrote:
A bug I've introduced once in my D code, are you able to spot it?
void main() {
double x = +0;
double y = -0;
}
The bug: 'y' isn't the desired double -0.0
To avoid this bug DMD may keep the -0 and +0 integer literals represented in
two distinct ways, so they have two different values when/if assigned to a
floating point. (But here D will behave a little differently from C).
An alternative is to just add a warning to DMD.
A third possibility is to just ignore this probably uncommon bug :-)
Bye,
bearophile
Hi bearophile,
This time it really looks like you are trying to pull the wings off of a
butterfly.
But in this case me thinks the butterfly wins.
Best regards and Happy New Year,
Justin Johansson
Jan 10 2011
On 10/01/11 11:27, bearophile wrote:A bug I've introduced once in my D code, are you able to spot it? void main() { double x = +0; double y = -0; } The bug: 'y' isn't the desired double -0.0 To avoid this bug DMD may keep the -0 and +0 integer literals represented in two distinct ways, so they have two different values when/if assigned to a floating point. (But here D will behave a little differently from C). An alternative is to just add a warning to DMD. A third possibility is to just ignore this probably uncommon bug :-) Bye, bearophileA bug I've introduced once in my D code, are you able to spot it? void main() { double x = +0; double y = -0; }The butterfly answers back to you. There is no bug. Did you mean <code> void main() { double x = +0.0; double y = -0.0; } </code> ? Are you able to spot it? Is it still a bug? Cheers Justin
Jan 10 2011
Le 10/01/2011 01:27, bearophile a écrit :
A bug I've introduced once in my D code, are you able to spot it?
void main() {
double x = +0;
double y = -0;
}
The bug: 'y' isn't the desired double -0.0
To avoid this bug DMD may keep the -0 and +0 integer literals represented in
two distinct ways, so they have two different values when/if assigned to a
floating point. (But here D will behave a little differently from C).
An alternative is to just add a warning to DMD.
A third possibility is to just ignore this probably uncommon bug :-)
Bye,
bearophile
Hi,
Do you suggest the compiler should guess you want -0.0 and convert 0 to
double before applying the unary minus operator ?
I suppose such a fix would be likely to create many more bugs than the
one it is suppose to solve.
I don't have a clue on what kind of user code needs to differentiate 0.0
and -0.0 fp litterals, but I humbly suppose that it must be kind of rare.
Jan 10 2011









"Robert Jacques" <sandford jhu.edu> 