Reputation: 3451
I have an issue that hopefully would not affect my numerical calculations in Fortran 90.
The thing is that I have an array declared as
Real(r8), Allocatable :: matKBody(:)
By my hand, initializing such array I done it like
allocate(matKBody(1:Nk), STAT=ierr)
If (ierr /= 0) Stop
matKBody(:) = ( 0.0_r8, 0.0_r8)
which is a way to initialize a complex array. I noticed this error but I was surprised that it doesn't matter if I initialize such array in this way or using the 'correct' statement:
matKBody(:) = 0.0_r8
...
... ! Do some stuff with the array
...
Deallocate( matKBody, STAT=ierr )
If (ierr /= 0) Stop
If I print both arrays they give me the correct initialization , i.e. both initialize real numbers.
Why fortran (or the compiler) is not aware of such kind of things? (I used ifort for compilation).
Upvotes: 0
Views: 319
Reputation: 29391
Full example:
program test_convert
use, intrinsic :: ISO_FORTRAN_ENV
Real(real64), Allocatable :: matKBody(:)
allocate (matKBody (1:10) )
matKBody(:) = ( 0.0_real64, 0.0_real64)
end program test_convert
Compiled with gfortran with options: -O2 -fimplicit-none -Wall -Wline-truncation -Wcharacter-truncation -Wsurprising -Waliasing -Wimplicit-interface -Wunused-parameter -fcheck=all -std=f2008 -pedantic -fbacktrace
Output from gfortran:
matKBody(:) = ( 0.0_real64, 0.0_real64)
1
Warning: Possible change of value in conversion from COMPLEX(8) to REAL(8) at (1)
So some compiler are "aware" of such things and will tell you about them if you request it to.
As discussed in the comments, Fortran provides automatic conversion between types upon assignment. So this is not an error, but gfortran at least will provide a warning so that the programmer can check whether they intended the conversion. You can suppress the warning and state your intent to cause a conversion via:
matKBody(:) = real ( ( 0.0_real64, 0.0_real64), real64 )
(This is just an example, since writing = 0.0_real64
is so much simpler for this particular assignment.)
Upvotes: 1