Print

Print


> Date:          Mon, 2 Oct 2000 10:30:31 -0700
> From:          Ian Chivers <[log in to unmask]>

> On Mon, 02 Oct 2000 19:55:34 +0010 [log in to unmask] 
> wrote:
> 
> > Juergen VonHagen wrote (little bits snipped for brevity)::
> > 
> > >robin wrote:
> > >> 
> > >> > From:          Aleksandar Donev <[log in to unmask]>
> > >> 
> > >> > I have a pedagological question for the Fortran teachers out there.
> > 
> > Variants of this are not only useful for teachers, but also practitioners.
> > 
> > >> > How does one write "best" an expression with mixed types, say:
> > >> >
> > >> > complex(kind=sp) :: x,y
> > >> > integer :: k
> > >> >
> > >> > y=(k-1)/(k+1)*x
> > 
> > If k is positive, the first part (integer arithmetic) is always 0, so y is 0
> > If k is zero, y = -x
> > If k is negative (except -1), y = 2.*x
> > 
> > This assumes left to right evaluation, so (k+1)/(k-1) is integer arithmetic.  I 
> > believe this is part of the standard, but would bow to RM's better knowledge.  
> > If it is not, I have screwed up big time over many years, but my vendors seem to 
> > believe what I do. 
> 
> i don't have a copy of the standard at hand so can't quote the exact 
> wording. fortran 90 allowed expressions to be replaced with
> a mathematically equivalent one. as this expression involves
> integer arithmetic i would have thought that (k-1)/(k+1) 
> has to be done first.

Doing (k-1)/(k+1) first will produce a zero result (k > 0).


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%