- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
My colleagues and I were a bit surprised by the results of the MAXLOC function in the following example:
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
[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
Link Copied
4 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
By the way, it's more confusing when you omit the DIM argument in a context which seems to call for it.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
-Kurt
Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page