fortran-lang / minpack

Modernized Minpack: for solving nonlinear equations and nonlinear least squares problems
https://fortran-lang.github.io/minpack/
Other
90 stars 18 forks source link

Superfluous if blocks due to FORTRAN 66 do statement semantics #37

Open ivan-pi opened 2 years ago

ivan-pi commented 2 years ago

Here's a section of code from rwupdt:

        do j = 1, n
            rowj = w(j)
            jm1 = j - 1

            ! apply the previous transformations to
            ! r(i,j), i=1,2,...,j-1, and to w(j).

            if (jm1 >= 1) then
                do i = 1, jm1
                    temp = Cos(i)*r(i, j) + Sin(i)*rowj
                    rowj = -Sin(i)*r(i, j) + Cos(i)*rowj
                    r(i, j) = temp
                end do
            end if

As you may notice for j = 1, jm1 = 0, meaning the if loop surrounding the inner do loop is totally superfluous.

As @arjenmarkus kindly explained in his reply at Discourse, this is a "legacy" issue related to Fortran 66 DO semantics. Edit: See Section 7.1.2.8 in the ANSI X 3.9 Fortran 66 standard. The requirement to surround the DO loop with an IF block came from the line:

At time of execution of the DO statement, m1, m2, and m3 must be greater than zero.

The MINPACK source code in the present state remains full of this pattern. Maybe CamFort, plusFORT, or fpt have a tool which could fix this?

It would be a good idea to start a document or wiki- page collecting such legacy "gotchas" if we plan to do more "modernization".