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 * 2not 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
5 / 2 * 3.0 | is 6.0 Incorrect because the order of operation is left to right. 5/2 = 2 then 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
Function | Description | Type of Argument(s)* | Type of Value |
---|---|---|---|
ABS (x) | Absolute value of x | I, R, DP | Same as argument |
COS (x) | Cosine of x radians | R, DP | Same as argument |
DBLE(x) | Conversion of x to double precision form | I, R | DP |
DPROD(x,y) | Double precision product of x and y | R | DP |
EXP(x) | Exponential function | R, DP | Same as argument |
INT(x) | Integer part of x | R, DP | I |
LOG(x) | Natural logarithm of x | R, DP | Same as argument |
MAX(xl, . . . , Xn) | Maximum of xl, . . .,xn | I, R, DP | Same as argument |
MIN(xl, . . . , xn) | Minimum of xl, . . ., xn | I, R, DP | Same as argument |
MOD(x,y) | x (mod y); x - INT(x/y) * y | I, R, DP | Same as argument |
NINT(x) | x rounded to nearest integer | R, DP | I |
REAL(x) | Conversion of x to real type | I, DP | R |
SIN(x) | Sine of x radians | R, DP | Same as argument |