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

Wired Performance

Blane_J_
New Contributor I
773 Views

I happen to test the lower_limit<=variable<=upperlimit form of selection statement in IF block, and it seems that the statement is always true even if the upperlimit is little than the lower_limit, why is that ? For example:

integer :: var = 10

if(9 <= var <= 11) write(*,*)  "Yes"              ! When execute, "Yes" is printed out.
if(11 <= var <= 9) write(*,*)  "Yes Again"        ! When execute, "Yes Again" is printed out.

 

0 Kudos
6 Replies
Steven_L_Intel1
Employee
773 Views

Fortran doesn't have the type of comparison test you're doing with three operands. What you get is the equivalent of ((9 <= var) <= 11) where (9 <= var) evaluates to .TRUE. or .FALSE.. Normally you would not be allowed to compare a LOGICAL with an INTEGER but we support that as an extension.)

You need to rewrite this as:

IF ((var >= 9) .and. (var <= 11)) ...

 

0 Kudos
Blane_J_
New Contributor I
773 Views

Thanks, Steve. By the way, what does .true. or .false. mean when either of them is in comparison ?

0 Kudos
andrew_4619
Honored Contributor III
773 Views

The "value" of .true. or .false. is not standard in Fortran and integer /logical comparisons are a language extension. Typically:

As an extension for backwards compatibility with other compilers, GNU Fortran allows the implicit conversion of LOGICAL values to INTEGER values and vice versa. When converting from a LOGICAL to an INTEGER.FALSE. is interpreted as zero, and .TRUE. is interpreted as one. When converting from INTEGER to LOGICAL, the value zero is interpreted as .FALSE. and any nonzero value is interpreted as .TRUE..

In Intel Fortran the integer value of True/False can be changed by compiler switches. This is all extensions to language for compatibility with old stuff and should reality be avoided as the behaviour of your code could be unpredictable if you change compiler

 

 

fpscomp logicals

Specifies that integers with a non-zero value are treated as true, integers with a zero value are treated as false. The literal constant .TRUE. has an integer value of 1, and the literal constant .FALSE. has an integer value of 0. This representation is used by Intel Fortran releases before Version 8.0 and by Fortran PowerStation.

The default is fpscomp nologicals, which specifies that odd integer values (low bit one) are treated as true and even integer values (low bit zero) are treated as false.

The literal constant .TRUE. has an integer value of -1, and the literal constant .FALSE. has an integer value of 0. This representation is used by Compaq* Visual Fortran. The internal representation of LOGICAL values is not specified by the Fortran standard. Programs which use integer values in LOGICAL contexts, or which pass LOGICAL values to procedures written in other languages, are non-portable and may not execute correctly. Intel recommends that you avoid coding practices that depend on the internal representation of LOGICAL values.

The fpscomp logical option affects the results of all logical expressions and affects the return value for the following Fortran features:

  • The INQUIRE statement specifiers OPENED, IOFOCUS, EXISTS, and NAMED

  • The EOF intrinsic function

  • The BTEST intrinsic function

  • The lexical intrinsic functions LLT, LLE, LGT, and LGE

0 Kudos
Blane_J_
New Contributor I
773 Views

That's detailed enough, Thanks app.

0 Kudos
TimP
Honored Contributor III
773 Views

Fpscomp logicals is among the settings included in standard-semantics for f2003 compliance.

0 Kudos
Reply