Print

Print


Aleksandar Donev <[log in to unmask]> wrote:
...
>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. This clouds readability to some extent.
>What do you recommend?

Both the latter cloud legibility to some extent. If the language
had rational arithmetic, the first form would be the clearest
(since it probably corresponds directly to the documentation
or the mathematical formula).  The second form directly expresses
the desired operation, but is rather verbose.  The third form
is less cluttered, but doesn't explicitly  specify the internal
machinations of the conversions.  I would suggest another
form as a possibility (not strongly to be favored, but merely
another alternative):

   y = (k-1) * x / (k+1)

I would recommend whichever the maintainer of the code
finds most legible.  (I prefer my last suggestion, but that's 
because it's what I use most often.  I have no evidence that
any are inherently superior.  Though I suspect the verbose
form might be inferior.)

--
J. Giles



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