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

Warnings for numerical operations on logical objects

IanH
Honored Contributor III
458 Views

I'm not sure whether the compiler is required to diagnose this sort of silliness or not with /stand, but it would be nice if it did.

PROGRAM LogicalOperations
  IMPLICIT NONE
  LOGICAL :: la, lb
  
  la = .TRUE.
  lb = .FALSE.
  
  PRINT *, -la
  PRINT *, +la
  PRINT *, la + lb
  PRINT *, la - lb
  PRINT *, la * lb
  PRINT *, lb / la
  PRINT *, la ** lb
END PROGRAM LogicalOperations

 

>ifort /check:all /warn:all /standard-semantics /stand "2014-09-21 logical-operations.f90"
Intel(R) Visual Fortran Compiler XE for applications running on IA-32, Version 15.0.0.108 Build 20140726
Copyright (C) 1985-2014 Intel Corporation.  All rights reserved.

Microsoft (R) Incremental Linker Version 10.00.40219.01
Copyright (C) Microsoft Corporation.  All rights reserved.

"-out:2014-09-21 logical-operations.exe"
-subsystem:console
"2014-09-21 logical-operations.obj"

 

0 Kudos
7 Replies
Steven_L_Intel1
Employee
458 Views

Yes, it should warn about this. I'm rather astonished it doesn't. I will let the developers know. Thanks.

0 Kudos
Steven_L_Intel1
Employee
458 Views

Escalated as DPD200361277.

0 Kudos
dboggs
New Contributor I
458 Views

How old are "the developers"? Once upon a time (in a galaxy far far away) it was very common to perform arithmetic on logical variables and it was considered quite acceptable. Assuming that the arithmetic statements shown perform some useful results, I dare say it would be quite verbose and more difficult to decipher to accomplish the same thing using "modern" code involving nested IF THEN statements.

Nevertheless one must appreciate the modern dogma which forbids this (along with other things like computed GO TO) and yes, if the modern Fortran rules say not to do this, then the compiler should AT LEAST warn about it. Old timers would not get so excited however.

Is it forbidden now because compilers are not required to equate .FALSE. = 0 and .TRUE. = 1?

0 Kudos
IanH
Honored Contributor III
458 Views
It was never permitted in standard code. Those operations are explicitly described in the standards over the years as being for integer, real (and double precision) or complex type. A separate set of operations is defined for logical and character. Similarly, there was never any requirement for the integer value of a logical constant (LOGICAL(C_BOOL) perhaps aside in more recent times). I thought using -1 for .true. was common.
0 Kudos
TimP
Honored Contributor III
458 Views
dboggs wrote:

Is it forbidden now because compilers are not required to equate .FALSE. = 0 and .TRUE. = 1?

Prior to availability of MERGE, some compilers supported schemes for treating logical expressions as masks (requiring .true. to match -1). You may have noticed that ifort -standard-semantics invokes the rather ancient -fpscomp:logicals making .true. match 1 in support of iso_c_interop.
0 Kudos
Steven_L_Intel1
Employee
458 Views

As Ian says, it has always been prohibited in the language. But limited mixing of integer and logical made some coding simpler, though typically not with arithmetic operators. Back on VMS, the condition handling standard defined status codes explicitly so that you could test for success/failure by doing an odd/even test on the value, which is where the Intel Fortran odd/even test for logicals came from.

Note that .TRUE. is indeed 1 with fpscomp:logicals, that doesn't mean that only the value 1 tests as true. The test is zero/nonzero.

0 Kudos
Steven_L_Intel1
Employee
458 Views

A future major release of the compiler will warn about this situation when standards checking is enabled.

0 Kudos
Reply