Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Legal operation

Ilie__Daniel
Beginner
726 Views

Hello!

I have the following code:

[fortran]program main
  implicit none
  
  integer, dimension(4) :: a
  logical :: res
  
  res = .false.
  
  a = (/ 1, 2, 3, 4 /)
  res = a(1) < a(2) < a(3) < a(4)

end program main[/fortran]

I thought that Line 10would not be legal, but as it happens IVF let's me get away with it.

Is this line treated like: res = a(1) < a(2) .and. a(2) < a(3).and. a(3) < a(4) ?

If I compile with option /stand:f95, then I get two warning messages for the highlighted line:

Warning: Fortran 95 does not allow this data type conversion. Why is this?

Thank you for your time.

Daniel.

0 Kudos
1 Solution
IDZ_A_Intel
Employee
726 Views

This is an extension that goes back more than 30 years. It made sense, sort of, on VMS, allowing you to test VMS status values for success or failure with a LOGICAL test, but the drawbacks are more severe outside of that context.

The way it is interpreted is this: if the LOGICAL expression evaluates to .TRUE., that is treated as -1; .FALSE. is 0. (If you have /fpscomp:logicals in effect, the values are 1 and 0). The reverse conversion is also allowed - a numeric value is converted to integer. If the value is even it's .FALSE.,if odd, .TRUE.

We used to do this conversion in list-directed and NAMELIST I/O as well, but no longer do so by default. So reading T in list-directed to a REAL is now an error. This part of the extension should never have been created in the first place.

View solution in original post

0 Kudos
2 Replies
Arjen_Markus
Honored Contributor II
726 Views

IIRC, it is a compiler extension: a silent conversion of logical values to integers.

If this happens, the line you posted does make sense - sort of ;).

I do not know how exactly it is interpreted, but it would be a bad idea IMHO

to rely on the compiler's acceptance. It is certainly not intended for this type

of work!

If you really, reallyneed an operation like that, you might consider creating

a user-defined operation. It can be done, but it is not pretty ;).

A better alternative would be:

n = size(a)

res = all( a(1:n-1) < a(2:n) )

Regards,

Arjen

0 Kudos
IDZ_A_Intel
Employee
727 Views

This is an extension that goes back more than 30 years. It made sense, sort of, on VMS, allowing you to test VMS status values for success or failure with a LOGICAL test, but the drawbacks are more severe outside of that context.

The way it is interpreted is this: if the LOGICAL expression evaluates to .TRUE., that is treated as -1; .FALSE. is 0. (If you have /fpscomp:logicals in effect, the values are 1 and 0). The reverse conversion is also allowed - a numeric value is converted to integer. If the value is even it's .FALSE.,if odd, .TRUE.

We used to do this conversion in list-directed and NAMELIST I/O as well, but no longer do so by default. So reading T in list-directed to a REAL is now an error. This part of the extension should never have been created in the first place.

0 Kudos
Reply