Did you know that...? Fortran gotchas thread

Another gotcha: if ch is of type character, a statement read(ch,*) x is allowed only if ch is a variable, not a constant. Why?

4 Likes

Yes. Although it is getting better the most reliable way of setting a variable to Inf or Nan was to read the word from a string. Have not tried in a while but that was the only way I could get it to work across all the compilers I was using at one time. I wrote little functions doing an internal read, initially from constant strings and was baffled when that did not work. Had to change them to saved variables or assign them on entry. It has happened a few other times that I tried to use a constant and I could not think of a good reason it was like that but did find the standard explicitly said it had to be a variable.

A lot of users with C experience are caught by surprise in the difference between array storage order and that you cannot open stdin and stdout as binary streams in any portable manner, and the differences between default argument passing being by reference or having that appearance is often mentioned as surprising. Not sure if that fits into this category as it is more a difference between languages more than an intrinsically surprising Fortran behavior but I often hear the term “surprising” when I mention to someone they are using a really bad stride regarding performance when accessing array data.

Maybe it is for “symmetry” with the write statement?

Another oddity to me was that you can only use internal I/O for UCS4 characters with UCS4 variables used as the internal file. Since reading and writing is the standard method in Fortran for converting between strings and numeric values it seemed like it would be the natural place for converting UTF-8-encoded CHARACTER strings to and from UCS4 variables but it is not. See

for the details.

You correctly raised this question in comp.lang.fortran in 2012, where Richard Maine favorited allowing it.

Yes, but we have had two revisions of the standard since 2012 and the committee is working on a third.

You can effectively read from a character constant by passing the constant to a procedure, as done below, but this should not be necessary.

module m
implicit none
contains
pure function from_char(x, n) result(y)
character (len=*), intent(in) :: x
integer, intent(in) :: n
integer :: y(n)
read (x, *) y
end function from_char
end module m

program main
use m
implicit none
print*,from_char("10 20", 2) ! gives 10 20
end program main

A quite different gotcha: real16 is in the standard but some compilers will reject real(real16) x . Fortunately we have selected_real_kind (new in F90) and real_kinds (new in F2008) that let us use various real kinds and various compilers.

and thinking about Nan and ±Inf. It makes sense once you hit it but how many tried filtering Nan numbers out by testing for equality to NaN? Assuming you have a variable NaN set to a NaN value I know the first time I tried “if (value == Nan) then …” or tried that same logical expression as a mask in some of the array intrinsics. Of course, once you hear a NaN by definition is not equal to any other value (even another NaN) it makes sense, but that did not occur to me until after I tried it.

Another old workaround was to compare the variable to itself – a regular floating point value will be equal to itself, but a NaN will not. However, all of these workarounds are now obsolete with near-universal IEEE support and the IEEE_arithmetic module, which has all kinds of ways to set, test, operate, and compare values. And something that I learned recently here, is that simply using that module might modify the way your compiler compiles your code, even if you don’t reference anything within it in your code.

Yes. I use the IEEE routines now in general but after years or working around compilers that did not support the routines I still hesitate to do so.

Fortran array syntax, support of complex intrinsics, standard-define parallelism and other advanced features make one of the biggest surprises of Fortran even more so – a lack of bsic standardized POSIX-like system interfaces. I so used to working around that I forget Fortran is the only lanuage I use that does not have extensive system interface procedures defined. That was such a big surprise – bug after this many years of dealing with such a gaping whole I have become use to it. They say not to normalize such things but a few decades can tend to wear you down, and I built my own interfaces for the platforms I run on years ago.

Maybe this is something else that AI agents can do?