为什么我们需要1,2,4,8个字节将逻辑变量存储在fortran中? [英] Why we need 1,2,4,8 bytes to store logical variable in fortran?
问题描述
我不明白,因为逻辑类型仅具有两种情况: true
和 false
,因此为什么我们需要逻辑(1)
,逻辑(2)
,逻辑(4)
,逻辑(8)
在Fortran中?
I don't understand that since logical type only has two cases: true
and false
, then why we need logical(1)
,logical(2)
,logical(4)
,logical(8)
in Fortran?
我们只需要 1位
.
有人可以解释吗?
推荐答案
首先,Fortran 不是说我们有逻辑类型,每个逻辑类型分别占用1、2、4和8个字节,它们当然不是 logical(1)
, logical(2)
, logical(4)
和 logical(8)代码>.一个实现可以选择来提供这些名称,将其称为这些名称.
First, Fortran doesn't say that we have logical types taking up 1, 2, 4 and 8 bytes each, and they certainly aren't logical(1)
, logical(2)
, logical(4)
, and logical(8)
. An implementation may choose to offer those, calling them those names.
一个逻辑变量确实可以只有两个值.从(F90,尽管F2008在不同的地方说的相同)标准4.3.2.2:
A logical variable can indeed be of only two values. From the (F90, although F2008 says the same in a different place) standard 4.3.2.2:
逻辑类型具有两个值,分别表示对和错.
The logical type has two values which represent true and false.
处理器必须为逻辑类型的数据提供一种或多种表示方法.每种这样的方法都有一个称为 kind 类型参数的类型参数的值.
A processor must provide one or more representation methods for data of type logical. Each such method is characterized by a value for a type parameter called the kind type parameter.
[这里强调,以后逐字记录.]
[Emphasis here and later verbatim.]
对于默认类型的逻辑类型,存储关联规则(14.6.3.1)表示:
For a logical type of default kind the rules of storage association (14.6.3.1) say that:
(1)默认整数,默认实数或默认逻辑类型的非指针标量对象占用单个数字存储单元.
(5)类型为[..]非默认逻辑[..]的非指针标量对象占用单个未指定存储单元,每种情况都不同.
因此,编译器必须提供与整数和实型大小相同的逻辑类型,但同样,它可以提供占用1位,1个字节或任何其他形式的表示形式.对于任何给定的表示形式,种类数和大小(因此,我的第一段:这个问题并非普遍有效)是特定于实现的.就是说,没有 SELECTED_LOGICAL_KIND
(或此类)固有的.
So, the compiler must offer a logical type which is of the same size as an integer and real type, but, equally, it can offer representations taking up 1 bit, 1 byte, or whatever. The kind number, and size, for any given representation (hence my first paragraph: the question isn't universally valid) is implementation-specific. That said, there is no SELECTED_LOGICAL_KIND
(or such) intrinsic.
关于为什么多种表示有用的原因,归结为提供选择,也许是针对特殊情况,例如阵列和理想的内存管理(有些人喜欢玩非便携式的技巧).但是,内存访问/对齐要求建议标量逻辑至少应为一个字节(或填充使其相同).对于C互操作性(F2003 +),有一个 C_BOOL
对应于配套C处理器的 _Bool
,它的大小不必相同.
As to why multiple representations can be useful, that comes down to offering a choice, perhaps for special cases such as for arrays and ideal memory management (some people like to play non-portable tricks). However, memory access/alignment requirements suggest that a scalar logical would be at least one byte (or padding make it the same). For C interoperability (F2003+) there is a kind C_BOOL
corresponding to the companion C processor's _Bool
, which needn't be the same size.
这篇关于为什么我们需要1,2,4,8个字节将逻辑变量存储在fortran中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!