Floating point types default to NaN?

A Guy With a Question aguywithaquestion at gmail.com
Sat Nov 25 16:16:52 UTC 2017


On Saturday, 25 November 2017 at 09:39:15 UTC, Dave Jones wrote:
> On Friday, 24 November 2017 at 22:38:49 UTC, Jonathan M Davis 
> wrote:
>> On Friday, November 24, 2017 20:43:14 A Guy With a Question 
>> via Digitalmars- d-learn wrote:
>>> On Friday, 24 November 2017 at 14:43:24 UTC, Adam D. Ruppe
>> That requires data flow analysis, which the compiler doesn't 
>> do a lot of, because it can be complicated. It also tends to 
>> result in the compiler giving warnings or errors in cases 
>> where it's not actually true that the variable is used before 
>> it's given a value, because it can't do it perfectly. There 
>> was a recent discussion on this in the main newsgroup with 
>> regards to guaranteeing with a pointer or reference was 
>> initialized to something other than null.
>
> I think he means just spew an error if a float is declared but 
> not explicitly initialised... Whats the problem with...
>
> float foo; // compiler error "floats must be explicitly 
> initialised"
> float foo = float.nan; // old behaviour.
>
> I mean at the end of the day, that would turn a run time error 
> into a compile time error which is a good thing isnt it?

That is exactly what I meant. I think most languages just choose 
to default it to something convenient, which is 0. C++ chooses 
not to default it for speed reasons. If D chooses it's defaults 
to make errors stick out, why not just error at declaration if 
they don't explicitly set it to something. Including void, which 
would give you C++ undefined behavior from what I've read so far.




More information about the Digitalmars-d-learn mailing list