View mode: basic / threaded / horizontal-split · Log in · Help
January 10, 2011
-0 assigned to a FP variable
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
January 10, 2011
Re: -0 assigned to a FP variable
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
January 10, 2011
Re: -0 assigned to a FP variable
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
January 10, 2011
Re: -0 assigned to a FP variable
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:
>> 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.

-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 )).
January 10, 2011
Re: -0 assigned to a FP variable
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?
January 10, 2011
Re: -0 assigned to a FP variable
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
January 10, 2011
Re: -0 assigned to a FP variable
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

> 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 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
January 10, 2011
Re: -0 assigned to a FP variable
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.
Top | Discussion index | About this forum | D home