The solution to "Error handling"...

Walter Bright newshound2 at digitalmars.com
Sat Jul 4 23:55:22 UTC 2026


Of course. The issue is really that floating point is the best we can do when 
attempting to represent irrational numbers. We cannot even represent them on 
paper, other than as symbols like pi.

Before floating point, we had slide rules. They didn't have NaNs, but were only 
good for 3 digits. There is no way to accurately calculate an irrational number 
- not with pen and paper, not with slide rules, not with calculators, and not 
with computers. Not even a decimal type will slay that dragon.

But back to NaNs in particular.

Here's what sparked my interest in NaNs:

Back in the bad old C days,
```c
float f;
```
was uninitialized, i.e. would have a garbage value. This would often go 
unnoticed, silently corrupting results. At some point years later, a more 
advanced compiler would give: "Warning: use of uninitialized variable". The 
engineer who wrote it was long gone, and the sad sack maintainer had to fix it. 
So Mr. Sad Sack didn't know what the initialization should be, so "zero should 
be good enough for anyone" and 0.0 was added, and it compiled without error, and 
so it was all good.

And never mind the computation it was supposed to generate was wrong, and the 
error possibly not detected.

D has a philosophy of "doing the wrong thing should be harder than doing the 
right thing." We see this in how things like void initializations work, you have 
to do it deliberately.

So,
```d
float f;
```
is doing the wrong thing, and so it gets initialized by NaN to draw attention to 
it being wrong. Then, we hope, it will be properly initialized by the engineer 
who writes the original code, not some junior maintainer who has no idea what 
the correct value should be and so just inserts 0.0 to shut the compiler up.

The bottom line is default 0.0 initialization hides bugs, and NaN exposes bugs. 
I prefer the latter.


More information about the Digitalmars-d mailing list