Fortran 中的类型声明

发布于 2024-07-26 10:18:47 字数 591 浏览 7 评论 0原文

我认为下面的 Y 类型是 REAL。

      SUBROUTINE F(X, Y)
C        IMPLICIT NONE
        REAL :: X, Y
C        REAL :: A, B
C        REAL, PARAMETER :: C = 3.14E0
C        X = Y + 2 * SIN(Y)
      END

但这里它的类型是什么?

      SUBROUTINE F(X, Y)
C        IMPLICIT NONE
        REAL X, Y
C        REAL :: A, B
C        REAL, PARAMETER :: C = 3.14E0
C        X = Y + 2 * SIN(Y)
      END

由于该行开头的 REAL 标记,它是隐式键入还是 REAL? 我的意思是类型声明的范围是什么,只是到 ,还是一直到行尾?

:: 的目的是什么?

(抱歉,半夜在混乱的状态下处理解析器会让你害怕做出错误的假设,而当下的智慧告诉你去咨询有更多经验的其他人。)

I suppose the type of Y below is REAL.

      SUBROUTINE F(X, Y)
C        IMPLICIT NONE
        REAL :: X, Y
C        REAL :: A, B
C        REAL, PARAMETER :: C = 3.14E0
C        X = Y + 2 * SIN(Y)
      END

But what is it's type here?

      SUBROUTINE F(X, Y)
C        IMPLICIT NONE
        REAL X, Y
C        REAL :: A, B
C        REAL, PARAMETER :: C = 3.14E0
C        X = Y + 2 * SIN(Y)
      END

Is it implicitly typed or REAL because of the REAL token at beginning of that line? I mean what's the scope of a type declaration, just up to the , or all the way to the end of line?

And what purpose does :: serve?

(Sorry, working on a parser in a confused state at midnight makes you afraid of making wrong assumptions and wisdom of the moment says go consult others with more experience.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云柯 2024-08-02 10:18:47

范围到行尾:Y 是 REAL

至于“::”,它是 FORTRAN 90 中的新内容

scope to the end of the line: Y is REAL

As for "::", it's something new in FORTRAN 90.

凉墨 2024-08-02 10:18:47

您已声明IMPLICIT NONE,因此不会发生隐式类型。 您是正确的,Y 将是 REAL - 您可以在一行上声明多个变量。

:: 字符在 Fortran 90 声明中用于指定实际变量名称的类型和选项。 正如您所发现的,如果您将其省略,编译器通常会很高兴,但我发现包含它会使它更具可读性。

附带说明一下,这段代码看起来像是使用 FORTRAN 77 样式编写的 - 如果它是新代码,您可能需要使用 Fortran 90 样式语法编写代码,从注释到缩进(例如,您不必启动命令)在某些列中,有更多可用的内在函数,动态内存等)

You have declared IMPLICIT NONE, so there is no implicit typing going on. You're correct that Y will be REAL - you're allowed to declare multiple variables on one line.

The :: character is used in Fortran 90 declarations to specify the type and options from the actual variable name. As you found, the compiler is usually happy if you leave it out, but I find that it makes it much more readable to include it.

As a side note, it looks like this code is written using FORTRAN 77 style - if it's new code you may want write the code using Fortran 90-style syntax for everything from comments to indenting (e.g. you don't have to start your commands in certain columns, there are more intrinsic functions available, dynamic memory, etc.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文