Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2010
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2010 by Ivor Horton; ISBN: 9780470500880
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2010 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2011, 08:53 AM
Registered User
 
Join Date: Jun 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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)
 
Old March 28th, 2012, 11:00 AM
Registered User
 
Join Date: Mar 2012
Posts: 12
Thanks: 0
Thanked 3 Times in 3 Posts
Default

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.
 
Old June 11th, 2012, 02:21 PM
Authorized User
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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
 
Old June 18th, 2012, 11:37 AM
Registered User
 
Join Date: Jun 2011
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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;
 
Old June 18th, 2012, 02:43 PM
Authorized User
 
Join Date: May 2012
Posts: 66
Thanks: 0
Thanked 4 Times in 4 Posts
Default

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

Last edited by drpepper; June 18th, 2012 at 02:45 PM.. Reason: typo





Similar Threads
Thread Thread Starter Forum Replies Last Post
Chap 2 - Avatars and uploading files, (would apply to Chap 6 too) kenj BOOK: PHP and MySQL: Create-Modify-Reuse ISBN: 978-0-470-19242-9 0 October 18th, 2009 03:14 PM
Compiler Romaunt V. Intro Programming 2 October 18th, 2008 06:27 AM
"Compiler Options" Question (Newbie-Type) DaveLeeNC Access VBA 2 February 20th, 2006 08:09 AM
For all who need a C/C++ compiler... Ammiel C++ Programming 1 July 17th, 2003 01:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.