digitalmars.D.learn - I'm getting NAN out of nowhere
- Binarydepth (14/14) Jul 09 2015 This is my code :
- Adam D. Ruppe (7/8) Jul 09 2015 You didn't initialize this variable. Set it to 0.0 and it will
- Binarydepth (2/10) Jul 09 2015 Thank you very much!! :D
- bachmeier (7/15) Jul 09 2015 Is there a reason the compiler doesn't identify this as an error?
- Adam D. Ruppe (4/6) Jul 09 2015 The compiler just doesn't really try to trace if variables have
- Steven Schveighoffer (5/17) Jul 09 2015 prom has been initialized, to NaN by the compiler. It's not an accident.
- flamencofantasy (2/16) Jul 11 2015 foreach(nem; 0..3)
- Steven Schveighoffer (8/24) Jul 13 2015 that is a good catch, if the purpose is to fill in all 3 nums elements.
- Binarydepth (3/30) Apr 10 2017 You mean with readf ?
- Binarydepth (2/19) Apr 10 2017 Yes, Thank you!!
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
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
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:Thank you very much!! :Dfloat 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
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: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.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
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
On 7/9/15 1:04 PM, bachmeier wrote:On Thursday, 9 July 2015 at 15:18:18 UTC, Adam D. Ruppe wrote: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. -SteveOn Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote: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.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
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
On 7/11/15 12:57 PM, flamencofantasy wrote:On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote: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] -SteveThis 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 13 2015
On Monday, 13 July 2015 at 14:29:57 UTC, Steven Schveighoffer wrote:On 7/11/15 12:57 PM, flamencofantasy wrote:You mean with readf ?On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote: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] -SteveThis 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)
Apr 10 2017
On Saturday, 11 July 2015 at 16:57:55 UTC, flamencofantasy wrote:On Thursday, 9 July 2015 at 15:14:43 UTC, Binarydepth wrote:Yes, Thank you!!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)
Apr 10 2017