we recently found a strange bug using the Intel Fortran compiler where a simple integer vector vec is initialized by some computed values.
These computed values are wrong if its computation involves an addition (+1) and a power (**2) AND on the syntax of the initialization of a second vector vec2: "vec2 = 1" or "vec2(:) = 1"
For better understanding, here we paste a working minimal code snippet that produces a wrong result:
- program main
- implicit none
- integer, parameter :: a = 2
- integer, parameter :: b = 1
- integer, dimension(4) :: vec = (/ a, b, a*(b+1), a*(b+1)**2 /)
- integer, dimension(3) :: vec2
- vec2(:) = 1
- write(*,*) "what to expect:", (/ a, b, a*(b+1), a*(b+1)**2 /)
- write(*,*) "what we got: ", vec
- ! output should be: 2 1 4 8
- write(*,*) vec2
- end program main
Here the expression a*(b+1)**2 in line 5 is miscalculated. Obviously it should be 2*(1+1)**2=8. But the result is 2.
The mysterious thing is if we change line 7 to vec2 = 1 the correct result comes out.
If we compare the assembly files between these 2 versions of the code the only difference is the precomputed value of the 4th element of vec!
We cannot see any other reason for this strange behaviour despite it being a BUG.
We had reproduced this bug on 2 different PCs with 2 different version of ifort. With gfortran both versions produce the same, correct result.
Can anyone of you reproduce this? Both versions are attached to this post.