きったんの頭ん中☆
Fortran 90/95 メモ
Hello World
hello.f90
program hello
implicit none
print *, 'Hello World!'
end program hello
実行 (gfortran)
> gfortran hello.f90 -o hello
> ./hello
Hello World!
コメントアウト
データ型
整数型 | integer | 7 |
実数型 | real | 7.0e0 |
倍精度実数型 | double | precision 7.0d0 |
複素数型 | complex | (7.0, 3.0) |
倍精度複素数型 | complex(kind(0d0)) | (7.0d0, 3.0d0) |
論理型 | logical | .true. |
文字型 | character | 'nm7' |
real,parameter :: pi = 3.14
complex :: c = (7.0, 3.0)
character(len=7) :: n = '7mizuki'
data a,b/1,2/ c,d/2*0/ ! a=1, b=2, c=0, d=0
属性
- dimension 形状指定
- parameter 定数指定
- allocatable 動的領域確保
- optional 省略可能な引数
- intent(opt) 授受特性 opt: in, out, inout
文字列
- character a ! 長さ1
- character(len=7) b ! 長さ7
- character(7) c
- character*7 d
- character e*7, f*3
boolean
型変換
- int() ! real → int 切り捨て
- nint() ! real → int 四捨五入
- real() ! int → real
- dble() ! int → double precision
! int ⇒ character
character(3) a
integer :: i = 123
write (a,*) i
! character
character(3) a
integer i
a = "123"
read (a,*) i
配列
integer,dimension(7) :: a = 3
! a(1) = 3, a(2) = 3, ..., a(7) = 3
integer a(7)
data a/7*3/
! a(1) = 3, a(2) = 3, ..., a(7) = 3
integer :: b(3) = (/1, 2, 3/)
integer :: c(3) = (/(i*7, i=1,3)/) ! 1, 14, 21
integer :: d(2,3) = reshape((/i, i=1,6/), (/2,3/))
! 1 3 5
! 2 4 6
! 部分配列
integer e(7)
e(:) = 0
! 0 0 0 0 0 0 0
d(1:7:2) = 7
! 7 0 7 0 7 0 7
d(2:4) = 3
! 7 3 3 3 7 0 7
d(7:1:-1)
! 7 0 7 3 3 3 7
演算子
- + - * / ** //
- < > == /= <= >=
.lt. .gt. .eq. .ne. .gt. .ge.
- .not. .and. .or. .eqv. .neqv.
制御
- continue
- cycle
- do
- exit
- go to
- if then [else if [else]]
- return
- select case [case default]
- stop
関数
program func
print *, inc(1)
contains
integer function inc(n)
integer,intent(in) :: n
inc = n + 1
end function inc
end program func
組込み手続き
- sum
- minval
- maxval
- lbound
- ubound
- allocate
- deallocate
- len
- trim
- trim_len
時間
integer t1, t2, t_rate, t_max, diff
call system_clock(t1)
! do something
call system_clock(t2, t_rate, t_max)
if ( t2 < t1 ) then
diff = t_max - t1 + t2
else
diff = t2 - t1
endif
print *, diff/dble(t_rate)
real t1, t2
call cpu_time( t1 )
! do something
call cpu_time( t2 )
print *, t2 - t1
リンク
コンパイラ