Next Step, Arithmetic Assignment Statements

Assignment :

Study and understand the examples in arith.f and arithp.f, and those in Chapter 3 of your textbook.

You may not realize it, but most of you have experience programming arithmetic assignment statements. If I asked you to solve the current homework problem with a spreadsheet, you might begin by putting the time of fall in box A1 of the spreadsheet. Then in box A2, you would enter the formula "=0.5*9.8*A1^2. Box A2 would contain the distance of fall, and you would have just finished a simple programming job.

Fortran arithmetic assignments are very similar to those used in spreadsheets, Basic, or C. We had a simple example in the last class setting "s=a+b". For a spreadsheet, it is implied that "A2", is to the left of the equals. For Fortran or any similar language, you must include the name of a variable (shorthand for a memory address) to the left of the "=" to receive the results of the operations to the right of the "=".

Simple Arithmetic Operators

Operators in Fortran expressions are about what you would expect from other experiences, except perhaps exponentiation:

	+		add
	-		subtract
	*		multiply
	/		divide
	**		exponentiate
The Fortran expression, equivalent to our spreadsheet example above, might be:

d = 0.5*9.8*t**2 The use of "t**2" to square the time, is only required for Fortran 77. Fortran 90 is more generous and will recognize "t^2".

Precedence of Operations

It is important to understand how Fortran interprets a complex combination of arithmetic operations, to avoid unexpected results. It performs operations according to an order of precedence, and for operations with the same precedence will usually process them from left to right. The exception to this is when two or more exponentiations occur in a row. These sequential exponentiations are done from right to left. Order of precedence is:

  1. Parentheses - Fortran first performs operations grouped within parentheses. Within a given set of parentheses the precedence below is followed. There are no surprises here. Fortran interprets the use of parentheses in the way you have been trained during years of math courses.
  2. Exponentiation (** or ^) - Once parentheses are sorted out, Fortran takes the value to the left of every "**" and raises it to the power given by the value immediately to the right. Remember if a sequence like a**b**c occurs, it is interpreted as a**(b**c).
  3. Multiplication and Division (* and /) - given values existing or generated from any parenthetic expressions or exponentiation, Fortran next sweeps the arithmetic expression from left to right and performs each multiply and divide as they are found, using the values immediately to the left and right of the operator.
  4. Addition and Subtraction (+ and -) - given values existing or generated by operations with higher precedence, Fortran finally sweeps the arithmetic expression from left to right and performs each add and subtract as they are found, using the values immediately to the left and right of the operator.

As an example let's look at the following expression:

answer = 2.0 + 3.0*5.0/2.0 - (1.0 + 4.0)*3.0**2/3 First the operations in the parentheses are done to give:

answer = 2.0 + 3.0*5.0/2.0 - 5.0*3.0**2/3 Next the exponentiation is done to give:

answer = 2.0 + 3.0*5.0/2.0 - 5.0*9.0/3 If you really wanted three to the two-thirds power, you were out of luck.

Next a sweep is made evaluating multiplications and divides:

answer = 2.0 + 15.0/2.0 - 5.0*9.0/3 answer = 2.0 + 7.5 - 5.0*9.0/3 answer = 2.0 + 7.5 - 45.0/3 answer = 2.0 + 7.5 - 15.0 Finally adds and subtracts are performed

answer = 9.5 - 15.0 answer = 24.5

A Fine Point on use of Parentheses

When doing algebra, you were used to writing an expression like: x times the quantity a plus b as:

	x(a+b) or (a+b)x
In Fortran you must explicitly include the multiplication operator to avoid confusion with the use of functions:

x*(a+b) or (a+b)*x

Integer and Mixed Integer/Real Arithmetic

Once you are comfortable with how the precedence of operations work, you should have no problems with real (floating point) arithmetic. However there are a couple things you need to know about integer arithmetic and the consequences of mixing real numbers and integers in a calculation.

You had an example of how to declare Fortran variables to be real numbers. If you want them to be integers, the declaration statement is analogous. For example to let Fortran know that the variables "ians", "i1", and "i2" are integers include the statement:

integer ians, i1, i2 Such a type declaration must be made before any "executable" Fortran statements. So far the arithmetic assignment statement and "stop" are the only executable statements that you know, but there will be more. As we learn new Fortran statements make a mental note on each whether it is "executable", or is in the same "non-executable" class as type statements, and must be placed before the "executable" ones.

Let's look at what happens with the following lines of code.

i1 = 5 i2 = 3 ians = i1 / i2 If you plug this into a calculator, you get and answer of 2.666667. Since "ians" is an integer, you may be tempted to believe its value will be 3. Actually the value of "ians" is 2. The standard behavior of an integer divide is to produce an answer that is truncated down to the next integer value less than or equal to the real result of the divide.

At times you will want to do calculations involving mixtures of reals and integers. The results can be somewhat unexpected, until you recognize the pattern. If both variables (or constants) associated with a given operator (+,-,*,/) are integers, then the integer arithmetic unit is used and an integer value is returned for that portion of the calculation. If one is real, then the other is converted to real, and a real value is returned from the operation. Take a careful look at the examples of mixed arithmetic in arith.f.

If the value generated by the operations on the right side of the "=" is real, but the variable on the left side of the "=" is integer then the result of the calculation is truncated down to the next integer value less than or equal to the real result.

For example a Fortran statement:

ians = 2.0/3 + 1.0/2 Produces a value of 1 for "ians".

Be very careful, with this type of operation. You may think that the result of a set of real arithmetic is going to be 2.0. However, due to the finite precision of computer arithmetic the computed result may be 1.999999. If this is converted to an integer, the answer is 1.

Floating Point Constants

I have been casually using constants in arithmetic assignments. As you would expect, Fortran treats a constant as a real number if it sees a decimal point, and as an integer if not. However, I've limited myself to fairly simple constants. What if you want to use a really big number or a really small number? This of course calls for some form of scientific notation. The Fortran notation has been picked up by spreadsheets, so you may have seen it. If I want to use the speed of light (3 times ten to the 8th power meters per second) in a Fortran program, I might assign a variable:

c = 3.0e8

If I needed the mass of an electron, I might make the assignment:

emass = 9.11e-31

Two other ways to assign values to Variables

When the Fortran compiler creates the executable file, it is to a large extent creating the image of your program's future layout in the main memory of the computer, and includes space in the disk file that mirrors the space in computer memory where your variables will be located. As a result it is possible to include "non-executable" statements in your program that instruct the compiler to preload values for various variables or parameters.

Instead of an executable assignment statement like:

c = 3.0e8

You can get the compiler to put the value 3.0e8 into the location represented by "c", so it will be ready as soon as the program starts to execute. One way of doing this is a DATA statement:

data c / 3.0e8/

The value associated with the variable is always surrounded by a pair of slashes. Look at arith.f for examples of the use of the DATA statement to define more than one variable, paying attention to the use of commas to separate variable names and values.

Another way of preassigning a value to "c" is the PARAMETER statement:

parameter (c = 3.0e8)

The parentheses are always present in the syntax, as is the "=". Look at arithp.f for examples setting more than one parameter in a given statement, and for use of arithmetic assignments within the PARAMETER statement.

Because DATA and PARAMETER are "non-executable", they must be placed in the block of statements before "executable" statements (with REAL and INTEGER statements). They should be positioned after the REAL or INTEGER statements that establish the data type of the particular variables or parameters defined in the specific DATA and PARAMETER statements.

Notice that I'm starting to talk about variables and parameters as separate objects. That is because there are some important differences between the items set in DATA statements and those defined in PARAMETER statements. Anything set in a DATA statement is a true Fortran variable. It can be given a different value elsewhere in the program with an arithmetic assignment statement. The parameters assigned in a PARAMETER statement behave exactly like Fortran constants (2.0, 2, 2.0e0, etc) within a program. They can be used anywhere a constant can, and like a constant have a fixed value that can't be altered by the program. You will later see statement constructs that want constants in certain positions and don't accept variables. Any attempt to use a parameter on the left side of an "=" outside of the PARAMETER statement itself, will result in a Fortran syntax error at compilation time.

The restriction on reassignment of parameters is accompanied by the flexibility of using arithmetic expressions to initial set the value of the parameter. These expressions can contain constants and any previously defined parameter. No such capability exists within a DATA statement. The DATA statement is a straight initial assignment of the value of a Fortran constant to a variable.

We will have to learn about Fortran functions, subroutines, and arrays, before the full power of DATA and PARAMETER statements becomes apparent.

Review Questions

Test your knowledge with some review questions on this lecture.

Up one level / Home

Maintained by John Mahaffy :