Question

I'm developing a program in C++ with Visual Studio 2008. Recently i started to test my program in release mode, and i started to get some weird errors in relations to some calculations, which always return 0 instead of the correct value (that in debug mode were correctly calculated).

I have done some research and finally i identify (or at least i believe) what the problem is. If i disable the optimization flags (/Od) when i'm in release mode, the programs behaves well like usual in debug mode. But, because i really need the speed of this optimizations (at least with the O2 flag), i started to test a lot of things in order to get correct results while keep a good execution time. I set the floating point model to pf:strict option, but still get the same errors (my long doubles values still are 0).

Tracking this odd behaviour i get to this example code (which is inside a loop) (demand is a long double var):

long double demand = 0;
vector<int> someArray = getSomeArray();
for (int i = 0; i < someArray.size(); i++) {

    if (someCondition(someArray[i])) {

        int someId = getSomeId(someArray[i]);
        long double d = getSomeLongDouble(someArray[i]); <--- correctly setted!
        demand = demand + d; <---- d=0.018182000000000000 demand = 0
        someLogic(someId); <--- at this point, demand is still 0!!!!
    }

}

No matter what, demand is always 0, even when i insert a breakpoint and manually check that the value of the variable d is > 0. Please, i really need the boost of speed the optimization flags are giving, but i don't get how to keep this particular calculation correct...

Any help would be greatly appreciated :) And excuse my english!!

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top