“%”是什么意思Fortran 语言中的意思是/做什么?

发布于 2024-12-14 22:02:17 字数 179 浏览 3 评论 0原文

我正在尝试阅读一些 Fortran 代码,但无法确定 % (百分号)的作用。

它是这样的:

   x = a%rho * g * (-g*a%sigma + m%gb * m%ca * (1.6 * a%rho+g))

它有什么作用?

I am trying to read some Fortran code, but can not determine what the % (percentage sign) does.

It is in a line like:

   x = a%rho * g * (-g*a%sigma + m%gb * m%ca * (1.6 * a%rho+g))

What does it do?

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

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

发布评论

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

评论(3

回忆那么伤 2024-12-21 22:02:17

在 Fortran 90 中,它们允许您创建类似于 C++ 中的结构。它基本上充当点 (.) 运算符。

来自http://www.lahey.com/lookat90.htm

结构(派生类型)

您可以使用以下方式对数据进行分组:派生类型。这使用户能够将内部类型(包括数组和指针)组合成新类型,可以使用百分号作为分隔符来访问其各个组件。 (派生类型在 VAX Fortran 中称为记录。)
!使用派生类型和模块的示例。

module pipedef
   type pipe                          ! Define new type 'pipe', which
     real diameter                    ! is made up of two reals, an
     real flowrate                    ! integer, and a character.
     integer length
     character(len=10) :: flowtype
   end type pipe
end module pipedef

program main
   use pipedef                ! Associate module pipedef with main.
   type(pipe) water1, gas1    ! Declare two variables of type 'pipe'.
   water1 = pipe(4.5,44.8,1200,"turbulent") ! Assign value to water1.
   gas1%diameter = 14.9                     ! Assign value to parts
   gas1%flowrate = 91.284                   ! of gas1.
   gas1%length = 2550
   gas1%flowtype = 'laminar'
   .
   .
   .
end program

In Fortran 90 they allow you to create structures like in C++. It basically acts as the dot (.) operator.

From http://www.lahey.com/lookat90.htm :

Structures (Derived Types)

You can group your data using derived types. This enables users to combine intrinsic types (including arrays and pointers) into new types whose individual components can be accessed using the percent sign as a delimiter. (Derived types are known as records in VAX Fortran.)
! Example using derived types and modules.

module pipedef
   type pipe                          ! Define new type 'pipe', which
     real diameter                    ! is made up of two reals, an
     real flowrate                    ! integer, and a character.
     integer length
     character(len=10) :: flowtype
   end type pipe
end module pipedef

program main
   use pipedef                ! Associate module pipedef with main.
   type(pipe) water1, gas1    ! Declare two variables of type 'pipe'.
   water1 = pipe(4.5,44.8,1200,"turbulent") ! Assign value to water1.
   gas1%diameter = 14.9                     ! Assign value to parts
   gas1%flowrate = 91.284                   ! of gas1.
   gas1%length = 2550
   gas1%flowtype = 'laminar'
   .
   .
   .
end program
云淡月浅 2024-12-21 22:02:17

% 作为令牌有许多密切相关的用途。随着 Fortran 的发展,这些用途的数量不断增加。

回到 Fortran 90,以及问题中看到的用法,% 用于访问派生类型的组件。考虑派生类型 a_t 以及该类型的对象 a

type a_t
  real rho, sigma
end type
type(a_t) a

a 的组件 rhosigma可以使用a%rhoa%sigma 访问。从问题中可以看出,这些组件可以在表达式中使用(例如 a%rho * g),也可以是赋值的左侧(a%rho =1.)。

派生类型的组件本身可能是派生类型的对象:

type b_t
  type(a_t) a
end type
type(b_t) b

因此在单个引用中可能会多次出现 %

b%a%rho = ...

这里,派生类型的组件 rho派生类型对象 a 本身是 b 的组件,是赋值的目标。人们可以在一个引用中看到相当可怕的 % 数量,但零件引用始终是从左到右解析的。

来到 Fortran 2003,人们就会看到 % 通过其他几种方式与派生类型相关:

  • 引用对象的绑定;
  • 查询参数化类型的参数。

考虑派生类型

type a_t(n)
  integer, len :: n=1
  real x(n)
 contains
  procedure f
end type
type(a_t(2)) a

对象a 有一个长度类型参数和一个类型绑定过程。在像绑定f这样的表达式中

x = a%f()

,派生类型对象被引用。

可以引用a的参数n,就像

print *, a%n, SIZE(a%x)

可以引用组件x一样多。

最后,从 Fortran 2008 开始,% 可用于访问复杂对象的实部和虚部:

complex x, y(3)
x%im = 1.
x%re = 0.
y = (2., 1.)
print *, y(2)%im+y(3)%re

% as a token has a number of closely related uses. As Fortran has developed those uses have increased in count.

Going back to Fortran 90, and the use seen in the question, % is used to access the components of a derived type. Consider the derived type a_t with object a of that type:

type a_t
  real rho, sigma
end type
type(a_t) a

The components rho and sigma of a may be accessed with a%rho and a%sigma. As can be seen in the question, these components may be used in expressions (such as a%rho * g) or they may be the left-hand side of an assignment (a%rho=1.).

A component of a derived type may itself be an object of derived type:

type b_t
  type(a_t) a
end type
type(b_t) b

and so there may be multiple appearances of % in a single reference:

b%a%rho = ...

Here, the component rho of the derived type object a, which is itself a component of b, is the target of assignment. One can see a quite horrifying count of %s in one reference, but the part references are always resolved from left to right.

Coming to Fortran 2003, one then sees % relating to derived types in a couple of other ways:

  • referencing a binding of an object;
  • inquiring of a parameterized type's parameters.

Consider the derived type

type a_t(n)
  integer, len :: n=1
  real x(n)
 contains
  procedure f
end type
type(a_t(2)) a

The object a has a single length-type parameter and a type-bound procedure. In an expression like

x = a%f()

the binding f of the derived type object is referenced.

The parameter n of a may be referenced as

print *, a%n, SIZE(a%x)

much as the component x may be referenced.

Finally, from Fortran 2008, % may be used to access the real and imaginary parts of a complex object:

complex x, y(3)
x%im = 1.
x%re = 0.
y = (2., 1.)
print *, y(2)%im+y(3)%re
数理化全能战士 2024-12-21 22:02:17

它是派生类型的部分标识符。看看这个。 http://www.lahey.com/lookat90.htm

It's a part identifier for a derived type. Check this out. http://www.lahey.com/lookat90.htm

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