Write a detailed note on Arithmetic used in the Fortran programming.
Answers
Explanation:
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.
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.
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.