Part VIII – more F90/95

PartVIII– moreF90/95
Ø
DoublePrecision
Ø
DOWHILE
Ø
SELECTCASE
(Pages62-63inLabManual)
DOUBLEPRECISION
Realorfloatingpointdatainscientificnotationofbase2system:
value=mantissax2exponent
precision
range
DOUBLEPRECISION
Realorfloatingpointdatainscientificnotationofbase2system:
value=mantissax2exponent
precision
range
Single-precision: arealnumberoccupies32bits(4bytes)ofmemory:
24bitsmantissa:+/-223=8,388,608,i.e.about6or7significantdigits
8bitsexponent:+/-27 =128,2128=3.4x1038,2-128=2.9x10-39
i.e.,range10-38 to1038
DOUBLEPRECISION
Single-precision: arealnumberoccupies32bits(4bytes)ofmemory:
24bitsmantissa:+/-223=8,388,608,i.e.about6or7significantdigits
8bitsexponent:+/-27 =128,2128=3.4x1038,2-128=2.9x10-39
i.e.,range10-38 to1038
Example:12345678.9willberound-offto12345680.0forsingle-precisionrealdata
(7significantdigits,round-offerror=1.1)
DOUBLEPRECISION
Single-precision: arealnumberoccupies32bits(4bytes)ofmemory:
24bitsmantissa:+/-223=8,388,608,i.e.about6or7significantdigits
8bitsexponent:+/-27 =128,2128=3.4x1038,2-128=2.9x10-39
i.e.,range10-38 to1038
Example:12345678.9willberound-offto12345680.0forsingle-precisionrealdata
(7significantdigits,round-offerror=1.1)
Double-precision: arealnumberoccupies64bits(8bytes)ofmemory:
53bitsmantissa:14or15significantdigits
11bitsexponent: rangeof10-308 to10308
DOUBLEPRECISION
FORTRAN77
REAL * 8
! Fortran 77
Double precision
REAL * 12
! Fortran 77
Tripel precisioin
REAL * 16
! Fortran 77
Quad precision
DOUBLEPRECISION
Doublingthestandardaccuracy:
DOUBLE PRECISION, PARAMETER :: pi = 3.14159265358979D00
DOUBLE PRECISION, REAL :: Msun_in_MeV = 1.115829D60
DOUBLEPRECISION
Doublingthestandardaccuracy:
DOUBLE PRECISION, PARAMETER :: pi = 3.14159265358979D00
DOUBLE PRECISION, REAL :: Msun_in_MeV = 1.115829D60
Usekind typeparametertodeclarethedatatype:
INTEGER, PARAMETER :: single = 4
INTEGER, PARAMETER :: double = 8
REAL (kind=double), PARAMETER :: pi = 3.14159265358979D00
REAL (kind=double) :: Msun_in_MeV = 1.115829D60
REAL (kind=single) :: var1, var2
REAL (kind=double) :: var3, var4
DOUBLEPRECISION
Usekind typeparametertodeclarethedatatype:
INTEGER, PARAMETER :: single = 4
INTEGER, PARAMETER :: double = 8
REAL (kind=double), PARAMETER :: pi = 3.14159265358979D00
REAL (kind=double) :: Msun_in_MeV = 1.115829D60
REAL (kind=single) :: var1, var2
REAL (kind=double) :: var3, var4
Useselected_real_kind intrinsicfunctiontoselecttypeofrealvalue:
INTEGER, PARAMETER
:: precision1 = selected_real_kind(10)
INTEGER, PARAMETER
:: precision2 = selected_real_kind(20)
REAL (kind=precision1), PARAMETER :: pi = 3.14159265358979D00
REAL (kind=precision2) :: Msun_in_MeV = 1.115829D60
REAL (kind=precision1) :: var1, var2
REAL (kind=precision2) :: var3, var4
DOUBLEPRECISION
UsingdoubleprecisioninFUNCTIONsub-programs
Singleprecisioncase
Doubleprecisioncase
real function(x)
implicit none
real, intent(in) :: x
double precision function(x)
implicit none
double precision, intent(in) :: x
DOUBLEPRECISION
UsingdoubleprecisioninFUNCTIONsandSUBROUTINEs
PROGRAM ALPHA
implicit none
! Using real kind: (use decimal precision of at least 20 digits)
integer, parameter :: p=selected_real_kind(20)
real (kind=p) :: arg1, agr2, TF
FUNCTION TF(arg1, arg2)
implicit none
integer, parameter :: p=selected_real_kind(20)
real (kind=p) :: arg1, agr2, TF
Double-PrecisionIntrinsicFunctions
Single precision
Double precision
ABS(X)
DABS(X)
COS(X)
DCOS(X)
EXP(X)
DEXP(X)
ALOG(X)
DALOG(X)
ALOG10(X)
DALOG10(X)
SQRT(X)
DSQRT(X)
DOWHILEstatement
InasenseacombinationofIF andtheDO loop
DO WHILE ([logical expression])
[code]
END DO
Theentireloopwillbeexecutedsolongasthelogical
expressionistrue.
Example
i = 2
ifact = 1
do while (i <= 10)
ifact=ifact*i
i = i + 1
end do
print *, i-1,' factorial = ', ifact
Example:Youmaygiveaname toaDOWHILEblock
i = 2
ifact = 1
factorial: do while (i <= 10)
ifact=ifact*i
i = i + 1
end do factorial
print *, i-1,' factorial = ', ifact
Example:Endyourloopata labeledstatement
i = 2
ifact = 1
do 100 while (i <= 10)
ifact=ifact*i
100 i = i + 1
print *, i-1,' factorial = ', ifact
Example:Endyourloopata labeledstatement
i = 2
ifact = 1
do 100 while (i <= 10)
ifact=ifact*i
i = i + 1
100 end do
print *, i-1,' factorial = ', ifact
The SELECTCASE construct…testformorethanone
condition
Insteadofusing
if ( … ) then
else if ( …) then
else
end if
tree
The SELECTCASE construct…testformorethanone
condition
Syntax
SELECT CASE (<integer variable>)
CASE (this value)
Code to execute if <variable> = this value
CASE (that value)
Code to execute if <variable> = that value
.
.
.
CASE DEFAULT
Code to execute if <variable> does not equal the
value following any of the cases
END SELECT
&
Example1
SELECT CASE (IVAR)
CASE (:-1)
WRITE (*,*) 'Negative number'
CASE (0)
WRITE (*,*) ' Zero'
CASE (1:9)
WRITE (*,*) ' Digit ', IVAR
CASE (10:99)
WRITE (*,*) ' Number ', IVAR
CASE DEFAULT
WRITE (*,*) ' Number too big’
END SELECT
!
all negative numbers
!
zero case
!
one-digit number
!
two-digit number
!
all remaining cases
Example1NamedSELECTCASE
name: SELECT CASE (IVAR)
CASE (:-1)
WRITE (*,*) 'Negative number'
CASE (0)
WRITE (*,*) ' Zero'
CASE (1:9)
WRITE (*,*) ' Digit ', IVAR
CASE (10:99)
WRITE (*,*) ' Number ', IVAR
CASE DEFAULT
WRITE (*,*) ' Number too big’
END SELECT name
!
all negative numbers
!
zero case
!
one-digit number
!
two-digit number
!
all remaining cases
Example1
SELECT CASE (IVAR)
CASE (:-1)
WRITE (*,*) 'Negative number'
CASE (0)
WRITE (*,*) ' Zero'
CASE (1:9)
WRITE (*,*) ' Digit ', IVAR
CASE (10:99)
WRITE (*,*) ' Number ', IVAR
CASE DEFAULT
WRITE (*,*) ' Number too big’
END SELECT
!
all negative numbers
!
zero case
!
one-digit number
!
two-digit number
!
all remaining cases
Example2
SELECT CASE (N)
CASE(:-1)
! Case 1
print*, 'case 1'
CASE(0)
! Case 2
print*, 'case 2'
CASE(3,5,7,11,13)
! Case 3
print*, 'case 3'
CASE DEFAULT
! all remaining cases
WRITE (*,*) ' N out of range'
END SELECT
Example1
SELECT CASE (IVAR)
CASE (:-1)
WRITE (*,*) 'Negative number'
CASE (0)
WRITE (*,*) ' Zero'
CASE (1:9)
WRITE (*,*) ' Digit ', IVAR
CASE (10:99)
WRITE (*,*) ' Number ', IVAR
CASE DEFAULT
WRITE (*,*) ' Number too big’
END SELECT
!
all negative numbers
!
zero case
!
one-digit number
!
two-digit number
!
all remaining cases
Example2
Example3
SELECT CASE (N)
CASE(:-1)
INTEGER FUNCTION SIGNUM(N)
! Case 1
print*, 'case 1'
CASE(0)
! Case 2
print*, 'case 2'
CASE(3,5,7,11,13)
! Case 3
print*, 'case 3'
CASE DEFAULT
! all remaining cases
WRITE (*,*) ' N out of range'
END SELECT
SELECT CASE (N)
CASE (:-1)
SIGNUM = -1
CASE (0)
SIGNUM = 0
CASE (1:)
SIGNUM = 1
END SELECT
EndofPartVIII