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 * 2not 2N. Division is denoted by a slash (/), and exponentiation is denoted by a pair of asterisks (**).
OperatorOperation
+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.0is 6.0 Incorrect because the order of operation is left to right. 5/2 = 2 then 2 * 3.0 = 6.0
3.0 * 5 / 2is 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



FunctionDescriptionType of Argument(s)*Type of Value
ABS (x)Absolute value of xI, R, DPSame as argument
COS (x)Cosine of x radiansR, DPSame as argument
DBLE(x)Conversion of x to double precision formI, RDP
DPROD(x,y)Double precision product of x and yRDP
EXP(x)Exponential functionR, DPSame as argument
INT(x)Integer part of xR, DPI
LOG(x)Natural logarithm of xR, DPSame as argument
MAX(xl, . . . , Xn)Maximum of xl, . . .,xnI, R, DPSame as argument
MIN(xl, . . . , xn)Minimum of xl, . . ., xnI, R, DPSame as argument
MOD(x,y)x (mod y); x - INT(x/y) * yI, R, DPSame as argument
NINT(x)x rounded to nearest integerR, DPI
REAL(x)Conversion of x to real typeI, DPR
SIN(x)Sine of x radiansR, DPSame as argument