Movement against float.init being nan

Walter Bright newshound2 at digitalmars.com
Sun Aug 21 15:26:54 UTC 2022


On 8/21/2022 2:01 AM, claptrap wrote:
> So from my experience the rational is based on two fallacies.
> 
> 1. That using zero as default init would hide the bug.

If the computed answer is 1.7239 rather than 1.7230 because of an erroneous 0 
input, how would you know it is wrong? But if the computed answer is NaN, you 
*know* it is wrong.


> 2. That people will just stick zero in because they don't know what it should be.

I know *you* wouldn't do it (I wouldn't either), but like I wrote, I've seen it 
happen. I've also seen cases of:

     double x = 0;
     ... no uses of x ...
     x = 5;
     ...

where the initialization was put in to shut the compiler up. But to one 
unfortunate enough to be reviewing unfamiliar code, this double initialization 
has a smell about it - which initialization is the correct one? Why was it 
initialized to 0 when that makes no sense for the algorithm?

It is also why if it is desirable for a variable to be uninitialized, the

     double x = void;

form must be used, as that is highly unlikely to be written by accident.

I.e. this is all designed to encourage the programmer to write code 
intentionally, rather than accidentally.

BTW, I've done user support (helping them debug problems with their code) for 40 
years now. I've talked a lot with team managers about what problems they 
experience with code. A lot of D's unusual features come from this experience.


More information about the Digitalmars-d mailing list