我不明白,既然逻辑类型只有“真”和“假”这两种情况,那么为什么在Fortran中我们需要logical(1)
、logical(2)
、logical(4)
、logical(8)
?
我们只需要1个比特(bit)
。
能否有人解释一下呢?
SELECTED_LOGICAL_KIND
(或类似的)内在函数。C_BOOL
,对应于伴随C处理器的_Bool
,它不必是相同的大小。LOGICAL
The FORTRAN standard requires logical variables to be the same size as INTEGER/REAL >variables (see the chapter on memory management) although only one bit is really needed to implement this type.
The values used to implement the logical constants .TRUE. and
.FALSE. differ:| VMS | Sun | IRIX | -----------|------------|-----------|-----------|----------- .TRUE. | -1 | 1 | 1 | -----------|------------|-----------|-----------|----------- .FALSE. | 0 | 0 | 0 | -----------|------------|-----------|-----------|-----------
Unix machines naturally adopted the C convention, VMS has a seemingly strange value for .TRUE., however on a closer look you will see that
if .FALSE. is "all bits 0", .TRUE. should be "all bits 1", in two's complement signed integers the number with all bits set to 1 is -1.
http://www.ibiblio.org/pub/languages/fortran/ch2-3.html
这似乎是为了更简单的内存管理而设计的。
SELECTED_LOGICAL_KIND
已经在 Fortran 2023 中添加。请参考 https://fortran.bcs.org/2022/AGM22_Reid.pdf 或规范以获取详细信息。 - Jeff Hammond