-0 assigned to a FP variable

Jonathan M Davis jmdavisProg at gmx.com
Sun Jan 9 17:39:12 PST 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


More information about the Digitalmars-d mailing list