I'm getting NAN out of nowhere

bachmeier via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Jul 9 10:04:41 PDT 2015


On Thursday, 9 July 2015 at 15:18:18 UTC, Adam D. Ruppe wrote:
> On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote:
>> 	float prom;
>
> You didn't initialize this variable. Set it to 0.0 and it will 
> work.
>
> Like how pointers are initialized to null automatically in D, 
> floats are auto initalized to NaN in D. The idea is to make use 
> of an uninitialized variable obvious quickly so you are 
> encouraged to initialize it.

Is there a reason the compiler doesn't identify this as an error? 
prom+=nums[nem]; doesn't make sense if prom hasn't been 
initialized. I'm not seeing how treating it as NaN is a solution. 
Since it's possible that an NaN value is legitimate, you'd either 
have to litter your code with assertions, or take a chance that 
something that a problem will show up at a later date.


More information about the Digitalmars-d-learn mailing list