float init 0 request
Walter Bright
newshound2 at digitalmars.com
Fri Aug 14 17:54:25 UTC 2026
On 8/14/2026 1:17 AM, Indraj Gandham wrote:
> On Friday, 14 August 2026 at 06:38:02 UTC, Walter Bright wrote:
>> It's similar to pointers being default initialized to null. Trying to use a
>> null pointer will result in a seg fault.
>
> It's not similar at all. There is no sensible init value for a pointer but there
> is for a float.
I'm afraid we disagree. The null pointer initialization ensures that you cannot
dereference an uninitialized pointer and not find out about it. Getting a "NaN"
in your program's output is sure to notify the user that there is a BUG in the
program. Default initializing to 0.0 runs the high risk of the bug not being
noticed.
> This change would bring more consistency within D with regard to
> the behaviour of numeric types. It would also bring D in line with what other
> languages do (and what most programmers likely expect).
This is why D is better than other languages.
>> 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.
>
> It's better to find it at compile time and emit a warning, as in Rust.
Consider a scientist setting out an array of data collection sensors. If you've
got a lot of them, inevitably some will be bad. A bad one should not bring down
the whole system. Having the bad data replaced with NaN will mean the rest of
your array data will not be corrupted by a stuck-at-zero problem with one sensor.
> The NaN value silently propagates and is therefore the worst possible choice.
The worst possible choice is the maintainer shutting up the compiler and
initializing it to 0.0 without checking what the correct value should be. And
you may never realize that this 0.0 propagated into your output. Yes, this
happens. I've seen it happen.
Having a NaN in your output tells you that your code is broken. A default 0.0
hides bugs.
More information about the Digitalmars-d
mailing list