halilak
halilak

Reputation: 39

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?

Upvotes: 3

Views: 8512

Answers (3)

totowtwo
totowtwo

Reputation: 2147

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.

Upvotes: 6

Bo Persson
Bo Persson

Reputation: 92361

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!

Upvotes: 1

therealmitchconnors
therealmitchconnors

Reputation: 2760

Try turning off optimizations in your release build...

Upvotes: 1

Related Questions