Fortran數位

Fortran的數位由三個內部資料型別來表示：

• 整型
• 實型
• 複雜型別

整型

program testingInt
implicit none

integer :: largeval
print *, huge(largeval)

end program testingInt

2147483647

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

32767
2147483647
9223372036854775807
170141183460469231731687303715884105727
2147483647

實型

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

0.666666687
0

複雜型別

program createComplex
implicit none

integer :: i = 10
real :: x = 5.17
print *, cmplx(i, x)

end program createComplex

(10.0000000, 5.17000008)

program ComplexArithmatic
implicit none

complex, parameter :: i = (0, 1)   ! sqrt(-1)
complex :: x, y, z

x = (7, 8);
y = (5, -7)
write(*,*) i * x * y

z = x + y
print *, "z = x + y = ", z

z = x - y
print *, "z = x - y = ", z

z = x * y
print *, "z = x * y = ", z

z = x / y
print *, "z = x / y = ", z

end program ComplexArithmatic

(9.00000000, 91.0000000)
z = x + y = (12.0000000, 1.00000000)
z = x - y = (2.00000000, 15.0000000)
z = x * y = (91.0000000, -9.00000000)
z = x / y = (-0.283783793, 1.20270276)

範圍，精度和數位的大小

64 9,223,372,036,854,774,807 (2**63)–1
32 2,147,483,647 (2**31)–1

64 0.8E+308 0.5E–308 15–18
32 1.7E+38 0.3E–38 6-9

program rangePrecision
implicit none

real:: x, y, z
x = 1.5e+40
y = 3.73e+40
z = x * y
print *, z

end program rangePrecision

x = 1.5e+40
1
Error : Real constant overflows its kind at (1)
main.f95:5.12:

y = 3.73e+40
1
Error : Real constant overflows its kind at (1)

program rangePrecision
implicit none

real:: x, y, z
x = 1.5e+20
y = 3.73e+20
z = x * y
print *, z

z = x/y
print *, z

end program rangePrecision

Infinity
0.402144760

program rangePrecision
implicit none

real:: x, y, z
x = 1.5e-30
y = 3.73e-60
z = x * y
print *, z

z = x/y
print *, z

end program rangePrecision

y = 3.73e-60
1
Warning : Real constant underflows its kind at (1)

Executing the program....
\$demo

0.00000000E+00
Infinity

Kind說明符

program kindCheck
implicit none

integer :: i
real :: r
complex :: cp
print *,' Integer ', kind(i)
print *,' Real ', kind(r)
print *,' Complex ', kind(cp)

end program kindCheck

Integer 4
Real 4
Complex 4

program checkKind
implicit none

integer :: i
real :: r
character*1 :: c
logical :: lg
complex :: cp

print *,' Integer ', kind(i)
print *,' Real ', kind(r)
print *,' Complex ', kind(cp)
print *,' Character ', kind(c)
print *,' Logical ', kind(lg)

end program checkKind

Integer 4
Real 4
Complex 4
Character 1
Logical 4