开发者

Different Results in Debug and Release Modes

vector<double> pvec;

double firstnode=0.0;

for(iter2=svec.begin(); iter2!=svec.end(); iter2++)
{
    double price= 0.0;
    string sFiyat = iter2->substr(13);
    stringstream(sFiyat)>>price;
    price=log(price);

    if (iter2==iter)
    {
        firstnode = price;
    }
    price -= firstnode;

    pvec.push_back(price);
}

I got the code above and there is a miracalous difference in debug and release modes. T开发者_运维知识库he algorithm aims to make the first element of the vector equal to zero and then finds the differences of the logarithms of the first element with other elements.

In debug mode, this gives the result that I desire and the first element of the vector is always equal to zero. But when I switch to the release mode the first element of the vector is equal to some small number such as 8.86335e-019.

And that's not all. When I put the line "cout << price << endl;" after the line "price=log(price);" then the result I got from the release version is same with the one from the debug mode. Any explanations?


The debug floating point stack uses full 80-bit precision available in the FPU. Release modes perform on more efficient 64-bit truncated results.

Modify your floating-point behavior to be build independent with /fp http://msdn.microsoft.com/en-us/library/e7s85ffb%28VS.80%29.aspx See http://thetweaker.wordpress.com/2009/08/28/debugrelease-numerical-differences/ as well

Some of the differences you are observing are simply to do with display precision. Make sure to set cout to be full precision before you compare it to the value displayed by the MSVC debugger.


Try turning off optimizations in your release build...


When you use floating point calculations, an error on the order of 8e-19 is as close to zero as you get.

You have an error that is less than a billionth of a billionth of the calculated value. That's pretty close!

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜