Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
588 views
in Technique[技术] by (71.8m points)

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

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 number of bits of precision used by a variable defined using this kind. I'm stuck with how to define variable RP below, though, since the function to convert the variables x, u and alpha (real(x,RP) e.g.) require that RP (the kind type) to be constant.

If I define RP to be a parameter, i.e. at the top write integer, parameter :: RP I have to initialize it immediately, and then I obviously can't change it because, well, it's a parameter, so this won't work.

Here's what I have:

program f1
  implicit none
  integer :: n,t  ! n used in selected_real_kind(n), t is precision (#bits)
  integer :: RP
  real :: x=1.5, u=1.0, alpha=1.0  ! will be reset using _RP below                                                                               

  print '(A2,A4,A4)', "n", "RP", "t"  ! header for values

  do n=-1,34
     RP = selected_real_kind(n)

     ! convert x,u,alpha to type RP.  These functions throw the error!                               
     x = real(x,RP)
     u = real(u,RP)
     alpha = real(alpha,RP)

     ! init precision test variables                                                                                                             
     x=1.5_RP
     u=1.0_RP
     alpha=1.0_RP

     ! reset precision value to zero before each test                                                                                            
     t = 0

     ! precision test                                                                                                                            
     do while(x>alpha)
        u = u/2.0_RP
        x = alpha+u
        t = t+1
     end do

     print '(I2 I4 I4)', n, RP, t
  end do

end program f1
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...