Fortran:整数* 4与整数(4)与整数(种类= 4) [英] Fortran: integer*4 vs integer(4) vs integer(kind=4)
问题描述
我试图学习Fortran,我看到很多不同的定义被传递,我想知道他们是否试图完成同样的事情。以下是什么区别?
-
整数* 4
-
integer(4)
-
integer(kind = 4)$在Fortran> 90中,最好的方法是使用内部函数指定你需要的精度 - 这保证了可移植性和你得到你需要的精度。例如,要获得将支持至少8位小数位的整数i和my_int,可以使用:
integer,参数: :RegInt_K = selected_int_kind(8)
integer(kind = RegInt_K):: i,my_int
将RegInt_K(或您选择的任何名称)定义为参数后,您可以在整个代码中将其用作符号。这也很容易改变精度。
请求8或9位十进制数字通常会得到一个4字节的整数。
整数* 4是一个常见的扩展,它返回到旧的FORTRAN来指定一个4字节的整数。
整数(4)或整数(RegInt_K)是整数(kind = 4)或整数(kind = RegInt_K)的缩写。整数(4)与整数* 4不同并且不可移植 - 语言标准没有指定种类的数值。大多数编译器对于4字节整数使用kind = 4,对于这些编译器,integer * 4和integer(4)将提供相同的整数类型 - 但也有例外,因此整数(4)是不可移植的并且最好避免。
reals的方法类似。
更新:如果您不想指定数字类型按要求的精度,而是通过他们将使用的存储,Fortran 2008提供了一种方法。 reals和整数可以用
使用
ISO_FORTRAN_ENV
模块之后的存储位数来指定,例如for一个4字节(32位)整数:
使用ISO_FORTRAN_ENV
整数(int32):: MyInt
gfortran手册在内部模块下有文档。
I'm trying to learn Fortran and I'm seeing a lot of different definitions being passed around and I'm wondering if they're trying to accomplish the same thing. What is the difference between the following?
integer*4
integer(4)
integer(kind=4)
解决方案In Fortran >=90, the best approach is use intrinsic functions to specify the precision you need -- this guarantees both portability and that you get the precision that you need. For example, to obtain integers i and my_int that will support at least 8 decimal digits, you could use:
integer, parameter :: RegInt_K = selected_int_kind (8) integer (kind=RegInt_K) :: i, my_int
Having defined "RegInt_K" (or whatever name you select) as a parameter, you can use it throughout your code as a symbol. This also makes it easy to change the precision.
Requesting 8 or 9 decimal digits will typically obtain a 4-byte integer.
integer*4 is an common extension going back to old FORTRAN to specify a 4-byte integer.
integer (4) or integer (RegInt_K) are short for integer (kind=4) or integer (kind=RegInt_K). integer (4) is not the same as integer*4 and is non-portable -- the language standard does not specify the numeric values of kinds. Most compilers use the kind=4 for 4-byte integers -- for these compilers integer*4 and integer(4) will provide the same integer type -- but there are exceptions, so integer(4) is non-portable and best avoided.
The approach for reals is similar.
UPDATE: if you don't want to specify numeric types by the required precision, but instead by the storage that they will use, Fortran 2008 provides a method. reals and integers can be specified by the number of bits of storage after
use
ing theISO_FORTRAN_ENV
module, for example, for a 4-byte (32-bit) integer:use ISO_FORTRAN_ENV integer (int32) :: MyInt
The gfortran manual has documentation under "intrinsic modules".
这篇关于Fortran:整数* 4与整数(4)与整数(种类= 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!