floats default to NaN... why?

Andrew Wiley wiley.andrew.j at gmail.com
Fri Jun 8 19:53:52 PDT 2012


On Thu, Jun 7, 2012 at 6:50 PM, Minas <minas_mina1990 at hotmail.co.uk> wrote:

> I agree that the default value for floats/doubles should be zero. It feels
> much more natural.
>

The point is to make sure code is correct. Initializing your variables
should be what feels natural. Leaving then uninitialized is bad style, bad
practice, and generally a bad idea. If getting stung by a float
initializing to NaN is what it takes to make that happen, so be it.

>
> I think the problem here is that people are thinking about some stuff too
> much. D is a rather new language that wants to be practical. Floats
> defaulting to NaN is NOT practical FOR MOST PEOPLE when at the same time I
> write:


Actually, as far as I know, floats have been initialized to NaN since D
first appeared in 2001. This isn't news.

>


> int sum;
>
> for(...)
>  sum += blah blah blah
>
> And it works.
>
In any other systems language, this would be undefined behavior. D is
simply trying to make initialization bugs as obvious as possible. With
ints, the best we can do is 0. With floats, NaN makes it better.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20120608/b802a1ea/attachment.html>


More information about the Digitalmars-d-learn mailing list