floating point - nan initializers

Walter Bright newshound at digitalmars.com
Sun Feb 19 02:32:22 PST 2006


"Jarrett Billingsley" <kb3ctd2 at yahoo.com> wrote in message 
news:dt9cl0$1son$1 at digitaldaemon.com...
> "Walter Bright" <newshound at digitalmars.com> wrote in message 
> news:dt98cn$1ord$4 at digitaldaemon.com...
>> Default initializing to nan is part of that - it forces the user to 
>> *think* about what he wants the initial value to be. Initializing it by 
>> default to 0 means that it can easilly be overlooked, and 0.0 can 
>> introduce undetected, subtle errors in the result.
> And most of the time, I *think* that I want it to be 0.  It's a rare 
> occasion that I don't want it to be.  And I already explained that nan 
> gives me more problems than 0 would - especially in systems where the 
> values of the numbers aren't displayed in numerical form.

If it was default initialized to 0, there'd be no way to detect the errors 
that would happen in those rare cases where you didn't want it initialized 
to 0. At least with the nan initialization you *know* there's a bug. The nan 
default initialization is not there for convenience, it's there to flush out 
bugs.

> And so it is, then, that the two numerical types in D - integral and 
> floating point - have two different initializers.  It makes no sense.

Every type has the default initializer that is most appropriate for that 
type, and each makes sense in the context of its type. Float does doesn't 
need to make sense in the context of int - it's very different from int, 
more than just nans, and treating it like an int is a sure route to bugs.

> And again, I wonder to myself why I'm trying to convince you, because I 
> know it won't happen.  I keep forgetting that one has to be named Don, 
> Jill, or Matthew for one's suggestions to even be considered.

Consideration is not the same thing as adopting. D can't adopt every 
suggestion - especially since most are mutually contradictory. (Jill thought 
default 0 initialization for chars makes no sense, you think non-zero makes 
no sense.) I'm trying to explain the rationale for how it works, that it is 
not some thoughtless irrational decision. If you don't agree with the 
rationale, that's fine, but there *is* a rationale.

If anyone thinks I've agreed with every proposal they've made, or even a 
fraction of them, I think they'd vehemently disagree <g>. 





More information about the Digitalmars-d mailing list