Movement against float.init being nan
Steven Schveighoffer
schveiguy at gmail.com
Fri Aug 19 19:09:36 UTC 2022
On 8/19/22 2:04 PM, bachmeier wrote:
> On Friday, 19 August 2022 at 17:14:35 UTC, Steven Schveighoffer wrote:
>
>> I also would prefer that all floats/doubles default to 0 instead of NaN.
>
> It would be awful to choose an arbitrary, often incorrect value in order
> to give the appearance that your program is running.
The problem is that most people declare a number like `int x` and expect
it to default to 0. Because that's what D does.
For a float (number), they expect the same thing.
But it defaults to a completely useless value (NaN). 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 would be absurd to silently set the value of `z` to 1.0 in this code:
>
> ```
> double w;
> double z = w*2.5 + 1;
> ```
>
Not absurd at all. if `w` defaults to 0, I would expect `z` to be `2.5 *
0 + 1`.
Change it to `int`, and see if it looks absurd to you. You just aren't
used to it.
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.
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.
-Steve
More information about the Digitalmars-d
mailing list