According to gfortran on-line documentation, the MAXLOC intrinsic determines the location of the element in the array with the maximum value, but what is the definition of "location" in this instance? The on-line documentation for IBM's XL Fortran calls it "the subscript of the location", but what if the subscript doesn't start at 1?
PROGRAM BugTest
INTEGER :: I(-3:3) ! declare seven-element integer array
I = 0 ! initialize entire array to zero
I(-3) = 9 ! make first element have the largest value
WRITE (*,*) 'Maximum value in array ',MAXVAL(I)
WRITE (*,*) 'Location of maximum value ',MAXLOC(I,1)
END PROGRAM BugTest
The above program generates the output:
Maximum value in array 9
Location of maximum value 1
with both gfortran 7.1 and 10.2.
Yes, the largest value is in the first array element, but the subscript for that element is -3, so shouldn't MAXLOC return -3 for the "location"?
If the compiler is producing correct results, then the documentation really should clarify the definition of "location" for arrays with subscripts that do not start at 1.
According to gfortran on-line documentation, the MAXLOC intrinsic determines the location of the element in the array with the maximum value, but what is the definition of "location" in this instance? The on-line documentation for IBM's XL Fortran calls it "the subscript of the location", but what if the subscript doesn't start at 1?
PROGRAM BugTest
INTEGER :: I(-3:3) ! declare seven-element integer array
I = 0 ! initialize entire array to zero
I(-3) = 9 ! make first element have the largest value
WRITE (*,*) 'Maximum value in array ',MAXVAL(I)
WRITE (*,*) 'Location of maximum value ',MAXLOC(I,1)
END PROGRAM BugTest
The above program generates the output:
Maximum value in array 9
Location of maximum value 1
with both gfortran 7.1 and 10.2.
Yes, the largest value is in the first array element, but the subscript for that element is -3, so shouldn't MAXLOC return -3 for the "location"?
If the compiler is producing correct results, then the documentation really should clarify the definition of "location" for arrays with subscripts that do not start at 1.
On Saturday, September 25, 2021 at 5:32:18 AM UTC-5, Dave Tholen wrote:
According to gfortran on-line documentation, the MAXLOC intrinsic determines
the location of the element in the array with the maximum value, but what is
the definition of "location" in this instance? The on-line documentation for
IBM's XL Fortran calls it "the subscript of the location", but what if the subscript doesn't start at 1?
PROGRAM BugTest
INTEGER :: I(-3:3) ! declare seven-element integer array
I = 0 ! initialize entire array to zero
I(-3) = 9 ! make first element have the largest value
WRITE (*,*) 'Maximum value in array ',MAXVAL(I)
WRITE (*,*) 'Location of maximum value ',MAXLOC(I,1)
END PROGRAM BugTest
The above program generates the output:
Maximum value in array 9
Location of maximum value 1
with both gfortran 7.1 and 10.2.
Yes, the largest value is in the first array element, but the subscript for that element is -3, so shouldn't MAXLOC return -3 for the "location"?
If the compiler is producing correct results, then the documentation really should clarify the definition of "location" for arrays with subscripts that do not start at 1.The F2008 standard answers your question quite clearly in section 16.9.127 : "The i-th subscript returned lies in the range 1 to e_i, where e_i is the extent of the i-th dimension of ARRAY".
According to gfortran on-line documentation, the MAXLOC intrinsic determines the location of the element in the array with the maximum value, but what is the definition of "location" in this instance? The on-line documentation for IBM's XL Fortran calls it "the subscript of the location", but what if the subscript doesn't start at 1?
On Saturday, September 25, 2021 at 3:32:18 AM UTC-7, Dave Tholen wrote:
According to gfortran on-line documentation, the MAXLOC intrinsic determines >> the location of the element in the array with the maximum value, but what is >> the definition of "location" in this instance? The on-line documentation for >> IBM's XL Fortran calls it "the subscript of the location", but what if the >> subscript doesn't start at 1?
I have commented on the numbering system before, but note that array expressions always start at 1.
A dummy array that is not POINTER or ALLOCATABLE doesn't get the lower
bound from the caller. Since the argument of MAXLOC isn't POINTER or ALLOCATABLE, then it won't get the lower bound.
But overall, the standard does not make this very easy to follow, finding
the exact cases where arrays lose the lower bound.
hat gfortran does is correct. The standard is a bit more expansive on
that, it states (among other cases)
# The ith subscript returned lies in the range 1 to ei, where ei is
# the extent of the ith dimension of ARRAY. If ARRAY has size zero,
# all elements of the result are zero.
If the compiler is producing correct results, then the documentation really >> should clarify the definition of "location" for arrays with subscripts that >> do not start at 1.gfortran uses the terminology of the standard, but the documentation certainly could be improved. If you have a suggestion, please send it tofortran@gcc.gnu.org.
(Documentation for MINLOC and FINDLOC would then also be adjusted)
The "pseudo-subscript" notation used in the standard for functions such
as MAXLOC has been a source of confusion for many. I struggled with it myself, and worked with Malcolm Cohen to improve the wording for the
next revision. See https://j3-fortran.org/doc/year/21/21-143r1.txt for
the details.
Exactly when a dummy array keeps the lower bound of the actual array
and when it doesn't has come up at least a few times here.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 159 |
Nodes: | 16 (0 / 16) |
Uptime: | 98:34:34 |
Calls: | 3,209 |
Files: | 10,563 |
Messages: | 3,009,579 |