Chap3. Cycles. compiler options?
Hello! In Chapter 3 cycles with Floating Point written code:
Code:
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) 
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.

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) Code:
for (double x = 0.0; x!= 1.0; x+= 2.0) so... Let's do an abbreviated form of play computer Code:
for (double x = 2.0; x!= 1.0; x+= 0.2) 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) 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) 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 
Thanks, but you don't undestand situation.
Your code is right and I agree with it. I'm saying about ambiguity in presentation floatpoint 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) 
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 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 FloatingPoint 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:
drpepper 
All times are GMT 4. The time now is 07:32 PM. 
Powered by vBulletin®
Copyright ©2000  2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.