04-arithmetic_operations
TRANSCRIPT
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 1/12
CE 311 K - Introduction to
Computer Methods
Arithmetic Operations and
Intrinsic Functions
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 2/12
Assignment Statements
Assigns a value to a variable
vari abl e=const ant, arithmetic expression, or string
When a variable has not yet been assigned a value in the program, it is
said to be ³undefined´.
After the first value is assigned, the variable is said to be ³initialized´.
A variable can only store one value or string at a time. If a new value or
string is assigned to the variable after it is first initialized in a program,
the new value replaces the old one.
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 3/12
Character Strings
ExampleCHARCTER (LEN=6):: STATE49, STATE50
STATE50=µHAWAII¶
STATE49=µMICHIGAN´
When a variable does not yet have a value, it is said to be ³undefined´
Rules: ± If the declared length and the string size are the same, all characters are
stored in the character variable
± If the declared length is longer than the string, then blank ³padding´ will beadded to the right of the string to equalize the lengths.
± If the declared length is shorter than the string length, the string is truncated.
± No arithmetic operations
± So what is stored for STATE50 and STATE49?
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 4/12
Arithmetic Expressions
All ( ) are evaluated first. Innermost () are evaluated first in
expressions that contain nested ( ).
Operator Precedence1. exponentiation (**)
2. multiplication and division (*,/)
3. addition and subtraction (+,-) : Can be used as a sign, but standard
Fortran 90 does not allow two operands to appear side by side
Left Associative: Operators at the same precedence level are evaluated
from left to right, but consecutive exponentiation operators are
evaluated from right to left.
No division by zero or raising a zero value operand to a negative orzero valued ower.
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 5/12
Truncation vs. Rounding
Truncation: When computer stores a realnumber in an integer variable, it ignores thefractional portion and stores only the wholenumber portion of that real number.
e.g.) If Integer = 3.75, then Integer = 3
Rounding: The approximation of a real
number to an integer closest in value to thethat real number when writing to an output.
e.g.) If Real = 3.75 (F4.0), then Real = 4.e.g.) If Real = 3.75 (F4.1), then Real = 3.8
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 6/12
Mixed-mode Operation
The result of an operation involving two integer
numbers is an integer number.
e.g.) MEAN= (N1+N2)/2
If N1= 2 and N2= 3, then MEAN = 2
The result of an operation involving two real
numbers is a real number.
e.g.) AVE= (N1+N2)/2.0If N1= 2.0 and N2= 3.0, then AVE = 2.5
If N1= 2 and N2= 3, then AVE = 2.5 (= 5/2.0)
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 7/12
Mixed-mode Operation
Mixed-mode operation is an operation
involving both an integer value and a real
value. The intermediate results is a realvalue.
e.g.) P E R IM= 4×SIDE
If SIDE =2.2, then P
E R
IM = 8.8e.g.) ROOT = NUM**0.5
If NUM = 9, then ROOT = 3.0
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 8/12
A Case where Mixed-mode Operation is
Desired
A R EA=SIDE**2
Calculated as
SIDE×SIDE
If SIDE = 5.0, then
A R EA= 25.0
A R EA=SIDE**2.0
Performed by arithmetic
logic unit (ALU) using³logarithm.´
Calculated as
antilog(2.0×log( SIDE ))
If SIDE = 5.0, then A R
EA= 24.99999 (small error
introduced with logarithm)
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 9/12
Another Case where Mixed-mode
Operation is Desired
(-2.0)**2 is a valid
operation.
Performed as:
(-2.0)*(-2.0)= 4.0
(-2.0)**(2.0) is not a
valid operation.
Performed as:
antilog(2.0×log(-2.0))
Does not exist!
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 10/12
A Case where Mixed-mode Operation is
NOT Desired
V OLUM= (4/3)*3.14159* R**3
Final result (V OLUM ) will be a real
number.
Intermediate result of (4/3) is an integer
number (4/3= 1 but not 1.3333)
Solution: useV OLUM=(4.0/3.0)*3.14159* R**3 instead
8/7/2019 04-arithmetic_operations
http://slidepdf.com/reader/full/04-arithmeticoperations 12/12
Intrinsic Functions
³Built-in´ module of code provided with theFortran compiler
Certain operations that are used frequently
variable=NAME OF FUNCTION(argument)
± Argument can be a constant, variable, or expression
± Separate arguments by commas if there is morethan one
± Some functions are generic, others are data typespecific
± Can nest functions, but enclose in parentheses