Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > BOOK: Ivor Horton's Beginning Visual C++ 2010
|
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 January 24th, 2012, 08:04 AM
Registered User
 
Join Date: Jan 2012
Posts: 3
Thanks: 2
Thanked 0 Times in 0 Posts
Post For loop with Floating Point Loop Counter - pp. 150

On pp. 150 in chapter 3 - the book states (In the section - "Floating Point Loop Counters", in the middle of the second paragraph where the author tries to provide an example of a for loop which would never terminate):

"This means that you should not code a for loop such that ending the loop depends on a floating-point loop counter reaching a precise value. For example, the following pporly-designed loop never ends.

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

I tried it but the loop actually ends.
I got the following output: 00.20.40.60.8.

Then I added an endl to get slightly better output and I got:
0
0.2
0.4
0.6
0.8
--
Here is my code:

#include <iostream>

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

int main()
{
for (double x = 0.0; x != 1.0; x += 0.2)
cout << x << endl;
return 0;
}
----

I have submitted this to the book team as well. Any comments on this? Has anyone tried it out? I am essentially not able to substatiate the authors claim that the loop would continue forever. Thanks much.
 
Old March 28th, 2012, 09:31 AM
Registered User
 
Join Date: Mar 2012
Posts: 12
Thanks: 0
Thanked 3 Times in 3 Posts
Default

I can also confirm that the problem the original poster describes is the same for me. It is difficult to understand what the authors intention was with this example.

Of course, if I increment by 0.3 instead of 0.2 then the loop never ends, but I would be surprised if this was the authors intention. Maybe there is an errata for this.
The Following User Says Thank You to gsallis For This Useful Post:
shivesh (March 28th, 2012)





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL Loop with counter and concat chilly XSLT 19 July 28th, 2010 11:34 AM
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
Help Floating Point Accuracy righty Java Basics 0 January 28th, 2007 12:35 AM
Formating Floating point atoyot Visual Basic 2005 Basics 2 April 26th, 2006 08:36 PM
A weird problem with floating point ? tzecheung Javascript 1 June 25th, 2003 06:32 AM





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