Com221 Note 2
Com221 Note 2
Integer Arithmetic
Integer arithmetic involves only integer data.
Integer arithmetic always produces an integer result.
With division, the result will never include a fractional part. The fractional part
truncated (dropped) by the machine.
Integers should never be used to calculate real-world quantities, such as distance,
speed, or time. Integers should only be used for things that are intrinsically integer
in nature, such as counters.
Mixed-Mode Arithmetic
Integer arithmetic produces integers.
Real number arithmetic produces real numbers.
What if we mix them?
In memory, the integer and real number are stored differently. So, the machine
must first convert (or cast) the integer into its real number equivalent before
performing an operation. This conversion will leave us with real number arithmetic.
These are grouped in order of precedence, thus, * has a higher precedence than +. The
precedence can be overridden by using parentheses. For example:
3 * 2 + 1 yields 7, but
3 * (2+1) yields 9.
pg. 1
For operators of equal strength the precedence is from left to right. For example:
a*b/c
In this statement, first a and b are multiplied, after which the results is divided by c. The
exception to this rule is exponentiation:
2**2**3 is evaluated as 2**8, and not as 4**3.
3/2 yields 1
-3/2 yields –1
3**2 yields 9
3**(-2) equals 1/3**2, which yields 0
Sometimes this is what you want. However, if you do not want the result to be truncated,
you can use the real function. This function converts its argument to type real. Thus,
real(2) yields a result of type real, with the value 2.0.
With the examples from above:
real(2)/3 yields 1.5
2/real(3) yields 1.5
-2/real(3) yields –1.5
real(3)**-2 yields 0.1111111111 (which is 1/9)
However:
real(2/3) yields 0 (the integer division is performed first, yielding 0, which is then
converted to a real.)
Note that the function real can have 2 arguments (see the table in the section on intrinsic
functions, below). The second argument, an integer specifying the precision (kind value) of
the result, is however optional. If not specified, the conversion will be to default precision.
Kind values will be discussed later.
Numeric expressions can contain operands of different type (integer, real, complex). If this
is the case, the type of the “weaker” operand will be first converted to the “stronger”
type. (The order of the types, from strong to weak, is complex, real, integer.) The result
will also be of the stronger type.
If we consider the examples above again, in real(2)/3 the integer 3 is first converted to a
real number 3.0 before the division is performed, and the result of the division is a real
number (as we have seen).
The .and. is “exclusive”: the result of a .and. b is .true. only if the expressions a and b are
both true.
The .or. is “inclusive”: the result of a .or. b is .false. only if the expressions a and b are both
false. Thus, if we have the logical constants:
logical, parameter :: on = .true.
logical, parameter :: off = .false.
Then:
.not. on ! equals .false. off .or. off !equals .false.
.not. off ! equals .true. on .eqv. on ! equals .true.
on .and. on ! equals .true. on .eqv. off ! equals .false.
on .and. off ! equals .false. off .eqv. off ! equals .true.
off .and. off ! equals .false. on .neqv. on !equals .false.
on .or. on ! equals .true. on .neqv. off ! equals .true.
on .or. off ! equals .true. off .neqv. off !equals.false.
A few examples:
real :: val1, val2
logical :: result
pg. 3
val1 = -3.5
val2 = 2.0
result = val1 < val2 !result equals .true.
result = val1 >= val2 !result equals .false.
result = val1 < (val2 – 2.0) !result equals .true.
Numeric Functions
Mathematical Functions
Numeric Inquiry Functions
Floating-Point Manipulation Functions
Bit Manipulation Functions
Character Functions
Kind Functions
Logical Functions
Array Functions.
Fortran - Loops
There may be a situation, when you need to execute a block of code several number of
times. In general, statements are executed sequentially: The first statement in a function is
executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple times
and following is the general form of a loop statement in most of the programming
languages:
Fortran provides the following types of loop constructs to handle looping requirements. Click the following
links to check their detail.
Loop Type Description
pg. 4
do loop This construct enables a statement, or a series of
statements, to be carried out iteratively, while a given
condition is true.
do while loop Repeats a statement or group of statements while a given
condition is true. It tests the condition before executing
the loop body.
nested loops You can use one or more loop construct inside any other
loop construct.
Comments
We have already seen the exclamation mark (!). All characters after the exclamation mark
are ignored. Comments can be used for descriptive purposes, or for “commenting out” a
line of code.
Continuation lines
pg. 5
The maximum length of a Fortran statement is 132 characters. Sometimes statements are
so long that they don’t fit on one line. The continuation mark (&), placed at the end of the
line, allows the statement to continue on the next line. Fortran 90 allows a maximum of 39
continuation lines.
Thus, the following code
cos (alpha) = b*b + c*c – &
2*b*c*cos (gamma)
is identical to
cos (alpha) = b*b + c*c – 2*b*c*cos (gamma)
Summary
A program starts with program program_name and ends with end program
program_name.
The first statement should always be implicit none.
We have learned the different types of variables and constants in Fortran: integer,
real, complex, logical and character, and how to declare them in type declaration
statements.
The arithmetic operators: **, *, /, + and -.
The logical operators: .not., .and., .or., .eqv., and .neqv.
The relational operators: <, <=, >, >=, == and /=.
We learned the mathematical functions, and a selection of other intrinsic functions.
We learned how to read in variables and how to write to the screen.
pg. 6