I was reading about Fortran’s intrinsic function REAL
recently and discovered that it behaved inconsistently, so I fixed it by overloading the function in a module
that I generally use
everywhere.
Category Archives: Fortran
KIND declaration quirk in Fortran
Here’s another quirk I found about KIND
declarations in Fortran. Here are two ways to declare the KIND
of a variable:
COMPLEX(KIND=8) :: b
Here’s the interesting part: both of these variables have the same KIND
! They are both ‘double precision’, generally using 8 bytes of storage for each of the real and imaginary parts of the number, or 16 bytes total. I say ‘generally’ because this can be compiler specific. Check out the module ISO_FORTRAN_ENV
for more portability.
Update (2012-06-19): I learned why there is a difference here. It turns out that the declaration complex*16
is not part of any Fortran standard. Instead, it is a vendor extension that is supported by most, if not all, compilers today. So if you compile with strict standards-checking like gfortran -std=f2008
, then you will receive an error. The correct way to write it is with complex(KIND=8)
.
Precision of numbers in Fortran
So, we have a few ways of entering numbers in a Fortran program. Different ways of writing the number shows it’s precision. Thus,