Fortran:整数 * 4 与整数(4)与整数(种类 = 4) [英] Fortran: integer*4 vs integer(4) vs integer(kind=4)
问题描述
我正在努力学习 Fortran,我看到很多不同的定义被传递,我想知道他们是否正在尝试完成同样的事情.以下有什么区别?
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?
整数*4
整数(4)
整数(kind=4)
推荐答案
在 Fortran >=90 中,最好的方法是使用内部函数来指定您需要的精度——这既保证了可移植性,又保证了您获得所需的精度需要.例如,要获得至少支持 8 个十进制数字的整数 i
和 my_int
,您可以使用:
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
已将 RegInt_K
(或您选择的任何名称)定义为 parameter
,您可以在整个代码中将其用作符号.这也使更改精度变得容易.
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.
请求 8 位或 9 位十进制数字通常会获得一个 4 字节整数.
Requesting 8 or 9 decimal digits will typically obtain a 4-byte integer.
integer*4
是一个常见的扩展,可以追溯到旧的 FORTRAN 以指定一个 4 字节整数.虽然,这种语法不是,也从来不是标准的 Fortran.
integer*4
is an common extension going back to old FORTRAN to specify a 4-byte integer. Although, this syntax isn't and was never standard Fortran.
integer (4)
或 integer (RegInt_K)
是 integer (kind=4)
或 integer (kind=RegInt_K)
.integer (4)
与 integer*4
不同,并且不可移植——语言标准没有指定种类的数值.大多数编译器将 kind=4
用于 4 字节整数——因为这些编译器 integer*4
和 integer(4)
将提供相同的整数类型——但也有例外,所以 integer(4)
是不可移植的,最好避免使用.
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.
实数的方法类似.
更新:如果您不想通过所需的精度指定数字类型,而是通过它们将使用的存储来指定,Fortran 2008 提供了一种方法.实数和整数可以通过 use
使用 ISO_FORTRAN_ENV
模块后的存储位数指定,例如,对于 4 字节(32 位)整数:
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 the ISO_FORTRAN_ENV
module, for example, for a 4-byte (32-bit) integer:
use ISO_FORTRAN_ENV
integer (int32) :: MyInt
gfortran 手册在内在模块"下有文档.
The gfortran manual has documentation under "intrinsic modules".
这篇关于Fortran:整数 * 4 与整数(4)与整数(种类 = 4)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!