0% found this document useful (0 votes)
20 views6 pages

Com221 Note 2

The document discusses Fortran arithmetic rules, integer arithmetic, mixed-mode arithmetic, arithmetic operations and functions, numeric expressions, logical operators, relational operators, logical expressions, and loops in Fortran. Key points include: Fortran uses operators like *, /, +, - for arithmetic; integer division truncates results; mixed-mode arithmetic converts between types; parentheses change precedence order; logical operators include .AND., .OR., .NOT.; relational operators compare values; loops allow repeating code blocks through DO, DO WHILE, and nested loops.

Uploaded by

Ramadan Adamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Com221 Note 2

The document discusses Fortran arithmetic rules, integer arithmetic, mixed-mode arithmetic, arithmetic operations and functions, numeric expressions, logical operators, relational operators, logical expressions, and loops in Fortran. Key points include: Fortran uses operators like *, /, +, - for arithmetic; integer division truncates results; mixed-mode arithmetic converts between types; parentheses change precedence order; logical operators include .AND., .OR., .NOT.; relational operators compare values; loops allow repeating code blocks through DO, DO WHILE, and nested loops.

Uploaded by

Ramadan Adamu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 6

Fortran - Arithmetic Rules

1. No two operators can occur side by side. x = a * - b is illegal, while x = a * (-b) is


accepted
2. Implied multiplication is illegal. x = yz does not work, but x = y * z does.
3. Parentheses can be used to force a change in the order of precedence in an
expression. The (BODMAS) rules you were exposed to in primary school math be
applied.

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.

Fortran - Arithmetic Operations & Functions


In Fortran, addition and subtraction are denoted by the usual plus (+) and minus (-) signs.
Multiplication is denoted by an asterisk (*). This symbol must be used to denote every
multiplication; thus, to multiply N by 2, we must use 2 * N or N * 2 not 2N. Division is
denoted by a forward slash (/) sign, and exponentiation is denoted by a pair of asterisks
(**).
The intrinsic arithmetic operators available in all Fortran versions include:
Sign Function
** exponentiation
* multiplication
/ division
+ addition
− subtraction

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.

Fortran - Numeric Expressions


An expression using any of the arithmetic operators (**, *, /, +, -) is called a numeric
expression.
Be careful with integer divisions! The result of an integer division, i.e., a division in which
the numerator and denominator are both integers, is an integer, and may therefore have
to be truncated. The direction of the truncation is towards zero (the result is the integer
value equal or just less than the exact result):

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).

Fortran - Logical Operators


pg. 2
The type logical can have only two different values: .true. and .false. (note the dots around
the words true and false). Logical variables can be operated upon by logical operators.
These are (listed in decreasing order of precedence):
.not.
.and.
.or.
.eqv.
.neqv.

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.

Fortran - Relational operators


Relational operators are operators that are placed between expressions and that compare
the results of the expressions. The relational operators in Fortran 90 are:
< less than
<= less than or equal to
> greater than
>= greater than or equal to
== equal to
/= not equal to

Fortran - Logical Expressions


Expressions that use the relational operators are logical expressions. The values of logical
expressions can be either .true. or .false. Note that the range of numerical numbers in
Fortran ranges from the largest negative number to the largest positive number (thus, -
100.0 is smaller than 0.5).

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.

Fortran - Intrinsic functions


Intrinsic functions are some common and important functions that are provided as a part
of the Fortran language. We have already discussed some of these functions in the Arrays,
Characters and String chapters.
Intrinsic functions can be categorized as:

 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.

Loop Control Statements


Loop control statements change execution from its normal sequence. When execution
leaves a scope, all automatic objects that were created in that scope are destroyed.
Fortran supports the following control statements.
Control Description
Statement

exit If the exit statement is executed, the loop is exited, and


the execution of the program continues at the first
executable statement after the end do statement.

cycle If a cycle statement is executed, the program continues


at the start of the next iteration.

stop If you wish execution of your program to stop, you can


insert a stop statement

The mathematical functions are: cosh (x) hyperbolic cosine function


acos (x) inverse cosine (arc cosine) exp (x) exponential function
function log (x) natural logarithm
asin (x) inverse sine (arc sine) function
function log10 (x) common logarithm function
atan (x) inverse tangent (arc tangent) sin (x) sine function
function sinh (x) hyperbolic sine function
atan2 (x) arc tangent for complex sqrt (x) square root function
numbers tan (x) tangent function
cos (x) cosine function tanh (x) hyperbolic tangent function

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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy