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

Is this relational operation correct?

migmile
Beginner
1,120 Views
if (10<20) .....
Is this relational operation correct?
0 Kudos
9 Replies
Steven_L_Intel1
Employee
1,120 Views
Well, it's probably accepted by the compiler, but likely doesn't do what you want. It is not legal in standard Fortran because (10
Rewriting this to something more likely would be:

if ((10 < i) .and. (i < 20))

Message Edited by sblionel on 03-15-2005 12:40 PM

0 Kudos
migmile
Beginner
1,120 Views
Ok.
As written in "International Standard programming language Fortran" by J3:
"An intrinsic binary operation is an operation of the form x1 intrinsic-operator x2 where x1 and
x2 are of the intrinsic types (4.4) listed in Table 7.1 for the binary intrinsic operator :"
And later:
"A numeric relational intrinsic operation is a relational intrinsic operation where the operands are of numeric type. A character relational intrinsic operation is a relational intrinsic operation where
the operands are of type character."

So, in prev. example the second relational operation ((10
0 Kudos
migmile
Beginner
1,120 Views
Sorry, it is continuation:)
So, in prev. example the second relational operation ((10
0 Kudos
migmile
Beginner
1,120 Views
Ok.
As written in "International Standard programming language Fortran" by J3:
"An intrinsic binary operation is an operation of the form x1 intrinsic-operator x2 where x1 and
x2 are of the intrinsic types (4.4) listed in Table 7.1 for the binary intrinsic operator :"
And later:
"A numeric relational intrinsic operation is a relational intrinsic operation where the operands are of numeric type. A character relational intrinsic operation is a relational intrinsic operation where
the operands are of type character."

So, in prev. example the second relational operation
 ((10 < i) < 20) 
is illegal, as you write: "It is not legal in standard Fortran". For my opinion, compiler should tell me about it. Am i right?
0 Kudos
Steven_L_Intel1
Employee
1,120 Views
If you ask it to do so, yes:

c:Myprojects>ifort /stand t.f90
Intel Fortran Compiler for 32-bit applications, Version 8.1 Build 20050201Z Package ID: w_fc_pc_8.1.028
Copyright (C) 1985-2005 Intel Corporation. All rights reserved.

t.f90(2) : Warning: Fortran 95 does not allow this data type conversion.
if (10 < i < 20) j = 4
-------^

Actually, I've been lobbying to make the compiler less forgiving on this sort of conversion, but it has a very long history (30 years or more).
0 Kudos
migmile
Beginner
1,120 Views
All right. Thank you Steve :)))))
I was sure that the reason is long historical "tail" of FORTRAN. And i agree with you, that Fortran must eliminate such ... inconsistency, gap, "ear" of history.

By what reason CVF allow that expression? I try gfortran and it show me the error (not warning!).

migmile
0 Kudos
Steven_L_Intel1
Employee
1,120 Views
It is DEC Fortran history that "free conversion" between LOGICAL and numeric types is allowed. The value you get is the binary form of the LOGICAL value interpreted as a signed integer. .FALSE. is zero and .TRUE. is -1 (unless you have selected /fpscomp:logicals, in which case .TRUE. is +1). This is documented in the Language Reference Manual.

This extension was widely taken advantage of on VMS, where routines that returned an integer status could be tested as if they were logicals - success statuses tested as true and error statuses tested as false. If it were only the conversion of integer to logical, that might not be so bad, but, as you found, logicals get free conversion to integer or even real and complex, with bizarre results.

Another place people run into this is list-directed input, where T and F are accepted for numeric input. These are interpreted as .TRUE. and .FALSE. and converted according to the documented rules. I doubt anyone really depends on this behavior, though, and it has been the cause of many complaints over the years.

Newer compilers that don't have the generous history of the DEC-heritage compilers tend to leave out such extensions, often with good reason. It is a "pet project" of mine to get our compilers to at least warn you about such usage by default.
0 Kudos
migmile
Beginner
1,120 Views
Thank you Steve! Very interesting history!
I now about conversion from LOGICAL to INTEGER, but dont now reasons.
But look, all program languages are developed in one direction - to protect programmer from making errors. And FORTRAN with his "rich history" permits to make VERY MANY errors, especially for beginners.

migmile
0 Kudos
Steven_L_Intel1
Employee
1,120 Views
It is very common to mix integers and logicals. For example, programs do things such as (I .AND. J) where I and J are integers, expecting "bitwise AND", even though the .AND. operator is a "logical AND" which need produce only two distinct values, .TRUE. and .FALSE..
0 Kudos
Reply