开发者

operator+= yields zero

开发者 https://www.devze.com 2023-03-10 07:52 出处:网络
[edit]Problem solved - kind of. There never really was a problem. It seems that the debugger was lying to me. Thanks[/edit]

[edit]Problem solved - kind of. There never really was a problem. It seems that the debugger was lying to me. Thanks[/edit]

I have a wierd problem that I've been unable to resolve so far. I have a private class member

private:
float *_histVals;

which is initialized in the constructor as follows:

// allocate memory for the histogram bins
size_t hSize = binsPerChan*binsPerChan*binsPerChan;
this->_histVals = new float[ hSiz开发者_高级运维e ];
for (size_t i = 0; i < hSize; ++i)
    this->_histVals[i] = 0.0f;

later in the program I want to add something to certain array elements in the following way:

this->_histVals[ hIndex ] += imgFrac;

Here, imgFrac is float value of the magnitude 1.0*e-5 and the current value of the _histVals[ hIndex ] Element is zero. However the debugger shows that after the execution of this line the value is still zero.

I suspected a problem with floating point precision but when I try the same thing with a local float variable it works perfectly. So it must have something to do with the array itself. But I've no idea what that possibly could be. I've also checked the index which is within array bounds.

Any suggestions what could cause the problem? Thanks


Use std::vector<float> unless you want a difficult life or you're learning how people had to live in the bad old days.

Regarding this having no effect on the array element:

this->_histVals[ hIndex ] += imgFrac;

Possible causes:

  • The debugger is lying to you.
  • imgFrac is zero

Have you tried logging all the values involved to std::cout? Also to convince yourself there's no magic involved, try breaking it down into:

float newValue = this->_histVals[ hIndex ] + imgFrac;
this->_histVals[ hIndex ] = newValue;

And log the newValue.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号