ISO_FORTRAN_ENV 或 -fdefault-real-8 将实数提升为双精度

ISO_FORTRAN_ENV or -fdefault-real-8 to promote reals to double precision

我一直在使用 gfortran-fdefault-real-8 选项来自动将程序中任何地方声明的每个 REAL 提升为双精度, 以及任何常量例如 1.23。如果我想切换回单精度,我只需删除该选项并重新编译,而无需更改源代码中的单个字符。

我开始使用 ISO_FORTRAN_ENV 模块,因为它允许我使用常量,例如 INPUT|OUTPUT|ERROR_UNIT,以及 IOSTAT_ENDIOSTAT_EOR 以及其他(似乎是朝着可移植性方向迈出的又好又容易的一步,我错了吗?)。从那时起,我一直看到并忽略以下警告

Warning: Use of the NUMERIC_STORAGE_SIZE named constant from intrinsic module ISO_FORTRAN_ENV at (1) is incompatible with option -fdefault-real-8

因为这样的不兼容到目前为止似乎没有影响。

现在,如果可能并且值得的话,我想去掉这个警告。

如果我理解正确,为了避免出现此警告,我应该放弃 -fdefault-real-8 选项并将每个 REAL 更改为 REAL(real64) and/or to REAL(dp)(前提是,在后一种情况下,语句 USE, INTRINSIC :: ISO_FORTRAN_ENV, dp => real64 放在该单元中),这对于 sedvim.

来说不是一件难事

尽管如此,在我看来,此更改与使用 -fdefault-real-8 选项不同,因为 所有常量都将保持单精度 只要我不要向它们添加 d0。 假设删除了 -fdefault-real-8 选项并且在任何地方都使用了 ISO_FORTRAN_ENV ,是否有任何方法可以使程序中的任何常量表现得像每个常量 d0后缀? 无论这是否可能,我是否正确推断我可以将以下行放在一个模块中,该模块由所有其他程序单元使用,然后每个程序单元都可以使用 dp 作为类型参数?

USE, INTRINSIC :: ISO_FORTRAN_ENV
INTEGER, PARAMETER :: dp = real64

我更喜欢这种方式,因为我可以通过仅更改该行来切换到 real32real128 或其他任何方式。

如果您只想消除警告而不关心 -fdefault-real-8 对存储关联和某些 Fortran 标准要求的影响,请不要从模块中导入 NUMERIC_STORAGE_SIZE。例如,

USE, INTRINSIC :: ISO_FORTRAN_ENV, only: INPUT_UNIT,OUTPUT_UNIT,ERROR_UNIT

Assumed the -fdefault-real-8 option is removed and ISO_FORTRAN_ENV is used anywhere, is there any way to make any constant across the program behave as each had d0 suffix?

没有

顺便说一句,d0double precision 完全相同,所以这也没有太多固定,因为 double precision 的含义允许变化real.

Whether or not this is possible, have I correctly extrapolated that I can put the following lines in a single module which is used by all others program units, each of which can then use dp as kind type parameter?

是的。这是一种常见的做法。