floats default to NaN... why?

F i L witte2008 at gmail.com
Sat Apr 14 00:45:57 PDT 2012


Jonathan M Davis wrote:
> No. You always have a bug if you don't initialize a variable to 
> the value that
> it's supposed to be. It doesn't matter whether it's 0, NaN, 
> 527.1209823, or
> whatever. All having a default value that you're more likely to 
> use means is
> that you're less likely to have to explicitly initialize the 
> variable. It has
> to be initialized to the correct value regardless.

Yes, I'm in favor of default values. That's not my argument here. 
I'm saying it makes more sense to have the default values be 
_usable_ (for convenience) rather than designed to catch 
(**cause**) bugs.


> And if you're in the habit
> of always initializing variables and never relying on the 
> defaults if you can
> help it,

That seams like a very annoying habit requirement for a languages 
with a focus on "modern convenience". What's more bug prone, I 
think, is forcing developers to remember that unset floats 
(specifically) will cause bugs while it's neighboring int works 
perfectly without a explicit value.


> then the cases where variables weren't initialized to what they 
> were
> supposed to be stand out more.

Usable defaults don't need to stand out because they're usable 
and deterministic. If you want to make sure a constructor/method 
produces expected results, unittest it.. it makes more sense to 
catch errors at compile time anyways.




> D's approach is to say that
> it's _still_ a bug to not initialize a variable, since you 
> almost always need
> to initialize a variable to something _other_ than a default.

Not always, but that's besides the point. The point is that in 
the places where you do want zero values (Vector/Matrix/etc, 
Polygon structs, counters, etc..) it's better to have consistent 
expected behavior from the default value. Not some value that 
causes runtime bugs.


> I don't see how it's an
> issue, since you almost always need to initialize variables to 
> something other
> than the default, and so leaving them as the default is almost 
> always a bug.

To me, it's not a huge issue, only an annoyance. However I 
wouldn't underestimate the impact of bad marketing. When people 
are trying out the language, and they read "variables are 
defaulted, not garbage" do you think they're going to expect ints 
and floats to work in different ways?

And it doesn't cause bugs to default value types to zero. I have 
enough experience with C# to know that it doesn't. All it does is 
make the language more consistent.


> The only point of dispute that I see in general is whether it's 
> better to rely
> on the default or to still explicitly initialize it when you 
> actually want the
> default.

This sounds like an argument for C++. Explicit declaration isn't 
a guard against bugs, you'll still be hunting through code if 
something is incorrectly set.

The fact of the matter is default initialization _does_ happen in 
code, no matter how philosophically correct "always explicitly 
define value" might be. Unless that's enforced, it can and should 
be expected to happen. That given, it's more convenient to have 
consistent value type behavior. Float is a Value type and 
shouldn't be subjective to the security concerns of Reference 
types.


> Regardless, the _entire_ reason for default-initialization in D 
> revolves
> around making buggy initializations deterministic and more 
> detectable.

The same way string, and int are defaulted to usable values, 
float should be as well. Catching code that works with null 
pointers is one thing. Working with a value type without 
requiring the programmer have deeper knowledge about D's 
asymmetrical features is another.

If D doesn't accommodate entering Laymen, how does it expect to 
gain popularity in any major way? Efficiency puts D on the map, 
convenience is what brings the tourists.

I'm not trying to convince you that the sky is falling because I 
disagree with D's direction with floats.. just if the goal is to 
increase D's popularity, little things may turn heads away 
quicker than you think. My original post was inspired by me 
showing my D code to another C# guy earlier, and coming up with 
poor explanations as to why floats where required to be defaulted 
in my math lib. His reaction what along the lines of my first 
post.


More information about the Digitalmars-d-learn mailing list