Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fortran - setting kind/precision of a variable at run time

Is it possible to make a precision of a variable itself a variable that will be defined at a run time? Say, if I try to compile:

      SUBROUTINE FOO( VARIABLE, PRECISION_VALUE )

      IMPLICIT NONE

      INTEGER(4) :: PRECISION_VALUE
      INTEGER(PRECISION_VALUE) :: VARIABLE

      RETURN
      END

the compiler output is:

    error #6683: A kind type parameter must be a compile-time constant.   [PRECISION_VALUE]
          INTEGER(PRECISION_VALUE) :: VARIABLE
    --------------^
    compilation aborted for trial.f (code 1)

Anyway around it? I understand that not any arbitrary value can be used for KIND, but that's not my concern in this question.

like image 542
Puchatek Avatar asked May 10 '12 01:05

Puchatek


People also ask

How do you set precision in Fortran?

Obtaining the Kind ValueThe range is thus from 10-r to 10+r. For example, selected_real_kind (p = 10, r = 99) returns the kind value needed for a precision of 10 decimal places, and a range of at least 10-99 to 10+99.

How do you specify double precision in Fortran?

A double-precision exponent consists of the letter D , followed by an optional plus or minus sign, followed by an integer. A double-precision exponent denotes a power of 10. The value of a double-precision constant is the product of that power of 10 and the constant that precedes the D .

What is 0.0 d0 Fortran?

Note that a processor may assign an in itial value of zero (0.) to all double precision variables. In FORTRAN, the double-precision zero is written as 0.0D0, in which D stands for "times ten to the power...", i.e., 0.0 X 100. Likewise, 5.3D6 stands for 5.3 times ten to sixth power (that is, 5300000.00000000).

How do I use kind in Fortran?

The KIND of a variable is an integer label which tells the compiler which of its supported kinds it should use. Beware that although it is common for the KIND parameter to be the same as the number of bytes stored in a variable of that KIND, it is not required by the Fortran standard.


1 Answers

No, it's not possible, type, kind and rank have to be known. However, you can define generic subroutine interfaces with implementations for all the kinds that you expect to be passed into a routine at runtime.

like image 171
haraldkl Avatar answered Sep 28 '22 06:09

haraldkl