Вы находитесь на странице: 1из 12

|  




|
  


Arithmetic Operations and


Intrinsic Functions
Assignment Statements
‡ Assigns a value to a variable

‡   
    

 

‡ ½hen 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.
‡
| aracter Strings
‡ Y ample
CHARCTYR (LY STATY, STATY
STATY 
HA½AII¶
STATY
ICHIGA´

½hen 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 be
added to the right of the string to equalize the lengths.
± If the declared length is shorter than the string length, the string is truncated.
± o arithmetic operations

± So what is stored for STATY and STATY

‡
Arit metic Expressions
‡ All (  are evaluated first. Innermost ( are evaluated first in
e pressions that contain nested ( .

‡ Operator Precedence
1. e ponentiation (**
. multiplication and division (*,/
. addition and subtraction (+,-  Can be used as a sign, but standard
Fortran  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 e ponentiation operators are
evaluated from right to left.

‡ o division by zero or raising a zero value operand to a negative or


zero valued power.
£runcation vs. Rounding
‡ Truncation ½hen computer stores a real
number in an integer variable, it ignores the
fractional portion and stores only the whole
number portion of that real number.
e.g. If   .7, then   

‡ Rounding The appro imation of a real


number to an integer closest in value to the
that real number when writing to an output.
e.g. If Π .7 (F. , then Π .
e.g. If Π .7 (F.1, then Π .8
áixed-mode Operation
‡ The result of an operation involving two integer
numbers is an integer number.
e.g.  
If  and  , then  
‡ The result of an operation involving two real
numbers is a real number.
e.g.  !"
If  . and  . , then   .
If  and  , then   . ( / . 
áixed-mode Operation
‡ i ed-mode operation is an operation
involving both an integer value and a real
value. The intermediate results is a real
value.
e.g. A Œ#$%&
If %&  . , then A Π8.8
e.g. Π'(("!)
If ' , then Π .
A |ase w ere áixed-mode Operation is
Desired
Π%& (( Π%& ((!"
‡ Calculated as ‡ Performed by arithmetic
logic unit (ALU using
%& $%&
³logarithm.´
‡ If %&  . , then ‡ Calculated as
Π . antilog( . log(%& 
‡ If %&  . , then Œ 
. (small error
introduced with logarithm
Anot er |ase w ere áixed-mode
Operation is Desired

‡ (- . ** is a valid ‡ (- . **( .  is not a


operation. valid operation.
‡ Performed as ‡ Performed as
(- . *(- .  . antilog( . log(- . 

Does not exist!


A |ase w ere áixed-mode Operation is
OT Desired
'#(!#)*(Œ((
‡ Final result ( ' will be a real
number.
‡ Intermediate result of (/ is an integer
number (/ 1 but not 1.
‡ Solution use
'#!"!"(!#)*(Œ(( instead
Summary: áixed-mode Operation

‡ Avoided if possible
‡ Declare real variables to perform operations.
‡ The only case where mi ed-mode operation is
desired is
Π
e.g. Π %& **
e.g. !"(( is a valid operation but !"((!"
is not.
›ntrinsic Functions

‡ ³Built-in´ module of code provided with the


Fortran compiler
‡ Certain operations that are used frequently
‡ variableA Y OF FUCTIO(argument
± Argument can be a constant, variable, or e pression
± Separate arguments by commas if there is more
than one
± Some functions are generic, others are data type
specific
± Can nest functions, but enclose in parentheses

Вам также может понравиться