https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118580
kargls at comcast dot net changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |kargls at comcast dot net
--- Comment #2 from kargls at comcast dot net ---
(In reply to federico from comment #0)
> Sample program:
>
> ```
> program complex_eye
> implicit none
> integer, parameter :: dp = kind(0.d0)
> integer, parameter :: sp = kind(0.0)
> integer, parameter :: k = 2
> integer :: i,j
> complex(sp), dimension(k,k) :: A, B
> real(dp) :: eye(k, k), mx, mxB
>
> do j = 1,k; do i=1, k
> eye(i,j) = merge(1,0,i==j)
> A(i,j) = merge(1,0,i==j)
> enddo
>
> B = A - eye
> mx = maxval(abs(A-eye))
> mxB = maxval(abs(B))
>
> if (mx>1.0e-6) then
> write(*, *) "maxval(abs(A - eye)) = ", mx ! Should be 0, it is 1.0
> write(*, *) "maxval(abs(B)) = ", mxB
> stop 1
> else
> stop 0
> endif
>
> end program complex_eye
> ```
>
> when `maxval` is run on a temporary expression, it returns a wrong value
> (often 1.0, but sometimes junk values). when the operation is saved in
> variable B first, it returns the correct value.
>
> In Compiler explorer, see https://godbolt.org/z/E6dWcEc9s :
> - fails on all versions of gfortran from 4.9.4 to 15.0
> - fails with any optimization levels, also -O0
>
> We found this issue after an update to the Fortran Standard Library, but the
> issue seems not related to the library itself.
>
> Thank you,
> Federico
% gfcx -o z a.f90 && ./z
a.f90:27:3:
27 | end program complex_eye
| 1
Error: Expecting END DO statement at (1)
f951: Error: Unexpected end of file in 'a.f90'
Seems to be complicated what to generate an identity matrix
do j = 1,k; do i=1, k
eye(i,j) = merge(1,0,i==j)
A(i,j) = merge(1,0,i==j)
enddo
eye = 0
do j = 1, k
eye(j,j) = 1
end do
a = eye