From [log in to unmask] Thu Nov 20 05:20:33 1997
From: [log in to unmask] (Malcolm Cohen)
Subject: REAL DO loops are bad
To: [log in to unmask]
Date: Wed, 19 Nov 1997 17:59:13 +0000 (GMT)
Swietanowski Artur said:
> Van Snyder wrote:
> > (This is, by the way, related to the reason to prohibit REAL loop
> > inductors: the number of "trips" might vary from platform to platform.)
>
> Yes, quite obviously the program might execute differently. So what?
> When I write a numerical algorithm I don't care to much whether
> my program will give identical results on two different machines.
With REAL DO loops the answer is likely to be wildly different.
Wildly? Would seem to be an overstatement.
It really depends on the application, as to whether
there are any significant differences.
> Scientific programming requires numerical literacy and handling
> roundoff is just that.
>
> Same arguments for REAL iterators in DO loops.
REAL DO loops are ***NOT*** numerically literate.
Depends on one's viewpoint.
Banning REAL DOs is not the answer. If one wants to
use a REAL control variable, and a REAL DO isn't available,
one only has to use DO WHILE with a REAL control variable
and a REAL increment.
So what does banning REAL DO's achieve?
There are in fact at least two well-known problems with REAL DO loops:
(1) the iteration count is determined by INT((m2-m1+m3)/m3) instead of
NINT(ditto), so "DO X=0.1,1.0,0.1" could iterate 9, 10 or 11 times.
The answer, then, is not to use a real control variable if you
want a specific number of iterations.
So you need to add half the step size to the "final" value to be sure
of getting the iteration count desired. (Thus we have the principle
of maximal surprise - to be sure of getting to your desired "final value"
you have to specify something different!)
(2) the loop variable is incremented by the step value each time through
the loop; thus in a long loop a lot of rounding error can have
accumulated.
As the Larmouth report said, the only numerically good way of programming a
"real" DO loop is to go back to using an integer DO and a multiplier (as
the practice pre F77)
E.g. instead of
DO X=0.1,10,0.1
one should use
DO I=1,10
X = I*0.1
etc.
Very definitely NOT !
When X is 10, 10 * 0.1 yields 0.99999999 or thereabouts,
on account of the fact that 0.1 is truncated.
Best is X = I/10.
The inappropriate) example just serves to show that with any
loop, you need to take some care to ensure that it will
do what you want. And that goes for REAL and INTEGER control
variables alike.
With my numerical analyst's hat on I am fully in agreement with the Larmouth
report's conclusions on this; the REAL DO is badly designed and should never
be used.
There are certain classes of loop where a REAL DO loop is appropriate,
and there's no virtue or benefit in using some form of integer control.
Two instances spring to mind: solving a DE, and certain graphics
applications, where the start and finish are imprecise, and the limits
are expressed in REALs anyway.
Cheers,
...........................Malcolm Cohen, NAG Ltd., Oxford, U.K.
([log in to unmask])
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|