Fortran 简明教程

Fortran - Data Types

Fortran 提供五种内在数据类型,但是,您还可以自己导出数据类型。这五种内在类型有——

Fortran provides five intrinsic data types, however, you can derive your own data types as well. The five intrinsic types are −

  1. Integer type

  2. Real type

  3. Complex type

  4. Logical type

  5. Character type

Integer Type

整型只能保存整数值。下面的示例将提取在常规的 4 字节整型中能保存的最大值——

The integer types can hold only integer values. The following example extracts the largest value that can be held in a usual four byte integer −

program testingInt
implicit none

   integer :: largeval
   print *, huge(largeval)

end program testingInt

当您编译并执行以上程序时,将生成以下结果——

When you compile and execute the above program it produces the following result −

2147483647

请注意, huge() 函数可以给出特定整型数据类型中能保存的最大值。您也可以使用 kind 规范符指定字节数。如下示例所示——

Note that the huge() function gives the largest number that can be held by the specific integer data type. You can also specify the number of bytes using the kind specifier. The following example demonstrates this −

program testingInt
implicit none

   !two byte integer
   integer(kind = 2) :: shortval

   !four byte integer
   integer(kind = 4) :: longval

   !eight byte integer
   integer(kind = 8) :: verylongval

   !sixteen byte integer
   integer(kind = 16) :: veryverylongval

   !default integer
   integer :: defval

   print *, huge(shortval)
   print *, huge(longval)
   print *, huge(verylongval)
   print *, huge(veryverylongval)
   print *, huge(defval)

end program testingInt

当您编译和执行上述程序时,它将生成以下结果 −

When you compile and execute the above program, it produces the following result −

32767
2147483647
9223372036854775807
170141183460469231731687303715884105727
2147483647

Real Type

它存储浮点数,例如 2.0、3.1415、-100.876 等。

It stores the floating point numbers, such as 2.0, 3.1415, -100.876, etc.

传统上有两种不同的 real 类型:默认 real 类型和 double precision 类型。

Traditionally there are two different real types, the default real type and double precision type.

然而,Fortran 90/95 通过我们会在本章学习的关于数字部分的 kind 规范符对 real 和 integer 数据类型的精度提供了更多的控制。

However, Fortran 90/95 provides more control over the precision of real and integer data types through the kind specifier, which we will study in the chapter on Numbers.

以下示例展示了 real 数据类型的使用方法——

The following example shows the use of real data type −

program division
implicit none

   ! Define real variables
   real :: p, q, realRes

   ! Define integer variables
   integer :: i, j, intRes

   ! Assigning  values
   p = 2.0
   q = 3.0
   i = 2
   j = 3

   ! floating point division
   realRes = p/q
   intRes = i/j

   print *, realRes
   print *, intRes

end program division

当您编译并执行以上程序时,将生成以下结果——

When you compile and execute the above program it produces the following result −

0.666666687
0

Complex Type

它用于存储复数。一个复数有两部分,即实部和虚部。两个连续的数值存储单元存储这两部分。

This is used for storing complex numbers. A complex number has two parts, the real part and the imaginary part. Two consecutive numeric storage units store these two parts.

例如,复数 (3.0, -5.0) 等于 3.0 – 5.0i

For example, the complex number (3.0, -5.0) is equal to 3.0 – 5.0i

我们将在关于数字的章节中更详细地讨论复数类型。

We will discuss Complex types in more detail, in the Numbers chapter.

Logical Type

只有两个逻辑值: .true..false.

There are only two logical values: .true. and .false.

Character Type

字符类型存储字符和字符串。字符串的长度可以通过 len 规范符指定。如果未指定长度,该长度则为 1。

The character type stores characters and strings. The length of the string can be specified by len specifier. If no length is specified, it is 1.

For example,

For example,

character (len = 40) :: name
name = “Zara Ali”

表达式 name(1:4) 将给出子字符串“Zahra”。

The expression, name(1:4) would give the substring “Zara”.

Implicit Typing

较旧版本的 Fortran 允许使用称为隐式类型的功能,即,您无需在使用前声明变量。如果未声明变量,那么变量名称的第一个字母将确定其类型。

Older versions of Fortran allowed a feature called implicit typing, i.e., you do not have to declare the variables before use. If a variable is not declared, then the first letter of its name will determine its type.

从 i、j、k、l、m 或 n 开始的变量名,被认为是整数变量,其它是实数变量。但是,必须将所有变量声明出来,因为这是良好的编程习惯。为此,您用以下语句开始程序:

Variable names starting with i, j, k, l, m, or n, are considered to be for integer variable and others are real variables. However, you must declare all the variables as it is good programming practice. For that you start your program with the statement −

implicit none

此语句关闭隐式类型。

This statement turns off implicit typing.