In Chapter 3, we learned that Fortran has six intrinsic data types: integer, real, double precision, complex, logical, and character. We used these data types to declare variable names in declaration statements.
This chapter describes how to fill in values (constants) for each one of these variable names. Also described in this chapter are: (1) assignment statements, (2) arithmetic, logical, and character operations, and (3) a few commonly used intrinsic functions. Definitions
The arithmetic variable can be of integer, real, complex, or double precision type (see Chapter 3). Operations with arithmetic and logical variables are based on rules of rank of data types, and order of precedence of operators (Section 5.3).
5.1 CONSTANTS
A constant is a value that can be stored in the computer's
memory. The value is stored in a specific location, the location
is referred to by its address, and the address is referred to by its
variable name. The programmer declares
the variable name and assigns a value to it; the computer
keeps track of the location and address.
Integer Constants
An integer constant is a whole number with no decimal
point. It has the form:
si
where s is the sign (+ or -) and i is the integer number. For
example, -135 is an integer constant. The following rules apply for integer constants:
0
92
-345 The following are examples of invalid integer constants: 3.1416 31,860 9753845210 Periods or commas are not allowed, and the last example (9753845210) is out-of-range (it is too large, even
for a 4-byte integer constant).
In unformatted input, integer constants should have no
decimal point. If the decimal point is present, with or without a fractional part, the decimal point is ignored and the
fractional part, if any, is disregarded. Unformatted integer
output appears with no decimal point.
A real constant is a number that is written with a decimal
point and may have a fractional part following the decimal
point. A real constant has one of the following forms: s.r sr.r sr.
where s is the sign (+ or -) and r is a string of one or more
decimal digits; for example, -132.764 is a real constant.
The decimal point precedes or follows a string r, or is
embedded between two r's. As with an integer constant, a real constant can be
negative, zero, or positive. Although the plus sign (+) is
optional, the minus sign (-) is required preceding a negative real constant. Real constants may also be written in exponential notation. Exponential notation uses the letter E, followed by a
sign (+ or -) and an integer exponent, to represent "... times
10 to the power ..." A real constant can contain only the
numerals 0 through 9, algebraic signs, a decimal point, and
the letter E. In exponential notation, a real constant can
take any of the following forms: s.rEsk
sr.rEsk
sr.Esk iEsk
where k is the integer exponent and all other symbols have
been previously defined. An example is: -132.764E-2
The following rules apply for exponential notation:
The following are examples of valid real constants:
3.14159
-0.56
-3.56E-3
-123.456E0
56E4
The following are examples of invalid real constants:
3,1416
$33560
123._67
Commas or special characters such as $ or _ are not allowed as part of real constants.
In unformatted input, a real constant should have a
decimal point and, optionally, a fractional part. If a real
constant has no decimal point and therefore, no fractional
part, a decimal point is added to the right of the constant
and the fractional part is taken to be a string of zeros. Unformatted real output has a decimal point and a fractional
part, which may be a string of zeros.
A double-precision constant is written in exponential notation by using the letter D instead of E. A double-precision
constant has one of the following forms:
s.rDsk
sr.rDsk
sr.Dsk
iDsk
where D denotes "... times 10 to the power ..." and all
other symbols have been previously defined. For example:
-13257.3619D-3
As with real exponential notation, plus signs are optional and minus signs are required. The exponent k is required even if it happens to be zero.
There are two types
of double-precision constants: D-type and G-type. In D-type, the exponent k can have at most two digits.
In G-type, the exponent k can have at most three digits. The
type of double-precision constant is determined during
compilation time in a processor-dependent manner.
There is no limit to the number of digits of the string r,
but only the leftmost n digits (the value of n is processor
dependent) are significant. For an 8-byte double-precision
wordsize, n= 16 and k= 2 for a D-type constant; and n= 15
and k= 3 for a G-type. It can be seen that a G-type constant
has a slightly lesser accuracy than D-type, but it allows for
a larger integer exponent.
The following are examples of valid double-precision
constants:
-.123D-3
0.31D25
-35071.D+3
56D299
The last example (56D299) is valid only as G-type.
In unformatted input, a double-precision constant
should be specified using an exponential notation with the
letter D. Unformatted double-precision output is written in
exponential notation using the letter E.
In Fortran, a complex constant represents the complex
number a + bi.
A complex constant consists of a pair of real or integer
constants, separated by a comma and enclosed in parentheses. The first constant represents the real part of the complex number (a) and the second represents the imaginary
part (b). It takes the form:
(a,b)
where a and b are real or integer constants, as shown in the
following examples:
(2,3)
(3.5,-5.6)
(123.45E3,5.43E-1)
In unformatted input, a complex constant should be enclosed in parentheses, with its real and imaginary parts
separated by a comma. Unformatted complex output appears in the same way. A logical constant defines a logical value as either true or false. It takes one of the following forms: .TRUE. .FALSE. The surrounding (delimiting) periods are required. In unformatted input, a logical constant can be read as either .TRUE. or .FALSE., or any other character string beginning with the letters T (true) or F (false). Either of the surrounding periods is optional. Unformatted logical output appears as a single T (true) or a single F (false).
A character constant is a string of valid characters en closed within apostrophes. It takes the following form: 'abc&xyz'
The character string abc&xyz enclosed within apostrophes stands for any string of characters of the Fortran character set (Section 1.1). The value of the character constant is the string of characters contained between the required apostrophes. This value does not include the apostrophes, but it does include all blank spaces and tabs, if any, within them. The length of a character constant must be in the range of 1 to 2000 characters, including blank spaces. Within a character constant, the apostrophe character is represented by two consecutive apostrophes, with no blank spaces between them, as shown below: 'MCDONALD''S HAMBURGERS' This character constant would be stored and printed as follows: MCDONALD'S HAMBURGERS In unformatted input, a character constant should be surrounded by apostrophes. In unformatted output, a character constant appears as a character string without apostrophes. 5.2 ASSIGNMENT STATEMENTS
The three types of assignment statements are:
Arithmetic Assignment Statement The arithmetic assignment statement is perhaps the most common Fortran statement. It defines the value of a variable, be it integer, real, double precision, or complex. It has an equal (=) symbol preceded by a variable name and followed by a constant or arithmetic expression whose value is determined at execution time. The arithmetic assignment statement takes the form: v= e where v is a variable name and e is a constant or arithmetic expression. The symbol = does not mean "equal to" as in conventional mathematics. Rather, it means "is replaced by". For example, in the following statement: KCOUNT= KCOUNT + 1 the integer KCOUNT is incremented by one at execution time. The old value is replaced by the new value; the old value is lost and the new value remains in storage. The following statement defines the value of Z: Z= X + Y The values of X and Y should be defined prior to the execution of this statement. It is important to realize that some processors may assign initial values of 0. to all real variables. Then, the absence of a definition of X or Y will not stop execution. In the above case, if neither X nor Y were defined prior to this statement, the processor would add X = 0. to Y = 0. to give Z = 0.
In the assigment statement v = e:
In certain cases, the conversion may result in the loss of a portion of the data, as shown in the following statements: X= 3.5 J= X + 1 in which X is real and J is integer. Upon execution of these two statements, the value of X + 1 is 4.5; however, only its integer portion can be stored in J. Therefore, J= 4. Examples of valid arithmetic assignment statements are: I= J/K*M PI= 3.1416 B= 1.3D-2**3.5 ALPHA= (3.2*X*Y)/(2.5*Z) ZT= Z1 + (Z2*Z3) - (Z4/Z5) COM= (12.5,34.7) + (9.6,-3.5) Examples of invalid arithmetic assignment statements: 25.= A ALPHA= (3.2*X*Y)/(2.5*Z PI= 3.1416. ZT= Z1 + Z2*/Z3 Regarding these examples:
Logical Assignment Statement The logical assignment statement defines the value of a logical variable. It consists of an equal (=) symbol preceded by a logical variable name and followed by a logical constant (or logical expression) whose value is determined at execution time. The logical assignment statement takes the form: v = e where v is a logical variable name and e is a logical constant (or logical expression). The following are examples of valid logical assignment statements: A= .TRUE. BL= CL.AND.DL P= X.LT.3.5.AND..NOT.Q BIGV= V.GT.X.AND.V.GT.Y.AND.V.GT.Z Examples of invalid logical assignment statements: A= TRUE BL= CL.AND..DL P= X.LT.3.5.NOT..AND.Q BIGV= V.GT.X.AND.V.GT.Y.AND.GT.Z In these examples:
The character assignment statement defines the value of a character variable. It consists of an equal (=) symbol preceded by a character variable name and followed by a character constant (or character expression) whose value is determined at execution time. The character assignment statement takes the form: v = e where v is a character variable name and e is a character constant (or character expression). The following rules apply:
For example: CHARACTER*15 PROJECT,DATE PROJECT= 'SOLAR DEVELOPMENT NO. 2' DATE= 'MAY 10, 1994' The first statement declares PROJECT and DATE as character variables, each of maximum length equal to 15 spaces. The second statement assigns the character constant SOLAR DEVELOPMENT NO. 2 to PROJECT. The third statement assigns the character constant MAY 10, 1994 to DATE. Upon execution, the variable PROJECT is completely filled with the first 15 characters of SOLAR DEVELOPMENT NO. 2 The remaining characters are lost. The value of PROJECT is then SOLAR DEVELOPME
Likewise, the variable DATE is partially filled, starting from the left, with all the characters of MAY 10, 1994. The remaining three spaces to the right are filled with blank spaces denoted by b. The value of CHAR2 is then MAY 10, 1994bbb
Character constants are not initialized at execution time, remaining undefined until an assignment is made by a DATA or character assignment statement, or read from an input file. Following are valid character assignment statements: FRUIT= 'ORANGE' LABEL= 'THE VALUES ARE: ' ERROR_35= 'PLEASE EXAMINE VALUE OF Y' Following are invalid character assignment statements: FRUIT= 'APPLE NAME= 'MCDONALD'S' ERROR_45= IMPROPER VALUE OF Y Regarding these examples, note:
5.3 OPERATIONS
Arithmetic Operations Table 5.1 lists arithmetic operators. Table 5.2 shows examples of the order of precedence of arithmetic operations. Within one arithmetic expression, execution follows a preestablished order of precedence:
You may use parentheses to force an order of evaluation different from the preestablished order of precedence. Expressions within parentheses are evaluated first, working outwards to the expression within the outermost set. The expression contained within each set of parentheses is subject to the preestablished order of precedence. Nonessential parentheses do not affect the evaluation of the expression.
Arithmetic data types rank in this order, from highest to lowest:
When evaluating an arithmetic expression that contains two elements of different data type, the result is a variable of data type equal to that of the element of highest rank. For example, if two variables, one real and one integer, appear in one arithmetic expression, the result of the operation is of real type, as in A = 1.6 I = 2 C = A * I In this case, C is equal to 3.2. When an operation involves two integers, the result is also an integer. Therefore, any fraction resulting from the division of two integers is automatically truncated (lost). For example, in the following sequence of statements: I= 7 J= 2 K= I/J the value of K is stored as 3, not 3.5. Disregarding the possibility of this truncation is a common source of error in programming. In the following sequence of statements: I= 5 J= 2 X= 4. Y= I/J*X the expression for Y is evaluated from left to right. First, I/J= 2; second, Y is equal to 2 times 4, resulting in Y= 8. Parentheses may be used to override the established order of precedence. For instance: Y= I/(J*X) This results in first multiplying J times X, to yield 8., and then Y= 5/8. = 0.625. Note that two similar expressions (one with and another without parentheses) yield completely different answers. Logical Operations Table 5.3 lists the preestablished order of precedence of combined relational and logical expressions. Practical examples are shown in the box immediately following the table. As stated previously, when operators are of equal precedence, they are evaluated from left to right, except for exponentiation, which is evaluated from right to left. As with arithmetic operations, parentheses can be used to force an order of evaluation different from the preestablished order of precedence.
Character Linking Character strings can be linked together (i.e., concatenated) using the double slash operator (//), as shown in the following example: ALPHA='ABC'//'DEFGHI'//'JKLMNOPQRS'//'TUVWYZ' The character variable ALPHA is stored in memory as: ABCDEFGHIJKLMNOPQRSTUVWXYZ Embedded blank spaces, if any, are incorporated as part of the character string. 5.4 INTRINSIC FUNCTIONS
The Fortran library contains a set of intrinsic (built-in)
functions. These functions perform common mathematical
computations such as the square root, the logarithm, or a
trig function.
An example of the intrinsic function that calculates an
absolute value is shown in the following sequence of statements:
X= -3.2
Y= 10.5
Z= ABS(X) + Y In this example, the value of Z is evaluated as 13.7, i.e.,
the absolute value of X (3.2) plus the value of Y (10.5).
A few commonly used intrinsic functions are described
in Table 5.4.
The MOD function is used when action needs to be
taken at regular intervals, as shown below. 10 K= K + 1 IF (MOD(K,5).EQ.0) THEN WRITE(6,*) 'FIFTH K VALUE' END IF IF (K.LE.100) GO TO 10 CONTINUE This loop executes 100 times. The message FIFTH K VALUE will be printed when the counter K is a multiple of 5 (i.e., every fifth passage through the loop). 5.5 EXAMPLE PROGRAMS
Example Program 1: Assignment Statements
This program defines a series of variables of different data types, including integer (I,J), real (A,B), double precision (D1, D2, D3), complex (C1, C2, C3), logical (L1, L2), and character (LAB, LABEL). The program proceeds to do a series of valid operations on these variables, and writes the output directly to the screen. The first line of output from this program (unformatted output) is: 17 11.45000 (5.000000,10.00000) 2203703683870369. T The second line is: X_X_X_ ABCDEFGHIJKLMNOPQRSTUVWXYZ Example Program 2: Tic-Tac-Toe Board
The output from this program is: X XO O X X X0X0X0XOXOXOXOXOX X X O O X X X0X0X0XOXOXOXOXOX X X O O X X Note the following features of this program:
5.6 SUMMARY
This chapter describes constants, assignment statements, operations on arithmetic, logical and character variables, and a few commonly used intrinsic functions.
PROBLEMS
Write a program to calculate all six hyperbolic trigonometric functions. Test your program with θ= 30o. Write an interactive program to calculate the natural log of x using the first ten terms of the following infinite series: ln x = 2{[(x-1)/(x+1)] + (1/3)[(x-1)/(x+1)]3 + (1/5)[(x-1)/(x+1)]5 + ... }. Do not write out the 10 terms; rather, accumulate the sum in one storage location. Test your program with x= 1.5.
Write an interactive program to calculate tan-1x by using the first 10 terms of the following infinite series: tan-1x = x - (x3/3) + (x5/5) - (x7/7) + ... , for |x| < 1. Do not write out the 10 terms; rather, accumulate the sum in one storage location. If |x| is greater than or equal to 1, write a message such as: ENTERED VALUE IS OUT OF RANGE. PLEASE TRY AGAIN, and prompt for a new value of x. Test your program with x= 1.5, and x= 0.5.
Write an interactive program to calculate sin-1x by using the first 8 terms of the following infinite series: sin-1x = x + (1/2)(x3/3) + (1/2)(3/4)(x5/5) + (1/2)(3/4)(5/6)(x7/7) + ... , for |x| < 1. Do not write out the 8 terms; rather, accumulate the sum in one storage location. If |x| is greater than or equal to 1, write a message such as: ENTERED VALUE IS OUT OF RANGE. PLEASE TRY AGAIN, and prompt for a new value of x. Test your program with x= 1.5, and x= 0.5.
Write a program to calculate and print to a file TEMPERATURE.OUT a temperature conversion table, from Farenheit to Celsius, for 32 ≤ oF ≤ 212, in intervals of 1oF. Use appropriate table headings, such as: TEMPERATURE (oF) TEMPERATURE (oC)
Write a program to calculate the first positive root of a cubic equation of the form: y = ax3 + bx2 + cx + d. Hint: Evaluate y(x) for x= 0, 1., 2., and so on, in increments of 1., until y changes sign (goes from positive to negative, or viceversa). Then, decrease the increment to one-tenth of its value, return to the previous value of x, and continue to repeatedly evaluate the function y, until it changes sign again, decreasing the increment again, and repeating the process. Stop the calculation after 7 changes in sign (i.e., after 7 significant figures). Write the value of the first positive root x, preceded by an appropriate label. (THE FIRST POSITIVE ROOT OF THE CUBIC EQUATION IS = ). Test your program with the following data: a= 20., b= -35., c=15., d= -5.
Repeat Problem 10 counting the number of trials that it took to get the answer, and reporting the total number of trials at the end.
Repeat Problem 10 using the Newton approximation to the root. After the first sign change, return to the previous value of x (i.e., xo), and evaluate the new trial value of the root using Newton's approximation: xr= xo - [y(xo) / y'(xo)]. Repeat the process until the change in the value of the root is less than 1 × 10-6. Test your program using the same input data of Problem 10.
Repeat Problem 12 counting the total number of trials that it took to get the answer, and reporting the total number of trials at the end.
Write a program to print the following banner resembling the U.S. flag:
* * * * * * RRRRRRRRRRRRRRRRRRRRRRRR
|
Documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view; download Adobe Acrobat Reader. |
FORTRAN FOR SCIENTISTS AND ENGINEERS VICTOR M. PONCE • ONLINE EDITION • | |||||||
Copyright © 2014 • Victor M. Ponce • All rights reserved. |