float init 0 request
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Wed Aug 26 02:54:37 UTC 2026
On 26/08/2026 4:33 AM, Basile B. 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.
>
> I have followed the topic since the beginning and I see something that's
> never been noted: an inconsistency is boolean evaluation.
>
> ```d
> void main()
> {
> int a;
> assert(!a); // ok
> float b;
> assert(!b); // fails
> }
> ```
You've made a great point.
We should remove truthiness from floats, it doesn't make sense for it to
support it.
More information about the Digitalmars-d
mailing list