p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Ivor Horton's Beginning Visual C++ 2010 (http://p2p.wrox.com/forumdisplay.php?f=569)
-   -   Chap-3. Cycles. compiler options? (http://p2p.wrox.com/showthread.php?t=83880)

AnTiDoD June 4th, 2011 08:53 AM

Chap-3. Cycles. compiler options?
 
Hello! In Chapter 3 cycles with Floating Point written code:
Code:


for (double x = 0.0; x!= 1.0; x+= 0.2)
  cout << x;

The author says that this cycle will never end, but it is not so!

I know about the error of real numbers, but why she is not taking place? (That is if you put x != 2.0, then there will be an infinite loop)

gsallis March 28th, 2012 11:00 AM

This is a duplicate of another post and an errata request has been raised. I will post a new message as and when an errate becomes available.

drpepper June 11th, 2012 02:21 PM

AnTiDoD,

I am making a big leap here. I think the code which produces an endless loop either uses double x = 2.0; or x += 2.0;. The code would look like this:
Code:

for (double x =2.0; x!= 1.0; x+= 0.2)
  cout << x;

or this
Code:

for (double x = 0.0; x!= 1.0; x+= 2.0)
  cout << x;

Something that works for me to answer questions like yours is to play computer on paper, writing down the inputs, outputs, and values of all variables for a complete run of the program. Where different values are used to test the program, I test each value separately.

so... Let's do an abbreviated form of play computer
Code:

for (double x = 2.0; x!= 1.0; x+= 0.2)
  cout << x;

x is declared and initialized to the value of 2.0
x is not equal to 1.0, and never will be
the loop is infinite
Code:

for (double x = 0.0; x!= 1.0; x+= 2.0)
  cout << x;

x is declared and initialized to the value 0.0
x is not equal to 1.0; so the value of x is output
x is incremented by 2.0, making the value 2.0
x is not equal to 1.0, and never will be after this point in time
the loop is infinite

The code you provided works like this.
Code:

for (double x = 0.0; x!= 1.0; x+= 0.2)
  cout << x;

x is declared and initialized to the value of 0.0
x is not equal to 1.0; so the value of x is output
x is incremented by 0.2, making the value 0.2
x is not equal to 1.0; so the value of x is output
x is incremented again repeatedly until the value of x is equal to 1.0
the loop is terminated and the flow passes to the next line of executable code

The intended syntax to illustrate an endless loop could not use x! =2;. Substituting the condition with x != 2.0 produces the same result with the exception that the loop is not terminated until the value of x is 2.0

I hope this is helpful.

drpepper

AnTiDoD June 18th, 2012 11:37 AM

Thanks, but you don't undestand situation.
Your code is right and I agree with it.
I'm saying about ambiguity in presentation float-point numbers in computer. The author had this in mind.

Next code never stopped, and you known why?) I know)
Code:

for (double x = 0.0; x!= 1.0; x+= 0.1)
        cout << x << endl;


drpepper June 18th, 2012 02:43 PM

AnTiDoD,

Thank you for posing the question. You have prompted me to think a little deeper to explore the concepts behind the code. I had not attempted to compile and run the code. I just played computer with it.

I compiled and ran the code and discovered that it does loop endlessly. Here is the code I wrote to test for failure to end the loop:
Code:

//sample_loop.cpp
//does this terminate?

#include <iostream>

using std::cout;
using std::endl;

int main()
{
       
        for (double x = 0.0; x!= 1.0; x+= 0.1)
            cout << x << endl;

        return 0;
}

I believe the problem lies in the fact that 0.1 does not have an exact binary floating point equivalent. There are only approximations. This creates a situation where x will never equal exactly 1.0; therefore, the loop never satisfies the condition and continues until terminated manually.

If I understand the concept properly, the only floating point values which will result in a loop which terminates as desired must result in a value which is an exact power of two. Increments of 0.5, 0.25, 0.125, ... would terminate in the above code.

I am currently working my way through Chapter 3, but had not reached the Floating-Point Loop Counters topic prior to now. My attempt to resolve the loop termination problem prompted me to read ahead of my current progress to find an answer.

Did I come up with the correct answer to your question?
Quote:

Next code never stopped, and you known why?) I know)
regards,
drpepper


All times are GMT -4. The time now is 04:29 AM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.