[fortran90] have problem in real array index

Click For Summary

Discussion Overview

The discussion revolves around a problem related to array indexing in Fortran 90, specifically concerning the use of real numbers as indices in arrays. Participants explore the implications of this issue and seek solutions to eliminate warnings generated by the compiler.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a warning encountered when using a real number as an array index, specifically in the expression involving "int(dpp) - 0.5".
  • Another participant suggests modifying the expression to "int(dpp - 0.5)" to avoid the warning, emphasizing the order of operations in type conversion.
  • A third participant clarifies that while Fortran allows real arguments, the final index must be an integer, and highlights the difference between a warning and an error in this context.
  • A later reply confirms that the suggested change resolved the warning, indicating a successful application of the advice given.

Areas of Agreement / Disagreement

Participants generally agree on the nature of the problem and the proposed solution, with one participant confirming the effectiveness of the advice given. However, the discussion does not reach a consensus on the broader implications of using real indices in array operations.

Contextual Notes

The discussion does not address potential limitations or assumptions regarding the use of real numbers in array indexing beyond the immediate warning encountered.

Who May Find This Useful

Readers interested in Fortran programming, particularly those dealing with array operations and type conversions, may find this discussion relevant.

s_hy
Messages
57
Reaction score
0
hi all...i have problem in understanding in array index. my codes are as below

Code:
subroutine  tmz2d
implicit none


double precision                   :: phi
double precision                   :: cosphi,sinphi
double precision                   :: d,dp,dpp,d1,d2
integer                            :: i,j,m,h
integer                            :: minit,mlast
integer                            :: l,p
integer                            :: iinit,ilast,hlast,pinit,plast
integer                            :: n
integer                            :: linit,llast
integer                            :: jinit,jlast
double precision,dimension(200)    :: Hm,Em
double precision                   :: Einc,Hinc
double precision,dimension(200,200):: Ezinc,Hxinc,Hyinc
double precision, parameter        :: pi = 3.14159265
character(len=20)                  :: filename
 

 minit = 1
 mlast = 100
 linit = 1
 llast = 100
 pinit = 1
 plast = 100

 iinit = 1 
 ilast = 100
 jinit = 1
 jlast = 100


 phi = pi/4.0
 cosphi = cos(phi)
 sinphi = sin(phi)
 print *, 'cosphi=',cosphi,'sinphi=',sinphi


do n = 1,100
   
  write (filename, "('data',I3.3,'.dat')") n
  open (unit=130,file=filename)


 call bg1d
 !print *, 'see me2'

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
      if (Mod(m,2)/=0) then
	if (Mod(p,2)/=0) then
         d = cosphi*(m - minit) + sinphi*(p - pinit)
         dp = d - int(d)
         Einc = (1-dp)*Em(int(d))+dp*(Em(int(d)+1))
         Ezinc(m,p+2) = Einc
         write(130,*) Ezinc(m,p+2)
        end if
      end if
    end do
  end do

  do m = 2,2*ilast-1
    do p = 2,2*jlast-1
      if (Mod(m,2) == 0) then
	if (Mod(p,2) == 0) then
         d = cosphi*(m - minit) + sinphi*(p - pinit)
         dpp = d+1
         dp = dpp-int(d)
           Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
           Hxinc(m,p+1) = sinphi*Hinc
           Hyinc(m-1,p+2) = -cosphi*Hinc
        end if
      end if
    end do
  end do

end do !n
print *, 'see me3'


end subroutine tmz2d

and i got this warning when execute

subroutines.f90:167.48:

Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
1
Warning: Extension: REAL array index at (1)
subroutines.f90:167.28:

Hinc = (1-dp)*Hm(int(dpp)-0.5)+dp*Hm(0.5+int(dpp))
1
Warning: Extension: REAL array index at (1)


###########################################

the problem come from line 167 and i found out that fortran do not allow real index...(int(dpp) -0.5)..can anyone give me an advice how to solve this problem.

thank you in advance
 
Technology news on Phys.org
The obvious answer (which should remove the warning, but doesn't have to be correct for your problem) is to use int(dpp-0.5) - right now you are converting dpp to an int, then subtracting (or adding) 0.5 converting it back to a real. Do the subtraction first, conversion to an int later.
 
O.k., you found out that Fortran does not allow "real" index; more precisely, it allows a "real argument" but the final index needs to be integer...to that end, it allows a "real argument" which gets automatically typecast into an integer. If "real argument" wasn't allowed, you would get a compiler "error" instead of a "warning".

Say, is there a chest of drawers in your room? Does it have drawer 1.37?...It's the same with arrays.
 
thanks Borek, its worked and removed the warning

thanks gsal for the explanation..i am really new in fortran90
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 8 ·
Replies
8
Views
5K