www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - why is the default floating point value NAN ?

reply Codifies <a b.com> writes:
I'd have thought it ought to be 0.0 ?

So far I seen carefully considered and sensible reasons for doing 
things in D, so why NAN ?
Oct 17 2018
next sibling parent reply Codifies <a b.com> writes:
On Wednesday, 17 October 2018 at 15:48:16 UTC, Codifies wrote:
 I'd have thought it ought to be 0.0 ?

 So far I seen carefully considered and sensible reasons for 
 doing things in D, so why NAN ?
okay I should have carried on reading the blog, its so uninitialized values stick out when debugging...
Oct 17 2018
parent Dennis <dkorpel gmail.com> writes:
On Wednesday, 17 October 2018 at 15:51:21 UTC, Codifies wrote:
 okay I should have carried on reading the blog, its so 
 uninitialized values stick out when debugging...
Indeed, the initial value is not supposed to be useful, it's there because dealing with garbage memory when forgetting to initialize a variable is hard to debug. That's also why characters are initialized to 0xFF. Unfortunately, (associative) arrays and integers initialize to an empty array and 0 respectively by a lack of an 'invalid' value. These are often useful initial values, so people who didn't know about / didn't agree with that philosphy started using default initialization. Even the 'Count fequencies of all 2-tuples' example on the dlang homepage uses an uninitialized associative array.
Oct 17 2018
prev sibling parent Adam D. Ruppe <destructionator gmail.com> writes:
On Wednesday, 17 October 2018 at 15:48:16 UTC, Codifies wrote:
 I'd have thought it ought to be 0.0 ?

 So far I seen carefully considered and sensible reasons for 
 doing things in D, so why NAN ?
You are supposed to initialize your own variables explicitly. NaN is a somewhat easy way to indicate that you forgot to do that.
Oct 17 2018