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

前端 未结 3 2016
灰色年华
灰色年华 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 22:42

    Similar to george's answer, but in Fortran. We note that kinds are something that, essentially, must be known at compile-time. For that we ask the compiler which kinds it can offer us and, for each of those, create a small chunk of code to be compiled. Then compile and run it. [This last step certainly varies by system.]

    ! Rather than looping over a range with SELECTED_REAL_KIND, just see which
    ! real kinds are available to the compiler.  We can later match up real kinds
    ! for a requested precision with those here.
      use, intrinsic :: iso_fortran_env, only : real_kinds
      implicit none
    
      integer output
      integer i
    
      open(newunit=output, file='gosh.f90', action='write', position='rewind')
    
    ! Here we could instead do n=-1, 34 etc.
      do i=1, SIZE(real_kinds)
         write(output, 1) real_kinds(i)
      end do
    
      write(output, '("end")')
      close(output)
    
    ! A compile/execute line - do whatever is required.
      call execute_command_line('compile gosh.f90 && ./a.out')
    
    1 FORMAT ("block",/,"real(",I0,") x",/,"include 'domystuffz'"/,"end block")
    
    end
    

    where "domystuffz" is a file containing whatever analysis is wanted. As with High Performance Mark's answer something intrinsic would be nice and the include could be replaced by some simple code if required.

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2021-01-21 23:03

    Fortran provides a number of intrinsic functions for enquiring into the characteristics of the numbers it processes. Consult your documentation for functions such as digits, precision and radix. All of these (including the ones I haven't mentioned but which your documentation will list) are generic (enough) to work on inputs of all the numeric kinds supported by your compiler.

    You'll make headway faster using these functions than trying to roll your own. As you are discovering it's not a simple matter to write your own generic routines as Fortran wants kind selectors known (or knowable) at compile time. You'll also extend your knowledge of Fortran.

    You may also find functions of interest in the intrinsic module IEEE_ARITHMETIC.

    0 讨论(0)
提交回复
热议问题