Fortran-在运行时设置变量的种类/精度 [英] Fortran - setting kind/precision of a variable at run time

查看:71
本文介绍了Fortran-在运行时设置变量的种类/精度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使变量本身的精度成为将在运行时定义的变量?说,如果我尝试编译:

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

编译器输出为:

    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)

反正吗?我了解到 KIND 不能使用任何任意值,但这不是我在此问题中所担心的.

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

推荐答案

否,不可能,类型,种类和等级是必须知道的.但是,可以为期望在运行时传递给例程的所有类型的实现定义通用子例程接口.

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.

这篇关于Fortran-在运行时设置变量的种类/精度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆