Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Infinite loop
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Rounding errors=== {| style="float:right; border: 1px solid grey;" |- | ''C output on an [[AMD Turion]] processor:'' |- |x = 0.10000000149011611938 |- |x = 0.20000000298023223877 |- |x = 0.30000001192092895508 |- |x = 0.40000000596046447754 |- |x = 0.50000000000000000000 |- |x = 0.60000002384185791016 |- |x = 0.70000004768371582031 |- |x = 0.80000007152557373047 |- |x = 0.90000009536743164062 |- |x = 1.00000011920928955078 |- |x = 1.10000014305114746094 |- |x = 1.20000016689300537109 |- | ... |} Unexpected behavior in evaluating the terminating condition can also cause this problem. Here is an example in [[C (programming language)|C]]: <syntaxhighlight lang="c"> float x = 0.1; while (x != 1.1) { printf("x = %22.20f\n", x); x += 0.1; } </syntaxhighlight> 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 <code>(x != 1.1)</code> 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 0.1 exactly, thus introducing rounding errors on each increment (cf. box). The same can happen in [[Python (programming language)|Python]]: <syntaxhighlight lang="python"> x = 0.1 while x != 1: print(x) x += 0.1 </syntaxhighlight> 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 <code>x</code> equals 1.1, one might test whether <code>(x <= 1.0)</code>, or <code>(x < 1.1)</code>, 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 (computer science)|integer]] as a [[control flow|loop index]], counting the number of iterations that have been performed. A similar problem occurs frequently in [[numerical analysis]]: in order to compute a certain result, an iteration is intended to be carried out until the error is smaller than a chosen tolerance. However, because of rounding errors during the iteration, the specified tolerance can never be reached, resulting in an infinite loop.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)