- Jul 10, 2007
- 9,234
- 142
- 106
Interesting to know. Mathematically 1.1 does equal 1.100000. Never would have thought a computer wouldn't be able to figure that out.
Well it was to 6 decimal places, none of which were 0
Interesting to know. Mathematically 1.1 does equal 1.100000. Never would have thought a computer wouldn't be able to figure that out.
Well it was to 6 decimal places, none of which were 0
float 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.
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}
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
float 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.
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}
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
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
float 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.
while (x != 1.1) {
printf("x = %f\n", x);
x = x + 0.1;
}
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
