The solution to "Error handling"...
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Sat Jul 4 20:01:28 UTC 2026
On Friday, July 3, 2026 5:57:05 PM Mountain Daylight Time H. S. Teoh via Digitalmars-d wrote:
> As far as D is concerned, defaulting floats to NaN breaks the pattern of
> zero-initialization, which sucks. We should default to 0.0 instead!
Well, types in general don't have zero initialization. It's just the integer
types which do. And pointers too, I guess, since null is technically 0, but
from D's perspective, that's an implementation detail. And the character
types don't initialize to 0 even though they're numbers. And user-defined
types often don't initialize to zero. So, I'd argue that there really isn't
a consistent pattern of initializing to zero anyway. Types in general have a
default value, and that value various wildly depending on the type. There
are reasons why it could be argued that initializing floating point values
to 0 would be better, but it really doesn't make the language more
consistent. And from what I've seen, much as most of us absolutely hate the
whole mess with NaNs, the guys who actually do a lot of math with their
programs love it.
And really, floating point values break all kinds of assumptions which types
generally follow - like how NaN behaves with comparisons. It results in
utter nonsense such as a == b and a != b having the same result, which most
code assumes isn't a thing, because sane types do not work that way. So,
pretty much any time that floating point types are involved, they break
normal behavior and expectations to one degree or another.
- Jonathan M Davis
More information about the Digitalmars-d
mailing list