float init 0 request

Richard (Rikki) Andrew Cattermole richard at cattermole.co.nz
Fri Aug 14 14:08:07 UTC 2026


On 14/08/2026 5:57 PM, ABrightLight wrote:
> 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.

I mentioned this else where, the reason it doesn't cause a hardware 
exception was a decision that D made, as it had bad interactions.

The CPU can do it.

> 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.

Thanks.

It is based upon preventing uninitialized variable reads.

The main difference is it has been special cased so only a mathematical 
operation will trigger it. Other reads like returning it, won't.

This is to prevent false positives, but could be changed if so desired.



More information about the Digitalmars-d mailing list