Talk:FOR

From C64-Wiki
Jump to navigationJump to search

The article says, "Note that the counter variable evaluates to <endvalue> + <stepsize> after the last iteration (except if the step value is 0 where the loop ends with the exact limit value <endvalue>)." In practice, this is inconsistent. If the STEP value is between 0 and 0.5, the counter variable will evaluate to <endvalue> after the last iteration. If the STEP value is between -0.5 and 0, then rounding errors are introduced and unexpected results are possible. If the STEP value is less than -0.5, the behavior mentioned in the article is once again correct.

In the first place, please sign your comment, even you are anonymous (using --~~~~).
There is no reason why the range from -0.5 to 0.5 is something special. These are also floating point values like the one outside the range. I don't know to what extend rounding is relevant here at all. The rounding after floating point operations aims to the least significant bit of the mantissa. An error might always appear depending on the actual FP representation of a "nice" looking decimal number. The real problem that arises is sometimes the step value's FP representation leads to bad addition results which misses the end value. This makes the loop variable looking bad (what is probably not expected by a programmer), but will not make the loop itself unpredictable. --JohannKlasek (talk) 11:26, 14 March 2023 (CET)