きったんの頭

HOME > 開発室

Fortran 90/95 メモ

| Top | データ型 | 演算子 | 制御 | 組込み手続き | リンク |

Hello World

hello.f90
program hello
  implicit none
  print *, 'Hello World!'
end program hello
実行 (gfortran)
> gfortran hello.f90 -o hello
> ./hello
Hello World!

コメントアウト

データ型

整数型integer7
実数型real7.0e0
倍精度実数型doubleprecision 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

属性

文字列

boolean

型変換

! 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

演算子

制御

関数

program func
  print *, inc(1)
contains
  integer function inc(n)
    integer,intent(in) :: n
    inc = n + 1
  end function inc
end program func

組込み手続き

時間

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

コンパイラ