Docsity

Fortran Real Numbers: Declaration, Data Types, and Conversion - Prof. Dana G. Haugli, Study notes of Aerospace Engineering

Toggle sidebar
bg1
D. Haugli Aer E 160 Aerospace Engineering
2/4/2005 Real Numbers, Page 1 Iowa State University
Real Numbers
Any number with a decimal point (sometimes called a "floating point") would be considered a
REAL number. Several types of REAL numbers are available.
REAL or REAL (kind = 4). Numbers of this type require 4 bytes to store and process, and can
contain up to seven digits (plus the location of the decimal). For example,
REAL :: number
number = 1.234567
An alternate declaration would be,
REAL (kind = 4) :: number
As with other declarations, list variables one after the other separated by commas, and use
multiple REAL statements as needed:
REAL :: a, b, c
REAL :: sum
Variables of this type can be initialized with either a decimal or a power of 10 specification:
a=1.23 and a=1.23e0 (which means 0
1023.1 ×) are equivalent; so would be a=0.123e1 and
a=12.3e-1.
DOUBLE PRECISION or REAL( kind = 8). DOUBLE PRECISION numbers are floating point numbers
that occupy 8 bytes instead of 4 and can have up to fifteen digits (plus the location of the
decimal). The value of
π
in DOUBLE PRECISION would be:
pi = 3.14159265358979
which could be declared either of the following ways:
DOUBLE PRECISION :: pi
REAL (kind = 8) :: pi
Compilers typically assume numbers are REAL, so if pi is declared as double precision, the
statement,
pi = 4.*ATAN(1.)
would only compute the first 7 digits of pi because 4., 1. and ATAN are all single precision.
(The remaining 8 digits in the double precision number would be random junk.) Instead, use,
pi = 4.d0*DATAN(1.d0)
where d is the double-precision power of 10, and DATAN is a double precision inverse tangent
function.
pf2

Partial preview of the text

Download Fortran Real Numbers: Declaration, Data Types, and Conversion - Prof. Dana G. Haugli and more Study notes Aerospace Engineering in PDF only on Docsity!

D. Haugli Aer E 160 Aerospace Engineering 2/4/2005 Real Numbers, Page 1 Iowa State University

Real Numbers

Any number with a decimal point (sometimes called a "floating point") would be considered a REAL number. Several types of REAL numbers are available.

REAL or REAL (kind = 4). Numbers of this type require 4 bytes to store and process, and can contain up to seven digits (plus the location of the decimal). For example,

REAL :: number number = 1.

An alternate declaration would be,

REAL (kind = 4) :: number

As with other declarations, list variables one after the other separated by commas, and use multiple REAL statements as needed:

REAL :: a, b, c REAL :: sum

Variables of this type can be initialized with either a decimal or a power of 10 specification:

a=1.23 and a=1.23e0 (which means 1. 23 × 10 0 ) are equivalent; so would be a=0.123e1 and a=12.3e-1.

DOUBLE PRECISION or REAL( kind = 8). DOUBLE PRECISION numbers are floating point numbers that occupy 8 bytes instead of 4 and can have up to fifteen digits (plus the location of the

decimal). The value of π in DOUBLE PRECISION would be:

pi = 3.

which could be declared either of the following ways:

DOUBLE PRECISION :: pi REAL (kind = 8) :: pi

Compilers typically assume numbers are REAL, so if pi is declared as double precision, the statement,

pi = 4.*ATAN(1.)

would only compute the first 7 digits of pi because 4., 1. and ATAN are all single precision. (The remaining 8 digits in the double precision number would be random junk.) Instead, use,

pi = 4.d0*DATAN(1.d0)

where d is the double-precision power of 10, and DATAN is a double precision inverse tangent function.

D. Haugli Aer E 160 Aerospace Engineering 2/4/2005 Real Numbers, Page 2 Iowa State University

Converting Between Data Types. Intrinsic functions are built into FORTRAN that enable programmers to convert data from one type to another. These functions are very useful in helping create consistent equations (equations which are not mixed mode).

To truncate a real number to an integer, use the intrinsic function INT. INT truncates digits to the right of the decimal. Use the REAL intrinsic function to convert an integer to a real number.

Example 1. If the REAL variable a = 1.234567, then INT(a) would return the integer 1. REAL(1) would yield 1.000000 (to seven digits).

Use the INT intrinsic function to convert a DOUBLE PRECISION number to an integer, the REAL intrinsic function to convert a DOUBLE PRECISION number to seven digits, and the DBLE intrinsic function to convert INTEGER or REAL numbers to double precision.

Example 2. If the DOUBLE PRECISION variable pi = 3.14159265358979, then INT(pi) would yield 3 , REAL(pi) would yield 3.141593, and DBLE(3) would yield 3.00000000000000.