Print

Print


> Date:          Sun, 17 Sep 2000 11:54:36 -0400
> From:          Aleksandar Donev <[log in to unmask]>

> Hello,
> 
> I have a pedagological question for the Fortran teachers out there.
> How does one write "best" an expression with mixed types, say:
> 
> complex(kind=sp) :: x,y
> integer :: k
> 
> y=(k-1)/(k+1)*x
> 
> I recommend to students to explicitly write all the type conversions:
> 
> y=cmplx(k-1,0,sp)/cmplx(k+1,0,sp)*x
> 
> though
> 
> y=x*(k-1)/(k+1)
> 
> also works.

Only by accident.  And it's not efficient, either.

One must always be careful about writing division involving integers.
(k-1)/(k+1) can easily return a zero result, especially wen it's written at the 
start of an expression (as is your first example).

Writing REAL(k+1) is sufficient in your example.
Converting to complex is wasteful, and is almost unreadable.
It's wasteful because two complex multiplications are introduced
when simple scalar multiplication by a complex value is all that's required.

> This clouds readability to some extent.
> What do you recommend?
> 
> Thanks,
> Aleksandar


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