Reputation: 359
I've written a scientific fortran code without using any specific fortran standard. But I have now to declare which fortran standard I'm using.
I said I'm using fortran 2003 because I need the get_command_argument and command_argument_count intrinsic functions. However when using the flag -std=f2003 to check the code standards the compilation fails.
I get errors concerning the type declaration of reals in some parts. For example when I declare variables in the module:
module innout
implicit none
real*8,parameter :: nan=-1.
real*8,allocatable,save :: windU(:),windV(:)
real*8,allocatable,save :: input_param(:,:),input_rad(:,:)
real*8,allocatable,save :: prein(:),input(:),ref_lev(:)
character(30),allocatable,save :: sceneclass(:)
end module innout
I get the messages "Nonstandard type declaration REAL*8" in all real variables.
Anyone knows what's happening?
Upvotes: 1
Views: 2511
Reputation: 18098
The kind
specifier is the way to go...
If you limit yourself to Fortran 2003 Standard, then you need to use the kind()
or selected_real_kind()
function to determine the corresponding kind first:
module innout
implicit none
integer,parameter :: REAL64 = kind(1.d0)
real(kind=REAL64),parameter :: nan=-1._REAL64
real(kind=REAL64),allocatable,save :: windU(:),windV(:)
real(kind=REAL64),allocatable,save :: input_param(:,:),input_rad(:,:)
real(kind=REAL64),allocatable,save :: prein(:),input(:),ref_lev(:)
character(30),allocatable,save :: sceneclass(:)
end module innout
If you are allowed to/your compiler supports Fortran 2008, I would recommend the module ISO_Fortran_env
and the pre-defined constant REAL64
:
module innout
use,intrinsic :: ISO_Fortran_env, only: REAL64
implicit none
real(kind=REAL64),parameter :: nan=-1._REAL64
real(kind=REAL64),allocatable,save :: windU(:),windV(:)
real(kind=REAL64),allocatable,save :: input_param(:,:),input_rad(:,:)
real(kind=REAL64),allocatable,save :: prein(:),input(:),ref_lev(:)
character(30),allocatable,save :: sceneclass(:)
end module innout
Upvotes: 1
Reputation: 359
I found already the answer.
Using the following in the variable declaration, it seems to work fine:
integer, parameter :: dp = selected_real_kind(15, 307)
From http://fortranwiki.org/fortran/show/Real+precision
Upvotes: 0
Reputation: 78316
real*8
is not, and never has been, a Fortran-standard type declaration. These days the simplest approach to declaring a 64-bit real is probably to import the named-constant real64
from the intrinsic module iso_fortan_env
, like this:
use, intrinsic :: iso_fortran_env
...
real(real64) :: my_var
There are other ways, involving selected_real_kind
and other mechanisms, but if you want to program with IEEE floating-point types then real64
and real32
are a good way to go.
As @AlexanderVogt has pointed out in a comment these standard named constants were added to the language in the 2008 standard. Most recent compiler versions I have worked with already implement them.
Upvotes: 2