Fortran pros and cons (was Re: Coding style article with interesting section on white space)

Michael Tobis mt at 3planes.com
Sun Jan 30 23:04:26 EST 2005


beliavsky at aol.com wrote:
> Michael Tobis wrote:

> Fortran 90/95 is more expressive than Fortran 77 in many ways, as
> described in ...
> http://www.nr.com/CiP97.pdf .
>

> ... expresses more science per
> line of code and per programming workday.

The example shown on p 10 illustrates a shorter piece of code in f90
than in f77, but it is not obviously more expressive or less complex.
Arguably the f77 code is easier to write and maintain, even though it
has more linefeeds in it, so I find the example far from compelling.

In fact, I find the f90 example impenetrable. Specifically, what does
array_copy(source,dest,n,nn) do? I note that n and nn are uninitialized
at call time. Are these outputs from array_copy(), appearing, in that
inimitable Fortran way, in the call signature?

Here it is in Python with NumArray:

b = sort(compress(less(v,200.) * greater(v,100.),m ))
result = b[(len(b)+3)//4]

I certainly think this example is competitive for whatever that's
worth. It has the added benefit of handling the case of an empty list
with a nice catchable IndexError exception.

However, if this were intended to be maintainable code I would find it
most effectively expressed something like this:

... def magthresh(vel,mag,vmin=100.,vmax=200.):
...    selector = less(vel,vmax) * greater(vel,vmin)
...    sortedmags = sort(compress(selector,mag))
...    if len(sortedmags):
...       index = (len(sortedmags) + 3) // 4
...       return sortedmags[index]
...    else:
...       raise IndexError,"No velocities in range."

mt




More information about the Python-list mailing list