Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Main page
Recent changes
Random page
Help about MediaWiki
Special pages
Niidae Wiki
Search
Search
Appearance
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
Infinite loop
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Page information
Appearance
move to sidebar
hide
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.
Summary:
Please note that all contributions to Niidae Wiki may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see
Encyclopedia:Copyrights
for details).
Do not submit copyrighted work without permission!
Cancel
Editing help
(opens in new window)
Search
Search
Editing
Infinite loop
(section)
Add topic