In article <jicpe5$65s$
2...@speranza.aioe.org>,
glen herrmannsfeldt <
g...@ugcs.caltech.edu> wrote:
> > If the compiler computes an integer trip count before loop
> > execution, then the behavior will be a little different. There are
> > still several ways to compute the values "x" in this case, so there
> > are still some uncertainties in what are the results.
>
> If the end loop value is approximately half way between two loop
> values, then it should reliably stop as expected.
Consider this situation. An integer trip count is computed before
the loop, the initial value of "x" is set before the loop, and its
value is incremented each pass through the loop. The floating point
hardware is set to round down on the increment, and the floating
point increment happens to be smaller than the exact increment. In
this case, the value of "x" will always be smaller than the expected
value each pass through the loop. In such a case, it might appear
that there should have been one more pass through the loop, or that
the final pass through the loop did not succeed in reaching the
desired boundary value by one or more steps -- there are several
potential problems depending on the application.
However, if the floating point increments are set to round up in
this situation, then the final value of "x" will always be larger
than the mathematically correct value. With enough loop iterations,
it could even be large enough so that the last one or more values
that "x" has will be larger than the loop upper bound. In this
case, it might appear that there were "extra" loop iterations, or
that the final iterations overstepped the desired boundary value.
Again, there are several potential problems depending on the
application.
This is related to the way the trip count is computed for fortran do
loops. The trip count expression is defined in the fortran
standard, but it is not required by the standard that this is the
way the do loop must be implemented. In C, this does not happen
because the loop structure itself defines how the value is
incremented and what test is done to terminate the loop. A C loop is
more like a DO WHILE loop in fortran, where the termination is
determined during the loop executions rather than at the beginning.
The only way for the trip count to be computed correctly, so that it
is well defined and agrees with the expected number of loops in all
cases in fortran, is for integer do loop specifications.
$.02 -Ron Shepard