Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. The 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?

like image 929
halilak Avatar asked Jan 19 '23 10:01

halilak


1 Answers

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.

like image 146
totowtwo Avatar answered Jan 29 '23 02:01

totowtwo