Vous êtes sur la page 1sur 4

Arithmetic Operations and Functions

Operations
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 slash (/), and exponentiation is denoted by a pair of
asterisks (**).

Operator Operation
+ addition, unary plus
- subtraction, unary minus
* multiplication
/ division
** exponentiation

Real Arithmetic
Providing all variables and constants in the expression are real, real arithmetic will be
carried out as expected, with no decimal places being truncated.

Integer Arithmetic
Providing the expression has all integers, subtraction, addition, multiplication and
exponentiation will prove no problem. However integer division is somewhat different
than normal division with real values. Integer division ignores the fractional part. Any
decimal places are truncated.

Example

5 / 2 gives the result 2 instead of 2.5


3 / 4 gives the result 0 instead of 0.75

Mixed Mode Arithmetic


Mixed mode arithmetic is when an expression contains both reals and integers. If
ANY of the operands are real then result of the operation will be real. However, mixed
mode arithmetic should be used with extreme care. You may think you have a real
operand when in reality you have two integer operands.

Example

is 6.0 Incorrect because the order of operation is left to right. 5/2 = 2 then 2 * 3.0 =
5 / 2 * 3.0
6.0
3.0 * 5 / 2 is 7.5 Correct because of mixed mode arithmetic 3.0 * 5 = 15.0 then 15.0/2 = 7.5

Mixed Mode Variable Assignments


If the variable to which an expression is assigned has been declared as a real variable,
any decimal places resulting from the evaluation of the expression will be preserved.

Example

real variable 5 * 2.1 will have a value of 10.5.

However, if the variable to which an expression is assigned has been declared as an


integer variable, any decimal places resulting from the evaluation of the expression
will be lost.

Example

integer variable 5 * 2.1 will have a value of 10

Priority Rules.
Arithmetic expressions are evaluated in accordance with the following priority rules:

All exponentiations are performed first; consecutive exponentiations are


performed from right to left.

All multiplication and divisions are performed next, in the order in which they
appear from left to right.

The additions and subtractions are performed last, in the order in which they
appear from left to right.

Functions
FORTRAN provides several intrinsic functions to carry out calculations on am
number of values or arguments and return as result. Commonly used functions are
shown in the table below. To use a function we simply give the function name
followed by the argument(s) enclosed in parenthesis.

funtionname (name1, name2,.......)

Some FORTRAN Functions

Type of
Function Description Type of Value
Argument(s)*

Same as
ABS (x) Absolute value of x I, R, DP
argument

Same as
COS (x) Cosine of x radians R, DP
argument

Conversion of x to double
DBLE(x) I, R DP
precision form

Double precision product of x


DPROD(x,y) R DP
and y

Same as
EXP(x) Exponential function R, DP
argument

INT(x) Integer part of x R, DP I

Same as
LOG(x) Natural logarithm of x R, DP
argument

MAX(xl, . . . , Same as
Maximum of xl, . . .,xn I, R, DP
Xn) argument

Same as
MIN(xl, . . . , xn) Minimum of xl, . . ., xn I, R, DP
argument

Same as
MOD(x,y) x (mod y); x - INT(x/y) * y I, R, DP
argument

NINT(x) x rounded to nearest integer R, DP I

REAL(x) Conversion of x to real type I, DP R


Same as
SIN(x) Sine of x radians R, DP
argument

Same as
SQRT(x) Square root of x R, DP
argument

* I = integer, R = real, DP = double precision.

Assignment Statement
The assignment statement is used to assign values to variables and has the form:

variable = expression

For Example:

x = 2356.0
y = SQRT (25.0)
direction = x * y

Vous aimerez peut-être aussi