float init 0 request
ABrightLight
example at example.com
Fri Aug 14 05:57:55 UTC 2026
Hello. I understand this subject's been discussed a number of
times before, but I'd like to throw in my reasoning for why it
would benefit us more for floats to initialize to 0 than NaN.
1. float is generally used for numeric computation (things like
NaN boxing are examples of uses outside of this) and as such have
similarity to the integer types (they will even implicitly coerce
mathematical operations to float). Since integers default to 0,
floats should too.
1a. It may be argued that integers only default to 0 because
there is no meaningful "null" value, but in my opinion this is
not convincing because I think that most people would opt for
them getting a reasonable default value right from the start
instead of being forced to `= 0;` It is very common to see loop
variables and other forms just say `int i;` or similar, and no
one is confused by this.
2. It is argued that since, like pointers that have `null` and
are default initialized to this, floats should also initialize to
its version of `null`. However this is a false equivalence since
the use of a null pointer is specified/standardized to crash the
program due to a concern for memory safety (it's not even set to
undefined behavior, a program halt must occur). The same memory
safety concern is not present for most operations relying on
floats, and the existence of NaN being left without a crash [or
even an exception thrown] is to cater to performance. For cases
where debugging by program halt is required, most of the time the
approach done is for a compiler switch to allow throwing an
exception instead of just halting the program, since a stack
trace is presumed to be desired.
3. I know that rikki's DFA implementation will be able to detect
when a `float x;` occurs without then being actually initialized
to something useful (keep up the great work rikki!), but this
particular feature is a special case that is not exactly within
scope. And the fact that this is even being added as a special
case should be a hint that leaving NaN as the default init is
much less useful than some integer-compatible value like 0.
P.S. This subject is a bit subjective (no pun intended) but
overall we'd likely see more user satisfaction from the float
init switch. And from what I could tell, it's been well received
by the OpenD folks, so there's already been a bit of a testbed
for this.
More information about the Digitalmars-d
mailing list