> Date: Fri, 05 Dec 1997 09:35:28 -0600
> From: Dick Hendrickson <[log in to unmask]>
> At 07:59 AM 12/5/97 +0000, [log in to unmask] wrote:
> >
> >May I take the opportunity to remind all those who teach algorithms
> >to promote the very elegant (IMHO) solution to 90% of short-circuit
> >.and. cases:
> >Replace
> > IF (K .GT. 0 .SCAND. X(K) .GT. 0.) THEN
> >with
> >REAL, DIMENSION (0:NX) :: X
> >
> >X(0) = 0.0
> >
> > IF (X(K) .GT. 0.) THEN
> This fails with K=-1. It really depends on knowing where K came from, and
> more importantly, knowing how someone is going to modify the source of
> K next year.
I partly agree with Dick that expanding the index space of X won't solve
all problems. But there are certainly cases where that is the right
thing to do.
> I don't think there's a good substitue for something like
> IF ( K > 0 .AND X(max(1,K)) > 0) THEN
> unless we come up with .andthen., .orelse., At least it's obvious
> (to me anyhow :-) ) that you're protecting the subscript from something.
> Dick Hendrickson
It may be obvious what it's doing, but it isn't obvious why X(1) should
be the default value for the expression for all out-of-range values of K.
And what about the case of ( K .GT. ubound( X,1 ) ) ?
There's just no substitute for a comment to _explain_ the program's
_intentions_. Code can be self-documenting, but only in the sense it
documents what the computer is going to do. What the programmer _intended_
is a separate matter.
To me, this is a perfect example of why exception handling is needed.
If the code in the IF is meant to be executed for the K'th element
of the X array, then K being outside the index range of X is just
plain wrong and the code should explicitly handle that case. I
think it would be very nice to be able to write:
raise <exception_name>
or something similar. It would be even better if there was a more compact
way to express this particular condition, since it will come up lots.
I assume an interval arithmetic package, if it ever gets standardized, would
contain a data-type and operator that would allow soemthing like the following:
raise <exception_name> ( .NOT. ( K .IN. bounds( X ,dim=1 ) ) )
Of course, you can get the same functionality today with:
if( K .LT. lbound( X,dim=1 ) .OR. K .GT. ubound( X,dim=1 ) ) goto exception_num
(Go ahead, flame me. I'm a heretic and deserve to burn. :-)
BTW, why didn't F90 allow the general use of labels of the form "<name>:"?
I can't see any reason for sticking with numeric labels only.
-David
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|