debug and release mode answers different

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--;
vallikumariAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ZoppoCommented:
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


0
sarabandeCommented:
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
0
vallikumariAuthor Commented:
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
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
vallikumariAuthor Commented:
The solution worked
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.