On 9 Jul., 10:38, "Jimmy" <
jh...@sky.com> wrote:
> Also, in the sense of storage, you can use the 'transfer' function, eg
> program test
> logical :: a
> integer :: i,j
> i=99
> a=transfer(i,a)
> j=transfer(a,j)
I want to add a warning: Especially with higher optimizations such as -
O2 or -O3 one can quickly run into problems. Some compilers regard the
highest or the lowest bit as relevant to decide whether a number
is .true. or .false. While false = "0" usually (always) works, other
number might not. With C, integers nonequal zero are true and zero
false - but also there, using the "bool" type, there might be problems
if not "true/false" is used by if an integer is casted.
The safest way to convert integers to logicals is: (i /= 0)
For logical -> integer, one can use TRANSFER, but one should not
expect a special number, .TRUE. might be -1 or 1 (or any other number,
so far I only encountered -1 and 1, though).
GCC is one of those compilers, where IF(TRANSFER(2,.true.)) is
regarded as false in high optimization levels. I do not know whether
it affects GCC 4.0.x as used by g95 nor how g95 handles logicals as
datatype internally - thus it may or may not effect g95. (And for the
example above: As this is compile-time evaluable, the result of the
transfer is presumably .TRUE. independent of the optimization level.)
Tobias