Chapter 2 – Data Types
description
Transcript of Chapter 2 – Data Types
CSCE 206
1
Chapter 2 – Data Types
We start now to deal with details of “syntax”
Yes, you need to know syntax
OTOH, it’s in the book, if you can find it.
Prudent Programming Practice:• learn to do it right• don’t push the envelope on the rules• think: 80% get it right, 20% know the rules• sometimes (usually?) vanilla is better
CSCE 206
2
Data Types
INTEGER : numbers without decimal pointsREAL : numbers with dec points (scientific
notation)COMPLEX : complex numbersCHARACTER : strings of textLOGICAL : either .TRUE. or .FALSE.
CSCE 206
3
Symbols
Book calls these things “identifiers”
I will probably call these things “variable names” or “symbols”
Lots of fussy rules
Identifiers:• must start with a letter• must be only letters, numerals, or underscore• must be lessequal 31 characters long
CSCE 206
4
Case Sensitivity
Fortran is NOT, repeat NOT, “case sensitive”
ThisSymbol is the same as THISSYMBOL is the same as thissymbol is the the same as tHiSsYmBoL
CSCE 206
5
Variables
Declare variables asREAL :: x
INTEGER :: n
Assign values to variables asx = 3.14159
x = 0.314159E01
n = -23
Variables that are declared but not yet assigned a value are undefined (ANSI) although some compilers will assign zero
DO NOT RELY ON INITIAL ASSIGNMENT!!!!
CSCE 206
6
Equals is NOT “Equals”
Assignment statements are writtenr1 = (-b + SQRT(b**2 – 4.0*a*c))/(2.0*a)
This is not a mathematical “equality” statementThink of this as
“r1 gets the value yada yada”r1(-b + SQRT(b**2 – 4.0*a*c))/(2.0*a)
CSCE 206
7
Safe Programming Practice
Fortran historically has said that variables beginning with i through n are integers and all else are reals by default
This would let you avoid declaring variables
DO NOT DO THIS! NOT EVER!! NEVER!!!
A vast number of programming errors are typos of variable names
Let the compiler catch these errors.Use the IMPLICIT none
CSCE 206
8
Safe Programming Practice
Variables can be declared and assigned an initial value all at once.
REAL :: pi = 3.14159
instead ofREAL :: pi
pi = 3.14159
Or they can be declared to be constantsREAL,PARAMETER :: pi = 3.14159
This latter makes illegal any later assignment like
pi = 1.56789
CSCE 206
9
Issues
DO NOT EVER do the followingREAL :: x = 43.79 ! initial assignment
part 1 code here
x = 23.9
part 2 code here
Either make the variable a parameter and assign it a value once for all time, or else assign it a value with an assignment statement
What’s going to happen the second time you execute the part 1 code? Is that your final answer?
CSCE 206
10
The End
CSCE 206
11
Arithmetic and Assignments
Assignments are not “equals”Most expressionsx = some reasonably legal expression
are in fact legal in Fortranbut they may not produce the result you expect
CAUTION: Juxtaposition for multiplication won’t work.
The expression “a times b” must be writtena * b and not a b
CSCE 206
12
Arithmetic and Assignments
r1 = (-b + SQRT(b**2 – 4.0*a*c))/(2.0*a)
Exp, (mult and div), (add and sub), left to rightProper parentheses on numerators and
denominatorsConstant is 2.0, not just 2All the mults use the *Exp is “star star” and use exponent 2, not 2.0The SQRT calls the Fortran builtin function
SAFE PROGRAMMING: don’t be cute or clever; use spaces and parentheses to make the expression readable
CSCE 206
13
Arithmetic and Assignments
Don’t be cute or clever; code for correctness and readability
num = -b + SQRT(b**2 – 4.0*a*c)
den = 2.0*a
root = num/den
gets the job done just as well as the one-linerIf the expression is complicated and would take
multiple lines, it is probably better to spread it out
Many errors come from misplaced parenthesesAny decent, modern, compiler will produce code
that is just as efficient
CSCE 206
14
Integer Arithmetic
If you divide two integers, the fraction is thrown away
4/5 results in 0
14/5 results in 2
(-14)/5 could perhaps be -3 or -2 (rounding down to minus infinity or truncating toward zero)
CHECK YOUR COMPILER if in doubt
CSCE 206
15
Mixed-Mode Arithmetic
An expression with variables of different types (e.g., integers with reals) is called mixed-mode
Variables are promoted (INTEGER to REAL, specifically) before the expression is evaluated
But be careful! What about
x = (2/3) * 4.0
Would this truncate 2/3 to 0 and then multiply?
CSCE 206
16
Mixed-Mode Arithmetic
When you really need mixed mode arithmetic, be explicit – use the intrinsic functions such as
INT(expression)
REAL(expression)
so that you can see specifically what you have asked the machine to do
This is sometimes called a cast of one type to another (because that’s what the C language calls it)
CSCE 206
17
Buell’s Rules
IN ORDER OF IMPORTANCE
1. Get it right2. Know what the problems might be from
being cute, and don’t be cute3. Know where to look up the precise rulesIf you practice 1)-3) religiously, then you can
usually avoid 4)
The compiler/computer will always tell you what it’s going to do if there’s a question.
CSCE 206
18
Floating-Point (i.e. REAL) Arithmetic
Remember “significant digits” from science?0.01 * 0.01 = 0.00
because 0.01 * 0.01 = 0.0001 but we have only allowed two significant digits
Similar rules apply with computers and Fortran
The machine will calculate a number as a result
Only you will know whether the result makes sense
CSCE 206
19
Computer Arithmetic
Digital logic normally works in on or off mode
So computers work in binary arithmetic
Some effort must be made to deal with conversion from binary to decimal and back again
CSCE 206
20
Computer Arithmetic
Numbers are represented as a finite set of digits in binary
Fractions, base n, have terminating representations only if the prime divisors of the denominator are divisors of n
½ = 0.5, 1/5 = 0.2, 1/16 = 0.0625, 1/20 = 0.05
all terminate, but 1/3 = 0.33333… does not
Binary is even more restrictive: denominators must be powers of 2
Even 1/10 does not have a terminating rep in binary
CSCE 206
21
IEEE 754 Standard
(Virtually) all floating point arithmetic is now done using the IEEE 754 standard—this specifies what the results of floating point arithmetic should be
Prior to 754, chaos reigned
The IBM 360 couldn’t multiply by 1.0 correctly
Every machine had its little quirks
Cray: Do you want the right answer? Or do you want an answer fast?
CSCE 206
22
IEEE 754
Floating point is always slightly inaccurate due to finite precision and to representation in binary
One of the demands of IEEE 754 is that the result of a numerical operation should be the closest representable number to the number that would be the result if there were infinite precision
If you check compiler options, they will have many IEEE 754 possibilities, because accurate means slow, and some of the accuracy issues are mostly for debugging purposes
CSCE 206
23
32-Bit Floating Point Numbers
1 bit for sign
8 bits for exponent in excess 127 notation(2**128 ~ 10**38)
23 bits for mantissa with an implied leftmost 1(2**24 = 16777216 ~ 10**7)
So we get 6 for sure, and sometimes 7, decimal places of accuracy
The dynamic range is about 10**(-38) to 10**38
CSCE 206
24
64-Bit Floating Point Numbers
1 bit for sign
11 bits for exponent in excess 1023
52 bits for mantissa with an implied leftmost 1(2**53 ~ 9*(10**15))
So we get 15 for sure, and sometimes 16, decimal places of accuracy
The dynamic range is about 10**300
CSCE 206
25
32-Bit Integers
Smallest integer is -2**31
Largest integer is 2**31 – 1 = 2147483647
So we get 9 for sure, and sometimes 10, decimal places of accuracy
CSCE 206
26
64-Bit Integers
Smallest integer is -2**63
Largest integer is 2**63 – 1 = 9 223 372 036 854 775 807
So we get 18 for sure, and sometimes 19, decimal places of accuracy
CSCE 206
27
Numbers in General
Fortran used to have only 32-bit and 64-bit numbers.
Now you can specify exactly what accuracy you want
However, we will concentrate on 32 bits and 64 bits because most machines deal in those numbers and thus most code is written for these sizes
CSCE 206
28
The End
CSCE 206
29
Fortran Requirements
Used to be “fixed format” card punching• Columns 1-5 for statement numbers• Column 6 to indicate continuation• Columns 7-72 for statements• Columns 73-80 for card sequence numbersFortran 90 can be “free format”• Ampersand (&) indicates continuation• Statement numbers not needed (except for FORMAT)
• Spaces (after the first space) don’t count except in a FORMAT string
aware of the old rules in case you see old code
CSCE 206
30
Coding Standards
INDENT!!!!!Use meaningful variable namesDull, boring, and correct is ok.Clever, fancy, and incorrect is not ok.Be consistent—this will help you find bugsDeclare variables by type, in alpha order?Sort subroutines in alpha order?Open and close structures with comments or
labels
CSCE 206
31
What’s In a Program?
Header
Specification/declaration statements
Executable statements
Subprograms
End of program statement
“Hello, world” program
CSCE 206
32
Header and Trailer
All programs start with a
PROGRAM this_is_the_program_name
Statement and they should all end with an
END PROGRAM this_is_the_program_name
statement
CSCE 206
33
Subprograms
Not everything should be done “in line” in a program.
Subprograms perform specific tasks
“Modular” programs are more readable and more likely to be correct
CSCE 206
34
Specification/Declaration
All symbols have a meaning in the program (variable names, function names, etc.)
In the specification/declaration section we tell the compiler the context in which we will be using the symbols.
E.g., an integer variable
CSCE 206
35
Executable
In the executable section of the program are the statements that actually perform the computation
“Hello, world” program
CSCE 206
36
Input and Output
PRINT *, list of variables
WRITE(6,*) list of variables
WRITE(6,nn) list of variables
nn FORMAT(format statement here)
The first two are “free format” and the last pair is a specifically formatted output statement
By default, “unit 5” is stdin for reading and “unit 6” is stdout for writing
CAVEAT: Fortran reads an entire line with each READ
CSCE 206
37
Input and Output
With free format output, (the *), you get scientific notation if needed, adequate default precision, etc., with little programming hassle
But you don’t get columns lined up neatly
The purpose of the FORMAT statement is to let you specify the precision and widths so that columnar output looks pretty
We’ll get to formatting later
CSCE 206
38
Pages 100 ff
These pages detail the Fortran up till now
Study them carefully
Pages 105ff contain hints, good ideas, etc.
CSCE 206
39
The End