1 / 39

Chapter 2 – Data Types

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

krauser
Download Presentation

Chapter 2 – Data Types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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

  2. Data Types INTEGER : numbers without decimal points REAL : numbers with dec points (scientific notation) COMPLEX : complex numbers CHARACTER : strings of text LOGICAL : either .TRUE. or .FALSE.

  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

  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

  5. Variables Declare variables as REAL :: x INTEGER :: n Assign values to variables as x = 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!!!!

  6. Equals is NOT “Equals” Assignment statements are written r1 = (-b + SQRT(b**2 – 4.0*a*c))/(2.0*a) This is not a mathematical “equality” statement Think of this as “r1 gets the value yada yada” r1(-b + SQRT(b**2 – 4.0*a*c))/(2.0*a)

  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

  8. Safe Programming Practice Variables can be declared and assigned an initial value all at once. REAL :: pi = 3.14159 instead of REAL :: pi pi = 3.14159 Or they can be declared to be constants REAL,PARAMETER :: pi = 3.14159 This latter makes illegal any later assignment like pi = 1.56789

  9. Issues DO NOT EVER do the following REAL :: 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?

  10. The End

  11. Arithmetic and Assignments Assignments are not “equals” Most expressions x = some reasonably legal expression are in fact legal in Fortran but they may not produce the result you expect CAUTION: Juxtaposition for multiplication won’t work. The expression “a times b” must be written a * b and not a b

  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 right Proper parentheses on numerators and denominators Constant is 2.0, not just 2 All the mults use the * Exp is “star star” and use exponent 2, not 2.0 The SQRT calls the Fortran builtin function SAFE PROGRAMMING: don’t be cute or clever; use spaces and parentheses to make the expression readable

  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-liner If the expression is complicated and would take multiple lines, it is probably better to spread it out Many errors come from misplaced parentheses Any decent, modern, compiler will produce code that is just as efficient

  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 (roundingdown to minus infinity or truncatingtowardzero) CHECK YOUR COMPILER if in doubt

  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?

  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)

  17. Buell’s Rules IN ORDER OF IMPORTANCE • Get it right • Know what the problems might be from being cute, and don’t be cute • Know where to look up the precise rules If 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.

  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

  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

  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

  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?

  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

  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

  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

  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

  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

  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

  28. The End

  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 numbers Fortran 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

  30. Coding Standards INDENT!!!!! Use meaningful variable names Dull, boring, and correct is ok. Clever, fancy, and incorrect is not ok. Be consistent—this will help you find bugs Declare variables by type, in alpha order? Sort subroutines in alpha order? Open and close structures with comments or labels

  31. What’s In a Program? Header Specification/declaration statements Executable statements Subprograms End of program statement “Hello, world” program

  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

  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

  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

  35. Executable In the executable section of the program are the statements that actually perform the computation “Hello, world” program

  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

  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

  38. Pages 100 ff These pages detail the Fortran up till now Study them carefully Pages 105ff contain hints, good ideas, etc.

  39. The End

More Related