声明:本博文翻译自https://www.tutorialspoint.com/fortran/fortran_data_types.htm
fortran提供了五种内置数据类型。但是也可以派生自己的数据类型。
五种内置数据类型分别是:整型Integer;实型Real;复数型Complex;逻辑型Logical;字符型Character。
1. 整型:Integer type
整型变量用来存储整型数据。下面的代码显示对于不同kind值的整型变量。用huge函数显示该kind值对应的最大值。
Program testInt
implicit none
integer(kind=2) :: shortval
integer(kind=4) :: longval
integer(kind=8) :: verylongval
integer :: defval
write(*,*) huge(shortval)
write(*,*) huge(longval)
write(*,*) huge(verylongval)
write(*,*) huge(defval)
End program testInt
用ivf编译器,结果如下:
32767
2147483647
9223372036854775807
2147483647
2. 实型:Real type
实型变量用来存储浮点数,包括双精度与单精度浮点数。在ivf编译器中,有单精度,双精度,双双精度。
ivf编译器默认为单精度。
Program testReal
implicit none
real(kind=4) :: a = 3.12
real(kind=8) :: b = 3.32d0
real(kind=16) :: c = 3.32q0
real :: p, q, realRes
integer :: i, j, intRes
write(*,*) a
write(*,*) b
p = 2.0; q = 3.0
i = 2; j = 3
realRes = p / q
intRes = i / j
write(*,*) realRes
write(*,*) intRes
End program testReal
结果如下:
3.120000
3.32000000000000
3.32000000000000000000000000000000
0.6666667
0
3. 复数型:Complex type
complex(kind=8) :: a = (3,4) !.. 声明并赋值一个复数型变量
complex(kind=8) :: c !.. 声明
c = cmplx(3,4) 或 c = (3,4) !.. 赋值
这里不过多介绍。在后面的博客中将会有详细介绍。
4. 逻辑型:Complex type
逻辑型数据只分为.true.和.false.
5. 字符型:Character type
下面声明一个长度为50的字符变量。
character(len=50) :: str = "luk"
luk(1:2)返回"lu"
上述也可以写为character*50 :: str = "luk"
6. Implicit typing
建议在编程时使用implicit none语句。可以“屏蔽”掉IN规则。即所有变量都需要显式声明。
IN规则:如果不使用implicit none语句。fortran语言默认以i,j,k,l,m,n开头的变量均为整型。
这一期就讲到这里。后续会慢慢更新!
Fortran:数据类型
最新推荐文章于 2024-05-05 16:56:53 发布