-0 assigned to a FP variable

Robert Jacques sandford at jhu.edu
Sun Jan 9 18:29:39 PST 2011


On Sun, 09 Jan 2011 20:39:12 -0500, Jonathan M Davis <jmdavisProg at 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 )).


More information about the Digitalmars-d mailing list