logical, allocatable :: l(:)
l = [real :: ] == [integer :: ]
print *,size(l),l
end program
I tried it with 5 compilers, and all printed 0 and nothing else. The gotcha is that l looks as if it should be [.true.] or [.false.], but that would have size 1.
Attention! The do-variable in io-implied-do does not behave in the same way as a data-i-do-variable or an ac-do-variable. The latter are statement entities, the former is not.
Character(len=*), Parameter :: c = " 1 2 3 "
Character(len=:), Allocatable :: k
Integer :: J(3)
k = c
i = 42
Read(k,*) (J(i),i=1,3) ! the same i as program i
Print '(I2)', i ! 4, if no i/o errors, undefined otherwise
Print '(3(I1))', J
i = 42
J = [(i,i=1,3)] ! a new i with statement scope
Print '(I2)', i ! 42
Print '(3(I1))', J
End program
Curiously enough, if you use IMPLICIT NONE, then you need to declare i as an integer, suggesting it is the same variable all over. ISTR something similar happens with data statements - index variables used there are not quite the same as variables with the same name outside these statements.
This is an interesting corner case. The output is what would be expected, but it might not be obvious to a new fortran programmer. If you change the variable to scalar, and change the statement to
I think it just means that all of the "i"s that appear in that scoping unit must be the same TKR, not that they must share the same memory. This is sort of what happened with statement function arguments in f77, the formal argument types had to agree in type with the later actual arguments, but whether they were stored in the same memory locations, or in registers, or in the hardware pipeline did not really matter to the language. I have always assumed that this “feature” was retained in the language in order to facilitate those last two types of optimizations.
Recently I was testing a DFT code and my coding agent hit this problem after a hanging process timed out:
About recursive I/O: I don’t think we should report it to FLEUR, because FLEUR’s code isn’t at fault. In the original source, a function is called inside a WRITE (oUnit, ...) list, but that function does no I/O, which is allowed. The recursive WRITE was the debug line I had added inside it. That is non-conforming (F2023 12.12 para 2, p. 274), which is why gfortran hung.
Here is a gotcha that not many compilers seem to warn about (Compiler Explorer):
! Can an INTEGER be edited with an L edit descriptor? Which values are true?
do j = -2, 2
print '(I3, 1X, L1)', j, j
end do
end
gfortran and flang print T T F T T, ifx and nvfortran print F T F T F. LFortran returns a runtime error.
I’m not sure passing an integer where a logical is expected is “legal” to begin with, but I’ve encountered codes that do it nonetheless. The portable option depends on the required integer to logical mapping, but I guess j /= 0 would be the “default choice”.
It is definitely not standard to use logicals as numeric values.
It is not standard to use numerical values as logicals. Even the INT function should not take a logical value. But almost everyone seems to have an extension for that, and give different results for what values are true and false. That is definitely an extension I could do without. I have run into that multiple times and even have an overload to force it to have a predictable result. I cannot believe I did not post that one as it is a longstanding personal peeve!
PS: Related to that, it is not standard to compare two logical expressions with == and /=. The standard requires .eqv. and .neqv. be used. I think every one of the programming environments that allows logicals as numerics allows that extension, maybe by promoting to integers (?).
PSS: I also think I was surprised that default REAL, INTEGER, and LOGICAL are the same storage size. That means a default Boolean value typically takes up 32 or 64 bits! And the smallest you can probably request it be is one byte, not one bit (I think a LOGICAL really was a bit on one Cray platform long ago, but not sure I remember that correctly anymore) . Anyway, does not probably matter in a lot of codes that make scant use of large LOGICAL arrays or even large temporary masks, but using the smallest LOGICAL kind can be a significant tweek if you do use a large LOGICAL array.
program small_logical
implicit none
! Requests the smallest logical kind with at least 1 bit of storage
integer, parameter :: lk = selected_logical_kind(1)
logical(kind=lk),allocatable :: bigmask(:)
logical(kind=lk) :: T, F,flag
flag = .true._lk
flag = T ! nicer if willing to dedicate T and F to logical values
print *, "Kind:", lk, "Storage size (bits):", storage_size(flag)
print *, "default storage size(bits):", storage_size(.true.)
end program small_logical
I was not sure what I expected for ANY() and ALL(). Looking at your post I think I changed my mind three times. According to the Fortran standard, ALL returns .true. and ANY returns .false. when given a zero-sized (empty) mask. According to AI … “This behavior follows standard mathematical logic for empty sets (vacuous truth and existential quantification) and ensures consistency across array operations.”
I think of ANY() as initializing the result to .false. and then branching out of the loop when the first .true. is found, and I think of ALL() as initializing to .true. and branching out when the first .false. is found. I think that is consistent with the zero-size array cases where the initialization occurs, but there is no loop.
The other side of that coin is whether an integer value can be read with .true., .false., T, or F input. VAX fortran allowed this with T resulting in -1 and F resulting in 0. Those were the internal representations of .true. and .false. in VAX fortran. I don’t remember if this was just list-directed i/o or also included format fields.
As for the original question, this depends on whether the C convention (nonzero->true, zero->false) is used or whether only the least significant bit is examined. I’m not certain about this, but I think most fortran compilers in the 1960s and 1970s used just the lsb of the logical variable. Then came the Bell Labs compilers, f77 and f2c, which, because they compiled to C code, used the C convention. After that, fortran compilers could be expected to use either of the conventions.
Although the article is still mostly valid 26 years later, the part about values has changed a little bit (no pun intended):
Fortran 2003 claimed interoperability with C, and the Intel compilers (which are the intended target of the article) even have a standard-semantics flag that “fixes” the LOGICAL value.
The logical stuff dates back to Fortran 66 limitations. It is all non-Standard. Since there are direct replacements, IMHO no compiler should compile it by default. Unfortunately most do. So the codes never get fixed.
Storage association (default integer, logical, and real are 1 numeric storage unit, default complex and double precision are 2 numeric storage units) is trickier. It was intended to make common blocks and equivalence more portable. With modules, derived types, and dynamic memory management, much of this is no longer needed. However if we ever get unions in derived types it might still be useful.