Movement against float.init being nan

Walter Bright newshound2 at digitalmars.com
Sat Aug 20 01:01:07 UTC 2022


On 8/19/2022 12:09 PM, Steven Schveighoffer wrote:
> But it defaults to a completely useless value (NaN).

It is not useless. NaN's have many uses, one of which is to not have silent bugs 
where you forgot to initialize a double.

> This is unexpected, and 
> commonly leads to hours of head-scratching (see Adam's Ruppe's 2020 live coding 
> session, where he couldn't figure out for much of the stream why his game wasn't 
> working).

It's fewer hours then tracking down why it is 0 instead of 6, because 0 doesn't 
leave a trail.


> D used default values to prevent errors in not initializing values, but default 
> initialization to a very commonly-expected value turns to be incredibly useful.

I've lost days in debugging issues with forgetting to initialize a variable.


> The design choices for float/double were made based on the fact that a value 
> that means "this isn't initialized" existed. It didn't for int, so meh, 0 was 
> chosen. Walter could have easily have just chosen int.min or something, and then 
> we would possibly not ever be used to it. But now we are used to it, so it has 
> become irksome that doubles/floats are the outlier here.

If there was a NaN value for int, I would have used it as the default. int.min 
is not really a NaN.

The NaN value for char is 0xFF, and for pointers is null. Both work well. (0xFF 
is specified as an illegal code point in Unicode.) It's integers that are the 
outliers :-)



More information about the Digitalmars-d mailing list