簡體   English   中英

Fortran 2003中的非標准類型聲明

[英]Nonstandard type declaration in Fortran 2003

我寫了一個科學的fortran代碼,沒有使用任何特定的fortran標准。 但我現在要宣布我正在使用哪種標准。

我說我正在使用fortran 2003因為我需要get_command_argument和command_argument_count內部函數。 但是,當使用標志-std = f2003檢查代碼標准時,編譯失敗。

我在某些部分遇到有關實數類型聲明的錯誤。 例如,當我在模塊中聲明變量時:

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

我在所有實變量中得到消息“非標准類型聲明REAL * 8”。

誰知道發生了什么?

real*8不是,也絕不是Fortran標准的類型聲明。 這些天來,宣布了64位真正的最簡單的方法可能是導入指定的常數 real64從內在模塊iso_fortan_env ,就像這樣:

use, intrinsic :: iso_fortran_env
...
real(real64) :: my_var

還有其他方法,涉及selected_real_kind和其他機制,但如果你想用IEEE浮點類型real64 ,那么real64real32是一個很好的方法。

正如@AlexanderVogt在評論中指出的那樣,這些標准命名常量被添加到2008標准中的語言中。 我使用過的最新編譯器版本已經實現了它們。

kind說明符是要走的路......

如果您將自己限制為Fortran 2003 Standard,那么您需要先使用kind()selected_real_kind()函數來確定相應的類型:

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

如果您允許/您的編譯器支持Fortran 2008,我建議使用模塊ISO_Fortran_env和預定義的常量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

我找到了答案。

在變量聲明中使用以下內容,它似乎工作正常:

integer, parameter :: dp = selected_real_kind(15, 307)

來自http://fortranwiki.org/fortran/show/Real+precision

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM