21 March, 2000 CS1001 Lecture 4 Data Types + Algorithms = Programs • Data Types & Arithmetic Errors • Arithmetic Operators and Functions • Program Testing Tips
21 March, 2000
CS1001 Lecture 4
Data Types + Algorithms = Programs
• Data Types & Arithmetic Errors• Arithmetic Operators and Functions• Program Testing Tips
21 March, 2000
Data Types In Fortran
• Integer (0, 137, -2516, +17745)• Real or Single Precision (0.0, -0.01536, 123.4,
1.234E2, -1.536E-2)• Double Precision (1.234D-6, 0.1111111D3)• Complex (3+4i)• Character Strings (‘A’, ‘JOHN DOE’)• Logical (.TRUE., .FALSE.)
21 March, 2000
INTEGER Data Type
• Limited to computer word size• For a 16-bit word size:
0111111111111111 = 215 - 1 = 32767
1000000000000000 = -215 = -32768
• For a 32-bit word size231 - 1 = 2147483647
-231 = -2147483648
21 March, 2000
REAL or Single Precision Data
• Separated into two parts:– Mantissa, or fractional part (24 bits of 32)
– Exponent, or magnitude part (8 bits of 32)
• First bit of each part reserved for sign• Overflow -- too large an exponent• Underflow -- too small an exponent• Range is approximately 10-38 to 10+38
• Approximately 7 significant digits
21 March, 2000
REAL Type (cont’d)
• Sometimes, the internal representation of REAL is not accurate enough
• e.g.,1.00/.1254
1.00e00/1.25e-01 = 8.00e00
instead of 7.97e00
• Cumulating of these roundoffs errors can affect final results.
21 March, 2000
DOUBLE Precision Data Type
• Instead of using one word, use two words.• Separated into two parts:
– Mantissa, or fractional part (instead if about 7 significant digits, you can have about 14)
– Exponent, or magnitude part
– Machine dependent
• First bit of each part reserved for sign• Overflow -- too large an exponent• Underflow -- too small an exponent
21 March, 2000
Arithmetic Error Disaster
Time in system’s internal clock was measured in tenths of seconds. System used 24 bits.
1/10 = 1/2**4+1/2**5+1/2**8 +… = 0.0001100110011001100110011001100…
with 24 bits, 1/10 = 0.00011001100110011001100
error of 0.0000000000000000000000011001100 ... which is about 0.000000095 decimal
In 100 hours, error = 0.000000095*100hr*60min/hr*60sec/min*10 = 0.34sec
21 March, 2000
Arithmetic Error Disaster
A Scud travels at about 1676 meters/sec
0.34 sec * 1676 meters/sec = 570 meters
Patriot missile miss the Scud !
21 March, 2000
COMPLEX Data Type
• Unique to Fortran• Complex numbers are represented as a pair of real
numbers -- (a, b)• First number is the “real” part• Second number is the “imaginary” part• (6.2, 2.4) means 6.2 + 2.4i
21 March, 2000
CHARACTER Data Type
• Has a specific length associated with it• Default is length 1 (single character)• Denoted by “ ” or ‘ ’
– “CS1001” character string of 6 characters long
– ‘All of you are super programmers’
• Usually ASCII encoded which is only 7 bits, but stores as 8 bits– “A” is decimal 65 or 41 hex, and “a” is decimal 97 or
61 hex
21 March, 2000
LOGICAL Data Types
• Boolean variables, or flags, 1-bit data• Value is either .TRUE. or .FALSE. or more
accurately, 0 or 1• Used for binary choices
– True or False
– Set or Clear
– Yes or No
21 March, 2000
Data Declaration• Within the program “specification” part• Uses type statement consisting of:
– type-specifier– list
• type-specifier is INTEGER, REAL, COMPLEX, CHARACTER(n) where n is the number of characters to be stored in identifier , or LOGICAL
• list is a list of identifiersINTEGER:: Numvalues, Factorial, Sum
CHARACTER(6) :: classnumber
classnumber = ‘CS1001’
21 March, 2000
Identifiers & Variables
• Identifiers are used to identify programs, constants, variables, and other entities.
• Variables are used to store computed values• Constants are used to store fixed values• Constants cannot be changed within a program• If a value must change from an initial value,
declare the variable, initialize it to an initial value, then change that value within the program where needed
21 March, 2000
Named Constants
• The PARAMETER statement declares the variable as a constant and associates each specified constant with a constant value:– INTEGER, PARAMETER :: iLimit = 50
– REAL, PARAMETER :: PI = 3.14159
– or REAL, PARAMETER :: rPI = 3.14159
21 March, 2000
Variable Initialization
• Data type and identifier (name) must first be declared with a type statement
• The DATA statement is no longer used• Use an assignment statement for each variable:
INTEGER :: iYear
REAL :: rBodyTemperature
iYear = 1997
rBodyTemperature = 98.6
21 March, 2000
Arithmetic Operations
Binary Unary+ Addition + positive
- Subtraction - negative
* Multiplication e.g., 5.0 ** (-1)
/ Division
** Exponentiation
Precedence Rules:
0. Parenthesis
1. Exponentiation, right to left
2. Multiplication and Division, left to right
3. Addition and subtraction, left to right
Height = 0.5*acceleration*time**2 & + (initialvelocity*time)+initialheight
21 March, 2000
Operations
• Operations result in same type as operands6/5 = 1 integer operation
6.0/5.0 = 1.2
• Mixed-Mode expressions -- combine integer and real datae.g. 1.0/4 => 1.0/4.0 => 0.25
3.0+8/5 => 3.0+1 => 3.0+1.0 => 4.0
3.0+8.0/5 => 3.0+1.6=>4.6
• Generally poor practice
21 March, 2000
21 March, 2000
Arithmetic Functions
• Perform standard operations on variables which are referenced as an argument
• Standard arithmetic functions do not have to be declared, merely used
• Examples: COS(X), SQRT(X)
21 March, 2000
Assignment Statement
variable = expression
e.g., Height = 0.5*acceleration*time**2 &
+ initialvelocity*time+initialheight
Velocity=acceleration*time+initialvelocity• NOT an algebraic equality, but a replacement
statementsum = sum + x means add the value of x to the value of
sum and store the result in sum.
21 March, 2000
Consider Hungarian Notation
• iName for INTEGER variables/constants• rName for REAL• dName for DOUBLE PRECISION• xName for COMPLEX• cName for CHARACTER strings• bName for LOGICAL (boolean)• Constants are all caps except type (rVAL)
21 March, 2000
Program Testing Tips
• Boundary/bounds check all inputs– 1-100 input range, check 1, 100, 101, 0
• Check upper and lower case if applicable• Quadrant test in all four quadrants
– 45, 135, 225, 315 degrees
• Type test data inputs– Enter 10.5 for an INT, 56 for a REAL, etc.
• Have someone else test your program, and reciprocate