float init 0 request
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Fri Aug 14 14:02:26 UTC 2026
On 14/08/2026 8:19 PM, ABrightLight wrote:
> On Friday, 14 August 2026 at 06:38:02 UTC, Walter Bright wrote:
>> Floats are default initialized to NaN so people will be coerced to
>> explicitly code what they want the initialized value to be.
>>
>> I've seen enough code where the programmer forgot to initialize a
>> float, it was defaulted to 0, and the wrong result was not detected.
>>
>> ```d
>> float f; // code reviewer: did the programmer intend it to be 0?
>> float g = 0; // code reviewer: yes, the programmer likely meant to
>> initialize it to 0
>> ```
>>
>> It's similar to pointers being default initialized to null. Trying to
>> use a null pointer will result in a seg fault.
>>
>> Most people decry this, but it's actually a great feature. If there's
>> a bug in the code, it's better to find it sooner rather than after you
>> ship.
>
> Hi Walter, I really appreciate the response, however I think that all
> the points I wrote were unaddressed (I mentioned the comparison with
> null pointers and why it is my opinion that it is a false equivalence as
> well).
Unfortunately this is a good example of Walter leaving out the analysis,
and skipping to the conclusion. I spend a good chunk of my time figuring
out what the analysis was lol.
NaN's come in two flavors, signaling and non-signaling.
Originally D default initialized floats to signal, but that was changed
as it was producing an inconsistency that was a problem (I wasn't
involved with this).
If you try to do math on a signaling NaN it will throw a hardware
exception, just like doing a null dereference does.
A non-signaling NaN is good, because you can at least detect when
something has gone wrong, just not the source.
Silent corruption is always worse than detectable corruption.
> Nevertheless, the topic is relatively low importance to me and I'm okay
> with just using a linter to remind myself to explicitly initialize the
> floats. I just figured the change would be an easy QOL improvement for
> most users.
https://forum.dlang.org/thread/tcfqveygmebplsgmexgr@forum.dlang.org
More information about the Digitalmars-d
mailing list