### Re: += on associative arrays leads to surprising result

On Tuesday, 27 August 2019 at 16:45:53 UTC, Samir wrote: I never understood why the intial value of floats, doubles and reals was NaN. That's for detecting uninitialised variables. If the result of a calculation is NaN, it's likely, that you forgot to initialise the variable.

### Re: += on associative arrays leads to surprising result

On Tuesday, 27 August 2019 at 16:12:07 UTC, berni wrote: What's your oppinion on this? As someone relatively new to programming in general and to D in particular, this behavior does, on the surface, seem inconsistent. Good to see that a bug exists for this, per ag0aep6g. I never understood why the intial value of floats, doubles and reals was NaN. Samir

### Re: += on associative arrays leads to surprising result

On 27.08.19 18:12, berni wrote: import std.stdio; void main() { real[int] a; a[0] += 100; writeln(a); } results (independed of the used compiler) in [0:100] I was a little bit surprised, because a[0] += 100 should be the same as a[0] = a[0]+100, which leads to a range violation error. Furthermore, as we work with real, I'd expected the result to be NaN... Is this a bug? I ask, because it would be quite convenient to use it the way it works now. For what it's worth, it's in Bugzilla: https://issues.dlang.org/show_bug.cgi?id=4463

### += on associative arrays leads to surprising result

import std.stdio; void main() { real[int] a; a[0] += 100; writeln(a); } results (independed of the used compiler) in [0:100] I was a little bit surprised, because a[0] += 100 should be the same as a[0] = a[0]+100, which leads to a range violation error. Furthermore, as we work with real, I'd expected the result to be NaN... Is this a bug? I ask, because it would be quite convenient to use it the way it works now. An alternative I found, would be to use object.update. But there I've to declare the 100 twice which results in code duplication: a.update(0,()=>100.0L,(ref real v)=>v+100.0L); Hence, my best solution needs two lines: if (0 !in a) a[0] = 0; a[0] += 100; What's your oppinion on this?