Slashdot Mirror


How Not to Write FORTRAN in Any Language

gManZboy writes "In an article that's sure to p/o Fortran programmers, Donn Seeley has assembled a rant that posits there are characteristics of good coding that transcend all programming languages, except Fortran. Seriously though, his point is that early FORTRAN made coding ugly. Thus the joke 'Don't write FORTRAN' was applied to anyone with ugly code. Though Fortran has in recent years overcome its early challenges, the point -- 'Don't write FORTRAN' (i.e. ugly stuff) -- still applies."

29 of 502 comments (clear)

  1. Re:Learning It? by What'sInAName · · Score: 2, Informative


    Well, I'm by no means an uber-coding guru, but I used to prgram in C, and I just finised a three-year job writing many little programs in Fortran 90. I liked it, for what it is. The modern variants of Fortran are, in general, much nicer to look at, and if you know C and/or other languages, you could probably pick it up pretty easily.

    I think I found basic i/o to be much easier in Fortran than C, but then that might be because my C is so rusty I have to keep re-looking everything up. Oh well.

    For a (mostly Fortran-like) language that has a free compiler, you might also take a look at F as well. (http://www.fortran.com/F/).

  2. Re:FORTAN programs are small right? by YankeeInExile · · Score: 2, Informative

    I am sure most of these programs are small and maintained by a few people.
    Actually, most production FORTRAN systems are beheamoth systems for modeling very complex behaviors. Although the community that keeps these systems up-to-date is probably relatively small.

    But not always: My friend Carl was writing CGI in FORTRAN to run on the XKL TD-1

    --
    How does the Slashdot Effect happen given that no slashdotters ever RTFA?
  3. Re:Learning It? by Anonymous Coward · · Score: 3, Informative
    Are there things that it does a lot better than other languages?

    You're kidding, right? FORmula TRANslation was meant for solving problems in numerical methods. It is still one of the best languages in the world for solving those kinds of problems. No, it was never meant to write operating systems (but apparently is was used for at least one OS). It is also a highly portable language; iff you stay away from extensions. If you are looking for good numerical code, you probably end up at Netlib. And most of that is probably still FORTRAN. Some people have even written C/C++ interfaces to FORTRAN numerical libraries, because the FORTRAN code is still the best implementation for solving the problem.

    Yes, please do write good code. Poorly written code is a pain in any language.

  4. Re:Learning It? by avandesande · · Score: 2, Informative

    The special thing about Fortran is that you can process a whole vector instead of using a for() loop. This makes it easy for compilers to break up tasks among multiple processors.

    --
    love is just extroverted narcissism
  5. Re:Article format by gManZboy · · Score: 3, Informative

    We are actually in the process of changing the printer-friendly version to display the article all on one page. Sorry it's not ready today, hopefully by next week.

    --
    Ed Grossman, InformationWeek
  6. Re:Learning It? by phritz · · Score: 5, Informative
    Is Fortran worth learning? And are there any things that it does a lot better than other languages?

    Maybe, and absolutely. Note that the article ignorantly used FORTRAN to mean FORTRAN 66. In fact, Fortran 90 has all the features of a modern programming language. Add to that the fact that fixed-format produces far prettier looking code than C's mess of curly braces and parentheses, although you can also use free-format if you choose. Really, the article makes no sense if you look at what FORTRAN is today.

    Answering your questions directly:
    Fortran 90 does array, vector, and matrix processing better than any other language, and can do some parallelization of vector processing with a compiler flag. If you're doing scientific programming, Fortran 90 is really the language you should be using. On the other hand, for programs that are actually supposed used by laypeople, there's not much support for doing that with FORTRAN.

  7. Re:FORTRAN - The ugly but lovable little SOB by fm6 · · Score: 4, Informative
    The problem with FORTRAN is that it was the very first high-level programming language. ("High-level" meaning a language that allows you to think in formulas and variables, not operation codes and registers.) So they had to make all the mistakes that taught computer scientists how not to write a grammar. Which mistakes produced a language that is both difficult to code and difficult to parse.

    Unfortunately, FORTRAN has achieved a role in scientific computing it will probably never lose. One co-worker of mine was a recent physics PhD who spent his entire academic career trying to persuade his fellow scientists that they'd save themselves a lot of grief by switching to C -- with no success.

  8. Fortran >=90 is nice and easy to learn by poszi · · Score: 2, Informative

    While Fortran 77 is ugly and I saw terrible programs written in Fortran 77, Fortran 90 or 95 is a pretty modern and nice language and if you know C, it is easy to learn Fortran 90. It is not that much different from anything else. It is great in numerical programs (especially linear algebra) as the matrix handling is nice and fast. The only problem is that only recently free (as in speech) Fortran 90 compilers has become available and they are still in beta stage. However, for Linux, Intel Fortan Compiler is free (as in beer) and works fine. It includes also documentation of the language so you may try it.

    --

    Save the bandwidth. Don't use sigs!

  9. Re:Learning It? by Peter+La+Casse · · Score: 2, Informative
    doesn't gcc include a fortran compiler? (g77?)

    It does, but it's starting to show its age. There's a new GCC Fortran 95 compiler under development ("gfortran") that will be officially released with GCC 4.

  10. Re:Why use FORTRAN these days? by rebelcool · · Score: 2, Informative

    Alot of it has to do with legacy scientific and engineering libraries. There are millions of lines of proven libraries that were originally written in the 60s and 70s for various scientific and engineering analysis disciplines.

    When I was at the university of texas, there was a parallel programming course that was conducted in fortran and c++. The professor of it quoted how much it would cost in labor and manhours to transfer all those fortran libraries to a more "modern" language - and it was many many millions of dollars and years of work. So, the course taught parallel programming by having students doing this work! It was fairly successful, and is probably still taught. The converted libraries generated by students are in use in several fields. It was alot of parallel linear algebra libraries, IIRC.

    --

    -

  11. Re:Learning It? by mz001b · · Score: 2, Informative
    There's a new GCC Fortran 95 compiler under development ("gfortran") that will be officially released with GCC 4

    and an even better, almost working one, g95 -- this is the code base that the gfortran people forked from. gfortran does not work, and is much further behind than g95.

  12. Why FORTRAN makes people think FORTRAN-66 (or 77) by Glomek · · Score: 4, Informative

    The p/o'd response basically sounds like "He's equating Fortran with FORTRAN-66 (or 77)".

    I know that I do this too. When someone says "It's written in FORTRAN" I don't think Fortran-95, I think FORTRAN-77... and I'm usually right.

    I suspect that there are two reasons for this:

    1. FORTRAN-77 was the big thing during FORTRAN's heyday, so most of the legacy FORTRAN code out there is FORTRAN-77.

    2. For a long time, the best Free Software FORTRAN compilers out there (g77, f2c) have been FORTRAN-77 compilers. g95 is still fairly young.
  13. Re:FORTRAN considered useful...like SQL by Tablizer · · Score: 2, Informative

    It reminds me of SQL in that respect. I have worked with managers who knew less about computers than their secretaries, but they were able to use SQL to write queries to get information that they wanted. SQL was written for that purpose. It ain't pretty, but it serves its target market.

    Target market? Do you mean when IBM first designed it, or how it is actually used now? Most SQL "users" are developers. However, SQL is poorly suited for developers. Beyond a single SELECT statement, SQL's flaws start to pile up pretty quick. I think it is time to replace SQL with a better relational language. A RDBMS can perhaps use both SQL and the replacement so that non-techies can use the more English-like SQL if desired.

    Alternatives include Chris Date's "Tutorial D". I am also working on a replacement, tentatively called "Tops Query Language". However, I don't think they will catch on because SQL can fixed to be "good enough" if the industry is ever pushed to improve the situation.

  14. Re:Learning It? by DoctorRad · · Score: 2, Informative
    For a (mostly Fortran-like) language that has a free compiler, you might also take a look at F as well. (http://www.fortran.com/F/).

    Seconded. F is a subset of Fortran95, with all the deprecated syntax options taken out. This promotes good coding style. Any F program will compile with a f95 compiler, so you can get the same code running on much bigger machines very easily.

    Matt...

  15. Re:A great programming language by seminumerical · · Score: 2, Informative
    I loved the fact that everything was pass by reference

    Actually, FORTRAN IV, on some computers of the day (I think IBM 360/370 using FORTRAN's G or H), used pass by "value and result" rather than pass by reference. There were so many differences amongst the dozens of dialects of FORTRAN out there in my day (late 70's) that portability was nonexistent and I changed languages.

    --
    In wartime... truth is so precious that she should always be attended by a bodyguard of lies. (Churchill)
  16. FORTRAN gets its bad reputation... by alispguru · · Score: 3, Informative
    ... from scientist and engineer coders. For these guys, FORTRAN is the only reasonable way for them to turn their domain knowledge into production code, for three main reasons:

    Libraries - the most bullet-proof, battle-tested numerical code is pretty much all in FORTRAN

    Optimizers - if you must wring the last factor of two of performance out of big vectorizing iron, and you're not a CS guru, the FORTRAN compiler is still your best bet

    Semantics - FORTRAN the language enforces some constraints that make vectorizing optimization work better than less constrained stuff like C

    The problem is, for these guys FORTRAN is a means to an end - most of them have had very little formal training in good coding practice, and worse, most of the code they read was written by people with similar experience.

    Maybe what we should do is require scientists and engineers to pair-program with recent CS graduates. Both sides would learn a lot from that.

    --

    To a Lisp hacker, XML is S-expressions in drag.
  17. Re:Learning It? by Anonymous Coward · · Score: 1, Informative

    What about APL? If you're in for cryptic but powerful languages, of course.

  18. Re:Speed of Fortran by twiddlingbits · · Score: 3, Informative

    " it's a lot quicker to write/maintain makes most of that speed differential meaningless." No it doesn't. Some of the FORTRAN code is used in algorithms that do things like Finite Element Analysis, High Energy Physics and Weather Prediction where it is not uncommon for something to run for HOURS or run many times with different variable settings (i.e. Monte Carlo). These systems invariably use highly optimized FORTRAN code. CPU time is NOT free and often an Engineer is charged a "tax" by IT for his CPU useage over his allocation. You only save the programmer time once, you save execution time EVERY run. I've worked in the Aerospace biz many years and have never seen big number crunching programs done in C/C++ or Java. I have seen some Assembler but not much. I've also written C code for embedded systems and I do know it can be written very efficently, but it's not at all optimized for Math thus FORTRAN beats it.

  19. Re:FORTRAN - The ugly but lovable little SOB by Tony-A · · Score: 2, Informative

    Tended to be FORTRAN for scientific computing and COBOL for business computing, with the two groups never communicating with each other.
    Then there was PL/I which allows writing FORTRAN and/or COBOL within an ALGOL structure.

    In addition to the punch-cards, remember that second generation mainframes were the equivalent of 64k bytes. Cramped, very cramped. The 7070 was 10,000 10-digit words (decimal) with three signs, which comes out in the same neighborhood.

  20. Re:hardly unfortunate by Fruit · · Score: 2, Informative
    Actually C has those too:
    int main(void) {
    char x[7][13];
    printf("%d\n", sizeof x);
    return 0;
    }

    % ./a.out
    91
  21. GPL by Bananenrepublik · · Score: 3, Informative

    Unfortunately, g95'S developer chose to rather violate the GPL than to either work together with the gcc people or at least let them use his code. Look here for more information on the split.

  22. Re:hardly unfortunate by YU+Nicks+NE+Way · · Score: 1, Informative
    You didn't read what I wrote. Yes, C has [][] allocators, but it does not have multidimensional dereference. If you want to iterate through the elements in the array that you allocate using
    char x[5][10]
    you have to do it by hand
    for (i = 0; i < 4; i++)
    for (j = 0; j < 10; j++)
    {
    *((char *) (&x[0][0])+ 10 * i + j) = '\\n';
    }
    That's why people talk about writing custom libraries to support scientific caculations in C.
  23. Re:hardly unfortunate by Dachannien · · Score: 2, Informative

    Either that's a bad example, or compiler optimization has come a long way, baby. Or maybe I just don't get what you're getting at. But a quick test using gcc reveals this:

    http://sokar.cwru.edu/multidim.html

    Note that the code that calculates the offset into the x array (italicized for convenience) is one instruction shorter for x[i][j] rather than the code you listed.

  24. Re:hardly unfortunate by pavon · · Score: 4, Informative
    You are misinformed. char x[5][10] does allocate a contiguous block of memory, and x[i][j] does index that memory the exactly as if you were to write *(&x + 10*i + j) !

    I know this for a fact as I have written graphics software that treated multidimensional arrays as a contiguous blocks of memory accessing it using x[i][j] notation and iterated pointer notation depending on the circumstance and both worked the same. I have also read the assembler output from a C compiler and it indeed compiled x[i][j] the same as *(&x + 10*i + j).

    To further back up my statement here is a direct quote from Kernighan and Ritchie's "The C Programming Language" Chapter 5:
    5.9 Pointers vs. Multi-dimensional Arrays
    Newcomers to C are sometimes confused about the difference between a two-dimensional array and an array of pointer such as name in the example above. Given the definitions.
    int a[10][20];
    int *b[10];
    then a[3][4] and b[3][4] are both syntactically legal references to a single int but a is a true two-dimensional array: 200 int-sized locations have been set aside, and the conventional rectangular subscript calculation 20×row+col is used to find the element a[row][col]. For b, however, the definition only allocates 10 pointers and does not initialize them; initialization must be done explicitly, either statically or with code. Assuming that each element of b does point to a twenty-element array, then there will be 200 ints set aside, plus ten cells for the pointers. The important advantage of the pointer array is that they rows of the array may be of different lengths. That is, each element of b need not point to a twenty-element vector; some may point to two elements, some to fifty, and some to none at all.

    So the notation x[i][j] will compile down to either *(&x + 10i + j) or *(*(x+i) + j) depending on whether x was declared a true multidimensional array or an array of pointers.

    One caveat (and possible source for your confusion) is that when declaring a true multidimensional array you must know the dimensions at compile time. You cannot for example declare x[width][height], and if you allocate a chunk of memory with malloc, you can access it as a single-dimensional array using the x[i] notation, but there is no notation for treating it as a multidimensional array. I have had to manually index dynamically allocated chunks of data using *(&x + 10i + j) before, and I agree it is a pain. Fortunately C++ fixed that and you can allocate multidimensional arrays at runtime and use the x[i][j] notation.

    To be fair however, I'm pretty sure that FORTRAN 77 did not have automatic or allocatable arrays either - there were some implementation-specific ways of doing it, but it was not officially added until FORTRAN 90. But I am young enough that FORTRAN 90 was my first FORTRAN, and what I know about 77 is only from historic reading, not experience, so take that with a grain of salt.
  25. Re:Nothing for you to see here, please move along. by locofungus · · Score: 2, Informative

    For the second case:

    #include <iostream>

    class A { public: virtual void do_something() { std::cout << "A" << std::endl; } };
    class B:public A { public: virtual void do_something() { std::cout << "B" << std::endl; } };

    void myfunc(A& a) {
    a.do_something();
    }



    Does this code print A or B when myfunc is called? Which do_something() is executed? Unless you are using function pointers, this ambiguity does not exist in C (or FORTRAN)

    For the first case I wrote:
    There is no way of telling just by looking at this code.

    in C if I have int i; do_something(i); I know that i doesn't change. I don't need to look at the prototype (ok do_something could be a macro)

    What I would have liked is for the caller to a non-const reference to still have to write that '&'. The callee gets the benefit of the reference and anyone looking at the caller code immediately knows that there might be something happening that isn't explicit in the code they are looking at.

    In theory a smart syntax highlighter could do this but it would probably have to almost be a compiler to handle this, especially if you want it to be correct, even in the face of macros. And I don't want to have to wait for a compilation every time I load a file up into my editor.

    An editor could not handle something like:

    #ifdef REALLY_SCREW_THINGS_UP
    void shit(int& i) { i=i+1; }
    #else
    void shit(int i) { }
    #endif

    int main() {
    int i=0;
    shit(i);
    }


    But a compiler could have refused to compile unless that call in main was changed to shit(&i); if REALLY_SCREW_THINGS_UP was defined.

    --
    God said, "div D = rho, div B = 0, curl E = -@B/@t, curl H = J + @D/@t," and there was light.
  26. Two conflated events by Anonymous Coward · · Score: 1, Informative

    http://rchrd.com/Misc-Texts/Famous_Fortran_Errors

    >From the alt.folklore.computers FAQ:

    III.1 - I heard that one of the NASA space probes went off course and
    had to be destroyed because of a typo in a FORTRAN DO loop.
    Is there any truth to this rumor?

    As revealed by past discussion in comp.risks (Risks Digest) as well as
    alt.folklore.computers and occasionally other newsgroups, this turns
    out to be a confusion of two separate events.

    The space probe that the DO-loop story has been wrongly attached to is
    Mariner I (or 1), which was intended for Venus (not Mars). Several
    incorrect or partially correct versions of what really happened were
    posted in comp.risks; the best of these cited a NASA publication called
    "Far Travelers" by Oran W. Nicks, but still did not have the whole story.

    Then in issue 8.75 we found out what really happened...

    | Date: Sat, 27 May 1989 15:34:33 PDT
    | From: Peter Neumann
    | Subject: Mariner I -- no holds BARred
    |
    | Paul Ceruzzi has written a truly outstanding book for the new show
    | that opened two weeks ago at the Smithsonian National Air and Space
    | Museum. The exhibit and the book are both entitled "Beyond the Limits
    | -- Flight Enters the Computer Age". Both are superb. Go for it (them).
    |
    | Paul has dug into several cases treated previously in RISKS and in
    | issues of the ACM Software Engineering Notes, and has been able to
    | resolve several mysteries. In particular he considers the case of
    | Mariner I, about which various inaccurate stories have been told.
    | Intended to be the first US spacecraft to visit another planet, it was
    | destroyed by a range officer on 22 July 1962 when it behaved
    | erratically four minutes after launch. The alleged missing `hyphen'
    | was really a missing `bar'. I quote from Paul's book, pp. 202-203:
    |
    | # During the launch the Atlas booster rocket was guided with the help
    | # of two radar systems. One, the Rate System, measured the velocity of
    | # the rocket as it ascended through the atmosphere. The other, the
    | # Track System, measured its distance and angle from a tracking
    | # antenna near the launch site. At the Cape a guidance computer
    | # processed these signals and sent control signals back to the
    | # tracking system, which in turn sent signals to the rocket. Its
    | # primary function was to ensure a proper separation from the Atlas
    | # booster and ignition of the Agena upper stage, which was to carry
    | # the Mariner Spacecraft to Venus.
    | #
    | # Timing for the two radar systems was separated by a difference of
    | # forty-three milliseconds. To compensate, the computer was instructed
    | # to add forty-three milliseconds to the data from the Rate System
    | # during the launch. This action, which set both systems to the same
    | # sampling time base, required smoothed, or averaged, track data,
    | # obtained by an earlier computation, not the raw velocity data
    | # relayed directly from the track radar. The symbol for this smoothed
    | # data was ... `R dot bar n' [R overstruck `.' and `_' and subscript n],
    | # where R stands for the radius, the dot for the first derivative
    | # (i.e., the velocity), the bar for smoothed data, and n for the
    | # increment.
    | #
    | # The bar was left out of the hand-written guidance equations. [A
    | # footnote cites interviews with John Norton and General Jack Albert.]
    | # Then during launch the on-board Rate System hardware failed. That in
    | # itself should not have jeopardized the mission, as the Track System
    | # radar was working and could have handled the ascent. But because of
    | # the missing bar in the guidance equations, the computer was
    | # processing the track data incorrectly. [Paul's EndNote amplifies:
    | # The Mariner I failure was thus a {\it combination} of

  27. Re:hardly unfortunate by Anonymous Coward · · Score: 1, Informative
    > Why do these use less code

    An example:
    real a(-1:M+2,-1:N+2,L1), b(M,L2,N), c(M,N,L,L3)
    a(1:M,1:N,2) = sin(b(:,1,:)) + cos(c(:,:,2,3)) !! all are M x N arrays.
    In Fortran, you can take "sections" of arrays. Also, although indices start from 1 by default, you can specify the lower limits if you like, as in the first array "a" above, which has -1 as the lower limits of the first two dimensions. Intrinsic functions can be applied to arrays and sections of arrays. The language has the syntax for these features. In C++ the above code would be
    float a[M+4][N+4][L1], b[M][L2][N], c[M][N][L][L3];
    for (int i=0; i<M; ++i)
    for (int j=0; j<N; ++j)
    a[i+2][j+2][1] = sin(b[i][0][j]) + cos(c[i][j][1][2]);
    But then, you have to remember i+2 for "a" corresponds to i for "b" and "c" because the lower bound of array indices are always 0 in C++.

    Array sections can be used as arguments to subroutines, as in
    call sub(a(3,:)) !! sub() expects 1-D array
    call sub(a(:,2))
    In C++, you can do this by defining a class representing multidimensional arrays. You would define arithmetic operations (+, -, *, /), and functions like sin, cos, log, exp, etc. for the class. You can even define array sections by defining a set of classes representing the notion of sections. Even then, your array classes would be far less easy to use than Fortran's native arrays.

    And even then, a C++ compiler can't apply as much optimization to your code as a Fortran compiler can. For example, a Fortran compiler can easily transform
    real a(M,N), b(M,N), c(M,N), d(M,N) ! 2D arrays
    a = sin(b) + cos(c) + tan(d)
    into the one-dimensionalized
    for (k = 1; k<= M*N; ++k)
    a[k] = sin(b[k]) + cos(c[k]) + tan(d[k]);
    The compiler can do this because arrays are native types and because the compiler knows the memory for each array is contiguous. Can a C++ compiler transform
    My2DArray<double> a(M,N),b(M,N),c(M,N),d(M,N);
    a = sin(b) + cos(c) + tan(d);
    likewise? I doubt it. I think a C++ compiler has to generate a temporary for the result of sin(b), cos(c), and tan(d), and add the first two temporaries, and then the third to get the final result.

    If there's a sizable demand for numerical computations in C++, then vendors would create a C++ compiler which has a built-in capability of efficient and convenient array operations and other stuff (like native complex numbers). But, that won't happen because people doing numerical computations are largely happy with Fortran 95 and those aren't happy with the language interested in numerical computations. Only minority of people (like me) aren't quite happy with Fortran 95 *and* are doing numerical computations.
  28. MOD DOWN by Anonymous Coward · · Score: 1, Informative

    In C, multidimensional arrays are a fiction, because a[i][j] is given exactly the semantics of *(*(a + i) + j), instead of *(a + i * second_dim + j).

    Poster has absolutely no clue what s/he is talking about.

  29. Re:hardly unfortunate by Mr2001 · · Score: 2, Informative

    One caveat (and possible source for your confusion) is that when declaring a true multidimensional array you must know the dimensions at compile time. You cannot for example declare x[width][height]

    I'm pretty sure GCC lets you do that, doesn't it?

    --
    Visual IRC: Fast. Powerful. Free.