Thread overview | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
|
July 09, 2015 I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
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. |
July 09, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | 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.
|
July 09, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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
|
July 09, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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.
|
July 09, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | 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.
|
July 09, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to bachmeier | 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
|
July 11, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to Binarydepth | 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)
|
July 13, 2015 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to flamencofantasy | 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
|
April 10, 2017 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to flamencofantasy | On Saturday, 11 July 2015 at 16:57:55 UTC, 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)
Yes, Thank you!!
|
April 10, 2017 Re: I'm getting NAN out of nowhere | ||||
---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | On Monday, 13 July 2015 at 14:29:57 UTC, Steven Schveighoffer wrote:
> 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
You mean with readf ?
|
Copyright © 1999-2021 by the D Language Foundation