Basic Scientific Programming
Computer Arithmetic

Eyad Taqieddin

1
Arithmetic Operations and Functions
Operator

Operation

Fortran Notation

+

Addition,Unary Plus

a+b , +b

-

Subtraction,Unary
Minus

a-b , -b

*

Multiplication

a*b

/

Division

a/b

Exponentiation

a**b

**

Eyad Taqieddin

2
Integer Arithmetic





Int
Int
Int
Int

+ Int = Int
- Int = Int
* Int = Int
/ Int = Int

5+2=7
5 -2=3
5 * 2 = 10
5 / 2 = 2 why?

Division is a special case, because the result
must be an integer. So, the fraction part is
truncated, and only the integer is stored.
Eyad Taqieddin

3
Example
what is the result of the following?
1) program example
Implicit none
Real :: x= 2.0 , y
Integer :: n = 2, j
y= 1/x
j= 1/n
print *, ‘y=‘ , y
print *, ‘j=‘, j
end program example
Eyad Taqieddin

4
Priority Rules
1) Parenthesis
2) All exponentiations are performed first, consecutive
exponentiations are performed from right to left.
3) All Multiplications and divisions are performed next,
in the order in which they appear from left to right.
4) The additions and subtractions are performed last,
in the order in which they appear from left to right.

Eyad Taqieddin

5
Example


2**3**2 = 2**9 = 512



10-8-2 = 2 – 2 = 0



2+4/2 +5**3 = 2+ 4/2+125=2+2+125
= 129

Eyad Taqieddin

6
Example



((-b) + sqrt( B**2 – 4*a*c))/(2*a)
((-b) + sqrt( B**2 – 4*a*c))/2*a
do u think these expressions are equal??

Eyad Taqieddin

7
Mixed Mode Expressions
Expressions that involve different types of
numeric operands
Int + Real = Real
Int - Real = Real
Int * Real = Real
Int / Real = Real
The integer value is converted to its real
equivalent.
Eyad Taqieddin

8
Example




2.0/5  2.0/5.0  0.4
3.0 + 6/4  3.0 + 1  3.0 +1.0  4.0
3.0 + 6.0/4 3.0+ 6.0/4.0  3.0 +1.5
 4.5
from the second and third expressions we see
why it is not a good programming practice to
use mixed mode operations.
Eyad Taqieddin

9
Functions
Fortran provides library functions for many
mathematical operations and functions.
Ex:

ABS(X) returns the absolute value of X
Sqrt(X) returns the square root of X
Sin(X) returns the sine of X radians

Eyad Taqieddin

10
The Assignment Statement
Form
Variable = Expression



Variable is a valid Fortran identifier.
Expression may be a constant, another
varialble to which a value has previously been
assigned or a formula to be evaluated.

Eyad Taqieddin

11
Example






Length = 6
assigns the value 6 to the memory location length.
C= x**2 + 5*x + 7
calculates the expressions (x**2 + 5*x + 7) and
assigns the result to the memory location C.
Salary = Salary + 1000
adds the value 1000 to the value stored in the
memory location salary and then stores the result in
the same memory location.
The previous value of salary is lost.
Eyad Taqieddin

12
Integer Vs. Real




If an integer-valued expression is assigned to a real
variable, the integer value is converted to a real
constant and then assigned to the variable.
temp = (6+2)/4 “assuming temp is real”
temp will be assigned the value 2.0 not 2
If a real-valued expression is assigned to an integer
variable, the fractional part is truncated and the
integer part is assigned to the variable.
temp = 2.36 / 2 “assuming temp is integer”
temp will be assigned the value 1 not 1.18
Eyad Taqieddin

13

1 arithmetic

  • 1.
    Basic Scientific Programming ComputerArithmetic Eyad Taqieddin 1
  • 2.
    Arithmetic Operations andFunctions Operator Operation Fortran Notation + Addition,Unary Plus a+b , +b - Subtraction,Unary Minus a-b , -b * Multiplication a*b / Division a/b Exponentiation a**b ** Eyad Taqieddin 2
  • 3.
    Integer Arithmetic     Int Int Int Int + Int= Int - Int = Int * Int = Int / Int = Int 5+2=7 5 -2=3 5 * 2 = 10 5 / 2 = 2 why? Division is a special case, because the result must be an integer. So, the fraction part is truncated, and only the integer is stored. Eyad Taqieddin 3
  • 4.
    Example what is theresult of the following? 1) program example Implicit none Real :: x= 2.0 , y Integer :: n = 2, j y= 1/x j= 1/n print *, ‘y=‘ , y print *, ‘j=‘, j end program example Eyad Taqieddin 4
  • 5.
    Priority Rules 1) Parenthesis 2)All exponentiations are performed first, consecutive exponentiations are performed from right to left. 3) All Multiplications and divisions are performed next, in the order in which they appear from left to right. 4) The additions and subtractions are performed last, in the order in which they appear from left to right. Eyad Taqieddin 5
  • 6.
    Example  2**3**2 = 2**9= 512  10-8-2 = 2 – 2 = 0  2+4/2 +5**3 = 2+ 4/2+125=2+2+125 = 129 Eyad Taqieddin 6
  • 7.
    Example   ((-b) + sqrt(B**2 – 4*a*c))/(2*a) ((-b) + sqrt( B**2 – 4*a*c))/2*a do u think these expressions are equal?? Eyad Taqieddin 7
  • 8.
    Mixed Mode Expressions Expressionsthat involve different types of numeric operands Int + Real = Real Int - Real = Real Int * Real = Real Int / Real = Real The integer value is converted to its real equivalent. Eyad Taqieddin 8
  • 9.
    Example    2.0/5  2.0/5.0 0.4 3.0 + 6/4  3.0 + 1  3.0 +1.0  4.0 3.0 + 6.0/4 3.0+ 6.0/4.0  3.0 +1.5  4.5 from the second and third expressions we see why it is not a good programming practice to use mixed mode operations. Eyad Taqieddin 9
  • 10.
    Functions Fortran provides libraryfunctions for many mathematical operations and functions. Ex: ABS(X) returns the absolute value of X Sqrt(X) returns the square root of X Sin(X) returns the sine of X radians Eyad Taqieddin 10
  • 11.
    The Assignment Statement Form Variable= Expression   Variable is a valid Fortran identifier. Expression may be a constant, another varialble to which a value has previously been assigned or a formula to be evaluated. Eyad Taqieddin 11
  • 12.
    Example    Length = 6 assignsthe value 6 to the memory location length. C= x**2 + 5*x + 7 calculates the expressions (x**2 + 5*x + 7) and assigns the result to the memory location C. Salary = Salary + 1000 adds the value 1000 to the value stored in the memory location salary and then stores the result in the same memory location. The previous value of salary is lost. Eyad Taqieddin 12
  • 13.
    Integer Vs. Real   Ifan integer-valued expression is assigned to a real variable, the integer value is converted to a real constant and then assigned to the variable. temp = (6+2)/4 “assuming temp is real” temp will be assigned the value 2.0 not 2 If a real-valued expression is assigned to an integer variable, the fractional part is truncated and the integer part is assigned to the variable. temp = 2.36 / 2 “assuming temp is integer” temp will be assigned the value 1 not 1.18 Eyad Taqieddin 13