What am I missing?

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.

disappoint

Lifer
Dec 7, 2009
10,132
382
126
float x = 0.1;
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}

On some systems, this loop will execute ten times as expected, but on other systems it will never terminate. The problem is that the loop terminating condition (x != 1.1) tests for exact equality of two floating point values, and the way floating point values are represented in many computers will make this test fail, because they cannot represent the value 1.1 exactly.
Because of the likelihood of tests for equality or not-equality failing unexpectedly, it is safer to use greater-than or less-than tests when dealing with floating-point values. For example, instead of testing whether x equals 1.1, one might test whether (x <= 1.0), or (x < 1.1), either of which would be certain to exit after a finite number of iterations. Another way to fix this particular example would be to use an integer as a loop index, counting the number of iterations that have been performed.


from: http://en.wikipedia.org/wiki/Infinite_loop
 

Tweak155

Lifer
Sep 23, 2003
11,449
264
126
float x = 0.1;
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}

On some systems, this loop will execute ten times as expected, but on other systems it will never terminate. The problem is that the loop terminating condition (x != 1.1) tests for exact equality of two floating point values, and the way floating point values are represented in many computers will make this test fail, because they cannot represent the value 1.1 exactly.
Because of the likelihood of tests for equality or not-equality failing unexpectedly, it is safer to use greater-than or less-than tests when dealing with floating-point values. For example, instead of testing whether x equals 1.1, one might test whether (x <= 1.0), or (x < 1.1), either of which would be certain to exit after a finite number of iterations. Another way to fix this particular example would be to use an integer as a loop index, counting the number of iterations that have been performed.


from: http://en.wikipedia.org/wiki/Infinite_loop

This seems like a good answer, but even when I type-cast onto the 1.1 it doesn't recognize it. I suppose I can't type cast it either. It is very odd.
 

coldmeat

Diamond Member
Jul 10, 2007
9,234
142
106
float x = 0.1;
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}

On some systems, this loop will execute ten times as expected, but on other systems it will never terminate. The problem is that the loop terminating condition (x != 1.1) tests for exact equality of two floating point values, and the way floating point values are represented in many computers will make this test fail, because they cannot represent the value 1.1 exactly.
Because of the likelihood of tests for equality or not-equality failing unexpectedly, it is safer to use greater-than or less-than tests when dealing with floating-point values. For example, instead of testing whether x equals 1.1, one might test whether (x <= 1.0), or (x < 1.1), either of which would be certain to exit after a finite number of iterations. Another way to fix this particular example would be to use an integer as a loop index, counting the number of iterations that have been performed.


from: http://en.wikipedia.org/wiki/Infinite_loop

Oh, makes sense I suppose. Why do I get weird numbers though after it runs for a little while? I'll start getting numbers like 258.704316
 

disappoint

Lifer
Dec 7, 2009
10,132
382
126
Oh, makes sense I suppose. Why do I get weird numbers though after it runs for a little while? I'll start getting numbers like 258.704316

My guess is if you let it run for too long, the imprecision adds up.

The computer cannot represent the number 1.1 in floating point form exactly any more than you can write down pi or 1/3 in decimal form exactly. You can only approximate it.
 

gophins72

Golden Member
Jul 22, 2005
1,541
0
76
definitely annoying, but I can see how these classes are good at teaching how to debug.

One of my previous jobs i was tasked to teach a bunch of new hires how to debug. I was completely lost at how to go about this.
 

CPA

Elite Member
Nov 19, 2001
30,322
4
0
float x = 0.1;
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}

On some systems, this loop will execute ten times as expected, but on other systems it will never terminate. The problem is that the loop terminating condition (x != 1.1) tests for exact equality of two floating point values, and the way floating point values are represented in many computers will make this test fail, because they cannot represent the value 1.1 exactly.
Because of the likelihood of tests for equality or not-equality failing unexpectedly, it is safer to use greater-than or less-than tests when dealing with floating-point values. For example, instead of testing whether x equals 1.1, one might test whether (x <= 1.0), or (x < 1.1), either of which would be certain to exit after a finite number of iterations. Another way to fix this particular example would be to use an integer as a loop index, counting the number of iterations that have been performed.


from: http://en.wikipedia.org/wiki/Infinite_loop


woohoo, I think I was right!

:p