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

MAXLOC behavior

Tim_Gallagher
New Contributor II
961 Views
My colleagues and I were a bit surprised by the results of the MAXLOC function in the following example:

[fortran]PROGRAM TEST
   INTEGER, DIMENSION(-1:2) :: A
   DO I = -1, 2
      A(I) = 3-I 
   ENDDO
   WRITE(*,*) A
   WRITE(*,*) MAXLOC(A), MAXVAL(A)
END
[/fortran]

Which results in:

4 3 2 1
1 4

We had all expected the MAXLOC(A) to be -1 since that is the index of the array A.

Now, I can understand why the result is 1 -- if I were to make a subroutine/function and passed in an array where the argument was declared as DIMENSION(:) or DIMENSION(*), the indexing inside the function begins at 1 regardless of the start index of the actual array.

But, logically, this is obviously a surprising behavior. For what it's worth, gfortran does the same thing.

I don't think this conforms to the standard.

The standard says:

The result of MAXLOC (ARRAY) is a rank-one array whose element values are
the values of the subscripts of an element of ARRAY whose value equals the
maximum value of all of the elements of ARRAY.

I interpret "the values of the subscripts of an element of ARRAY" as meaning in the index-range of ARRAY (ie., -1:3). The behavior now is returning the subscript of an element in the argument internal to the MAXLOC routine, not the subscript of ARRAY.

Thoughts?

Tim



0 Kudos
4 Replies
TimP
Honored Contributor III
961 Views
We've all had to hash through this over the years. In effect, these intrinsics treat the array section they work on as starting at subscript 1, not trying to reconcile with whatever numbering those elements had outside. One reason: A result of -1 would be expected if, say, there were no elements in the array.
By the way, it's more confusing when you omit the DIM argument in a context which seems to call for it.
0 Kudos
Tim_Gallagher
New Contributor II
961 Views
I can certainly understand that, but does that behavior conform to the standard? The way I read it, it doesn't.

Ultimatley it's not a huge problem because once the oddity is known, it's easy to fix.

The easiest result for a situation where the array had no elements would be to return lbound(ARRAY)-1, which is just as easy to check in the code as a -1.

Tim
0 Kudos
Dave_Allured
New Contributor II
961 Views

Excerpt from fortran 2003 standard (J3/04-007):
13.7.73 MAXLOC ...
Case (i): The result of MAXLOC (ARRAY) ...

The ith subscript returned lies in the range 1 to ei, where ei is the extent of the ith dimension of ARRAY.

-------------------
Note, in the standard this spec is unconditional, without reference to lower bounds.

For further reading, search for a discussion about this in newsgroup comp.lang.fortran. In short, IIRC, most intrinsic functions follow the same rules for subscript association as user procedures. The only intrinsics that *do* reference the actual bounds directly are lbound() and ubound().

--Dave

0 Kudos
Hirchert__Kurt_W
New Contributor II
961 Views
I suggest you read the sentence in the standard after the one from which you quoted: "The ith subscript returned lies in the ranger 1 to ei, where ei is the extent of the ith dimension of ARRAY." I think that sentence makes it clear that ifort's behavior does conform to the standard.

-Kurt
0 Kudos
Reply