float init 0 request

Walter Bright newshound2 at digitalmars.com
Sat Aug 22 19:44:51 UTC 2026


On 8/21/2026 4:22 PM, claptrap wrote:
> My argument is that given that NaN doesnt guarantee you will get an obvious 
> wrong value,

Right, when you use NaN in a comparison. 0.0 used in a comparison doesn't work 
any better at error detection, however.

> and that zero init will likely reduce the total amount of errors,

Better to reduce the errors to zero as much as practical.

> the trade of is less clear cut that it would seem. Because you are arguing **a 
> single case**, where zero is wrong, and NaN always makes it obvious. That's 
> cherry picking.

If you run a floating point operation that is non-trivial, like a matrix 
inversion, the likelihood of noticing that the output is wrong is much lower 
than seeing an output of NaN.

It is a fact that people mistakenly neglect to initialize a variable. It is also 
true that 0.0 is more likely to be what was intended than any other value.

But that is not the point with NaN. The point is to make it always the wrong 
value, and it is far, far, far more likely to be detected than a wrong 0.0 
initialization.

> Lets be honest, if zero init was such a source of bugs, it'd be an easy case to 
> make because we'd all be so sick of hunting zero init integer bugs. But no one 
> ever makes that point, because it's really a non issue.

I've seen such errors, and while they don't occur often, they can be hard to 
track down. I recall one that took a maddening 3 days to find.

> If you had chose zero init for floats 20 years ago everyone would yeah fine.

I totally agree that doing what other languages do would be yeah fine. But D 
tries to help write correct code, not code that seems to be correct.

BTW, my C compiler was the first to issue a warning for this:
```C
for (int i = 0; i < 10; ++i);
     printf("%d\n", i);
```
The inspiration for the warning was when a colleague told me my compiler had a 
bug in it because the printf only ran once.

Over time, this feature was copied by other compilers. With D, there was an 
opportunity to make it an error rather than a warning.



More information about the Digitalmars-d mailing list