Fortran 简明教程

Fortran - Numeric Precision

我们已经讨论过,在较早版本的 Fortran 中,存在两种 real 类型:默认实数类型和 double precision 类型。

We have already discussed that, in older versions of Fortran, there were two real types: the default real type and double precision type.

然而,Fortran 90/95 通过 kind 规范提供了对实数和整数数据类型精度的更高控制。

However, Fortran 90/95 provides more control over the precision of real and integer data types through the kind specifie.

The Kind Attribute

不同种类的数字在计算机内部的存储方式不同。 kind 特性允许你指定数字在内部的存储方式。例如,

Different kind of numbers are stored differently inside the computer. The kind attribute allows you to specify how a number is stored internally. For example,

real, kind = 2 :: a, b, c
real, kind = 4 :: e, f, g
integer, kind = 2 :: i, j, k
integer, kind = 3 :: l, m, n

在以上声明中,实变量 e、f 和 g 比实变量 a、b 和 c 具有更高的精度。整数变量 l、m 和 n 可以存储比整数变量 i、j 和 k 更大的值,并且具有更多的存储位数。但具体情况取决于机器。

In the above declaration, the real variables e, f and g have more precision than the real variables a, b and c. The integer variables l, m and n, can store larger values and have more digits for storage than the integer variables i, j and k. Although this is machine dependent.

Example

program kindSpecifier
implicit none

   real(kind = 4) :: a, b, c
   real(kind = 8) :: e, f, g
   integer(kind = 2) :: i, j, k
   integer(kind = 4) :: l, m, n
   integer :: kind_a, kind_i, kind_e, kind_l

   kind_a = kind(a)
   kind_i = kind(i)
   kind_e = kind(e)
   kind_l = kind(l)

   print *,'default kind for real is', kind_a
   print *,'default kind for int is', kind_i
   print *,'extended kind for real is', kind_e
   print *,'default kind for int is', kind_l

end program kindSpecifier

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

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

default kind for real is 4
default kind for int is 2
extended kind for real is 8
default kind for int is 4

Inquiring the Size of Variables

有一些固有函数允许你询问数字的大小。

There are a number of intrinsic functions that allows you to interrogate the size of numbers.

例如, bit_size(i) 固有函数指定用于存储的位数。对于实数, precision(x) 固有函数返回精度的小数位数,而 range(x) 固有函数返回指数范围的小数部分。

For example, the bit_size(i) intrinsic function specifies the number of bits used for storage. For real numbers, the precision(x) intrinsic function, returns the number of decimal digits of precision, while the range(x) intrinsic function returns the decimal range of the exponent.

Example

program getSize
implicit none

   real (kind = 4) :: a
   real (kind = 8) :: b
   integer (kind = 2) :: i
   integer (kind = 4) :: j

   print *,'precision of real(4) =', precision(a)
   print *,'precision of real(8) =', precision(b)

   print *,'range of real(4) =', range(a)
   print *,'range of real(8) =', range(b)


   print *,'maximum exponent of real(4) =' , maxexponent(a)
   print *,'maximum exponent of real(8) =' , maxexponent(b)

   print *,'minimum exponent of real(4) =' , minexponent(a)
   print *,'minimum exponent of real(8) =' , minexponent(b)

   print *,'bits in integer(2) =' , bit_size(i)
   print *,'bits in integer(4) =' , bit_size(j)

end program getSize

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

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

precision of real(4) = 6
precision of real(8) = 15
range of real(4) = 37
range of real(8) = 307
maximum exponent of real(4) = 128
maximum exponent of real(8) = 1024
minimum exponent of real(4) = -125
minimum exponent of real(8) = -1021
bits in integer(2) = 16
bits in integer(4) = 32

Obtaining the Kind Value

Fortran 提供了另外两个固有函数,以获取整数和实数所需精度的类型值−

Fortran provides two more intrinsic functions to obtain the kind value for the required precision of integers and reals −

  1. selected_int_kind (r)

  2. selected_real_kind ([p, r])

selected_real_kind 函数返回一个整数,对于给定的十进制精度 p 和十进制指数范围 r,该整数是类型参数值。十进制精度是有意义位数,十进制指数范围指定最小的可表示数字和最大的可表示数字。因此,范围为 10-r 到 10+r。

The selected_real_kind function returns an integer that is the kind type parameter value necessary for a given decimal precision p and decimal exponent range r. The decimal precision is the number of significant digits, and the decimal exponent range specifies the smallest and largest representable number. The range is thus from 10-r to 10+r.

例如,selected_real_kind (p = 10, r = 99) 返回以精度为 10 个小数位、范围至少为 10-99 至 10+99 所需的类型值。

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.

Example

program getKind
implicit none

   integer:: i
   i = selected_real_kind (p = 10, r = 99)
   print *,'selected_real_kind (p = 10, r = 99)', i

end program getKind

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

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

selected_real_kind (p = 10, r = 99) 8