www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - I'm getting NAN out of nowhere

reply "Binarydepth" <binarydepth gmail.com> writes:
This is my code :
import std.stdio : writeln, readf;
void main()	{
	int[3] nums;
	float prom;
	foreach(nem; 0..2)	{
		writeln("input a number : ");
		readf(" %d", &nums[nem]);
		prom+=nums[nem];
	}
	writeln(prom/3.0);
}

I get prompted two times for a number and I then get NAN out of 
nowhere.
Jul 09 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
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.
Jul 09 2015
next sibling parent "Binarydepth" <binarydepth gmail.com> writes:
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.
Thank you very much!! :D
Jul 09 2015
prev sibling parent reply "bachmeier" <no spam.com> writes:
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.
Jul 09 2015
next sibling parent "Adam D. Ruppe" <destructionator gmail.com> writes:
On Thursday, 9 July 2015 at 17:04:43 UTC, bachmeier wrote:
 Is there a reason the compiler doesn't identify this as an 
 error?
The compiler just doesn't really try to trace if variables have actually been initialized or not. It punts it to runtime for simplicity of compiler implementation.
Jul 09 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/9/15 1:04 PM, bachmeier wrote:
 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.
prom has been initialized, to NaN by the compiler. It's not an accident. All data is default initialized in D unless specifically initialized to void. -Steve
Jul 09 2015
prev sibling parent reply "flamencofantasy" <flamencofantasy gmail.com> writes:
On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote:
 This is my code :
 import std.stdio : writeln, readf;
 void main()	{
 	int[3] nums;
 	float prom;
 	foreach(nem; 0..2)	{
 		writeln("input a number : ");
 		readf(" %d", &nums[nem]);
 		prom+=nums[nem];
 	}
 	writeln(prom/3.0);
 }

 I get prompted two times for a number and I then get NAN out of 
 nowhere.
foreach(nem; 0..3)
Jul 11 2015
parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 7/11/15 12:57 PM, flamencofantasy wrote:
 On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote:
 This is my code :
 import std.stdio : writeln, readf;
 void main()    {
     int[3] nums;
     float prom;
     foreach(nem; 0..2)    {
         writeln("input a number : ");
         readf(" %d", &nums[nem]);
         prom+=nums[nem];
     }
     writeln(prom/3.0);
 }

 I get prompted two times for a number and I then get NAN out of nowhere.
foreach(nem; 0..3)
that is a good catch, if the purpose is to fill in all 3 nums elements. Note, a future-proof version would say: foreach(nem; 0..nums.length) A more d-idiomatic way is to say: foreach(ref nem; nums) And then use nem anywhere you see nums[nem] -Steve
Jul 13 2015