← Back to context

Comment by astro1234

4 days ago

Having written an open source Fortran library (for x ray scattering and absorption in a particular regime so this is fairly “niche” shall we say), genuinely curious: what if any advantages or strengths does Fortran have these days over other language choices? Are there circumstances where it’s arguably the “right” choice?

A lot of the early, absolute advantages of Fortran (numerical precision, non-aliasing) has been adopted by C.

However what still makes it a great choice is that it still a relatively simple language compared to C, and maps mathematical formulas to code very well. Inelegant code will still run reasonably fast (which is not the true with C) - so all of these points make it actually a good language when the program is being written by domain experts (i.e., scientists) rather than professional programmers or software engineers.

  • As @jleyank pointed out, the more restrictive semantics of Fortran prevent expressing things the compiler can't optimize. A Fortran compiler can make assumptions about code flow and memory accesses a C compiler can't, and, therefore, can optimize the code further than a naive C implementation could be optimized.

  • >Inelegant code will still run reasonably fast

    What do you mean by inelegant code in this context?

    Non-idiomatic? Like non-Pythonic in the case of Python?

    • Basically you don't need to think much about the computer itself, unlike C or C++. Just pass in your arrays or pointers (everything is passed by reference anyways), crunch numbers, and it'll be about as optimized as carefully __restrict__ laden C code.

      There's also fewer traps to fall in than C++ which is elephant sized in scope to Fortran's mouse.

May I ask: why did you write that library in Fortran? That is, what was your answer to your question at the time you wrote it?

I have heard that Fortran is very very good at optimizing array processing due to lack of aliases. That may be one answer today. There’s also value in continuity: in using a language for your task because it can build on what others have built. That applies to any established language, of course… but I could see it reasonably applied if someone wrote some kind of numerical processing library today. They’d write it in Fortran and call it from Python!

  • The answer was “my adviser insisted because that’s the language he used”. I had to fight to use Fortran 95 and not 77. So I think like you say value was building on an existing community’s codebase in a language they were familiar with and comfortable with.

    • Source code is meant to be read, and, occasionally, compiled.

      If nobody with domain knowledge can understand the program because it's written in a language unfamiliar to them (such as C) with tons of traps and pitfalls (such as C), the value of its contribution to the corpus of knowledge is diminished.

      1 reply →

    • Do you realize F95 is over 30 years old? The latest standard is 2023. When people talk about modern Fortran, they usually mean Fortran 2008, 2018, or 2023, or, at worst, Fortran 2003.

      1 reply →

I mean, I think it's best in class in its niche: writing numerical algorithms that compile to highly optimized machine code, while being reasonably readable and intuitive (at least from F90 on). It's arguably still the easiest way to write compute-intensive numerical extensions for python (well, if it's single-threaded, and if numpy can't do it, and if you're not using GPUs ...).