Reading writing fortran direct access unformatted files with different compilers

Ifort and gfortran do not use the same block size for record length by default. In ifort, the value of recl in your open statement is in 4-byte blocks, so your record length isn’t 985,600 bytes, it is 3,942,400 bytes long. That means the records are written at intervals of 3.9 million bytes. gfortran uses … Read more

how to generate integer random number in fortran 90 in the range [0,5]?

To support the answer by Alexander Vogt, I’ll generalize. The intrinsic random_number(u) returns a real number u (or an array of such) from the uniform distribution over the interval [0,1). [That is, it includes 0 but not 1.] To have a discrete uniform distribution on the integers {n, n+1, …, m-1, m} carve the continuous … Read more

Is Fortran easier to optimize than C for heavy calculations?

The languages have similar feature-sets. The performance difference comes from the fact that Fortran says aliasing is not allowed, unless an EQUIVALENCE statement is used. Any code that has aliasing is not valid Fortran, but it is up to the programmer and not the compiler to detect these errors. Thus Fortran compilers ignore possible aliasing … Read more

Does Fortran preserve the value of internal variables through function and subroutine calls?

To answer your question: Yes Fortran does preserve the value of internal variables through function and subroutine calls. Under certain conditions … If you declare an internal variable with the SAVE attribute it’s value is saved from one call to the next. This is, of course, useful in some cases. However, your question is a … Read more

Assumed size arrays: Colon vs. asterisk – DIMENSION(:) arr vs. arr(*)

The form real, dimension(:) :: arr declares an assumed-shape array, while the form real :: arr(*) declares an assumed-size array. And, yes, there are differences between their use. The differences arise because, approximately, the compiler ‘knows’ the shape of the assumed-shape array but not of the assumed-size array. The extra information available to the compiler … Read more

How to alias a function name in Fortran

Yes, Fortran has procedure pointers, so you can in effect alias a function name. Here is a code example which assigns to the function pointer “f_ptr” one function or the other. Thereafter the program can use “f_ptr” and the selected function will be invoked. module ExampleFuncs implicit none contains function f1 (x) real :: f1 … Read more