Defining a variable that must be declared constant, but changes in a loop

前端 未结 3 2059
灰色年华
灰色年华 2021-01-21 22:36

I\'m testing out ranges of values (-1:34 just for kicks) for the function selected_real_kind to determine the kind parameter it returns and the actual

3条回答
  •  暖寄归人
    2021-01-21 23:02

    elaborating on my comment, here is a python example:

     fortrancode = """
           implicit none
           integer, parameter :: n=%i
           integer,parameter :: rp=selected_real_kind(n)      
           write(*,*)n,rp
           end
     """
     from subprocess import call
     for n in range(-1,33):
      f=open('test.f','w')
      f.write(fortrancode%(n))  ! <- n here gets string substituted
                                !for the '%i' in fortrancode
      f.close()
       ! optional check  call(['grep','n=','test.f'])
      call(['gfortran','test.f','-o','test.exe'])
      call(['./test.exe'])
    

    python test.py

     -1 4
      0 4
      1 4
    ...
      7 8
    ...
     18 10
     19 -1
    ...
     32 -1
    

提交回复
热议问题