debug and release mode answers different

vallikumari
vallikumari used Ask the Experts™
on
I have code similar to this

counter=0;
inf=exp(1000.0);
for(i=0;i<=n;i++)
{
if(variable==inf) counter++;
}

counter--;

When I am running this code in debug mode, it is working well. But in release mode I am getting wrong results.

However when I modify this code as below, I am getting correct results in both release and debug modes.
Can someone help me out in knowing why so?
counter=0;
inf=exp(1000.0);
for(i=0;i<=n;i++)
{
if(variable==inf) counter++;
printf(" ");
}

counter--;
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Hi vallikumari,

how are inf and variable declared? A long time ago I had a similar problem using float and some casting lead to different results depending on the use of a printf: http://www.experts-exchange.com/Programming/System/Windows__Programming/Q_10169280.html

In short the explanaition was that without the printf further calculations are done with values which still were stored in FPU registers (with at least 80 bits). With the printf these values needed to be transferred from the FPU and thus casted to a smaller type. I know that in my case the printf was used to output the values, in your code it doesn't, so this is different. But it maybe that printf internally uses float or double even if no variable is printed.

BTW: You shouldn't use == to compare floating point variables, this can often fail due to the fact that rounding and casting can lead to very little differences. For example take a look at this code:
double x = 2.0, y = 1.0;
for ( int x = 0; x < 10; x++ )
{
	y += 0.1;
}
bool bEqual = x == y;
std::cout << std::boolalpha << "x: " << x << std::endl << "y: " << y << std::endl << "equal: " << bEqual << std::endl;

Open in new window

The output (compiled with VS 2010, Debug build) is:
x: 2
y: 2
equal: false

Open in new window

You can see why the bEqual is false when you print x and y with higher precision - to do so add some setprecision replace the last line with this one:
std::cout << std::boolalpha << std::setprecision( 20 ) << "x: " << x << std::endl << "y: " << y << std::endl << "equal: " << bEqual << std::endl;

Open in new window

Now the output is
x: 2
y: 2.0000000000000009
equal: false

Open in new window

So, to compare to doubles you should use a functionality which ignores these little difference. Double has a precision of 15 digits (see http://en.wikipedia.org/wiki/Double_precision) so a suitable comparsion could be done like this:
bool bEqual = fabs( x - y ) < 1e-15;

Open in new window

Using this the output is the expected one:
x: 2
y: 2.0000000000000009
equal: true

Open in new window


Hope that helps,

ZOPPO


Top Expert 2016
Commented:
to add to above: in debug mode the debugger initializes all variables with zeros. it also adds addional storage for each variable for debugging purposes. so if for example n is not initialized in debug mode the for loop would not be performed while in release mode you might have any random value for n.

same applies for all other variables used. so i recommend to initialize alle variables, arrays and pointers and check all boundaries to get same results for debug and release.

Sara
I could get the problem solved  by
assigning 1.7*exp(308) instead of exp(1000). It is the largest double.
The exp(1000) was different in release and debug modes.
thanks all of you for your time

Author

Commented:
The solution worked

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial