1 / 32

Chapter 2

Chapter 2. Basic Fortran. Attendance Requirements. Attendance is required for both class and lab. hours Minimum %70 for class attendance Minimum %80 for lab. Attendance This is one and only requirement for taking the final exam. Text Books. Main text book of the class: Programming in F

Télécharger la présentation

Chapter 2

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 BasicFortran

  2. Attendance Requirements • Attendance is required for both class and lab. hours • Minimum %70 for class attendance • Minimum %80 for lab. Attendance • This is one and only requirement for taking the final exam.

  3. Text Books • Main text book of the class: Programming in F T.M.R. ELLIS, Ivor R. PHILLIPS, Addison-Wesley, 1998 • Auxiliary text book: Essential Fortran Loren P. Meissner, PWS Publishing, 1997

  4. 2.1 Data types • There are five basic data types in fortran • 1) INTEGER • 2) REAL • 3) COMPLEX • 4) CHARACTER • 5) LOGICAL Numerical-data types Strings of characters Non-numerical data types Logical data values

  5. Integers • An Integer is a whole number (positive, negative, or zero) and does not contain commas or a decimal point. Examples for valid integer numbers are: 1 137 -1126 +17735 the followings are the examples for invalid integer number: 8,675 (Commas are not allowed in numerical constants) 26.0 (Integer constants may not contain decimal points) --5 (Only one algebraic sign is allowed) 7- (The algebraic sign must precede the string of digits.)

  6. Reals • A real constant must contain a decimal point, but no commas are allowed. Valid real constants are: 1.623 -0.03275 +55765. invalid real constants 15,627 (commas are not allowed in numerical constants.) 786 (Real constants must contain a decimal point.) a real constant can be represented by an exponential. An exponent written as the letter E with an integer constant following. For example: 6527.4684 may also be written as 6.5274684E3 which means 6.5274684X103 or similarly; 65.274684E2 0.65274684E4 and 65274.684E-1

  7. Complex • A complex number is represented as a pair of real (i.e., floating point) numbers. The first component of the pair represents the real part of the complex data object, and the second represents the imaginary part. For example; Z = a + i b Z = (a,b) NOTE: Since you don’t have enough background on complex numbers, you are not fully responsible from complex numbers and its related examples. But, you should now as much as taught.

  8. Character strings • Character constants, also called strings, are sequences of symbols from the ANSI standard character set for Fortran. • The number of a character constant between double quotes is the length of the constant For example: ”PDQ123-A” (Character constant of length 8) ”John Q. Doe” (Character constant of length 11)

  9. Logical type • An object of logical type has the value true or false, or (0 or 1), or (yes or no).

  10. Arithmetic operators in F OperatorMeaning + Addition - Substraction * Multiplication / Division ** Exponentiation (or ‘rising the power of’)

  11. Arithmetic operator priorities OperatorPriority ** High * and / Medium + and - Low Examples: W=c/d*b Total=2**3+5*2=18 W=x+z-y

  12. Application • How to download and install the F_W95.EXE ? 1) Web page of the Class 2) Network neighborhood --> Bim -> Ntserver -> F 3) ftp://install@160.75.2.100 password is install READING Study pages 38-49 (Ellis & Philips’s book)

  13. Names & Declarations • A data object is a constant that never changes or a variable that can change during program execution. • Data object may have names. For example, Average, X, Y, Einstein, or Potential_Energy. Names in a program must conform to 3 rules: 1) A name may contain up to 31 letters, digits, and underscore characters 2) The first character of a name must be a letter 3) Imbedded blank characters are not permitted in a name IMPORTANT: keywords such as program, write, and end are not actually names

  14. Type Declarations • Every variable and named constant must appear in a type declaration • The type of a Fortran variable determines the type of value that may be assigned to that variable. • In every F program, the specification statement implicit none must immediately follow the program statement program Research implicit none . . . end program Research Type Declaration Form Type name ::List of names Consider the following example

  15. Type Declarations implicit none integer :: Counts, Loop_Index real :: Current, Resistance, Voltage Names defined as part of the F language, including keywords and intrinsic function names (such as sin, tan, abs, etc.), must be written in lower case. Names that you invent can use any combination of upper and lower case, but each name must be written consistently.

  16. Type properties: Kind & Length Kind : A variable of any numerical type has a kind type parameter, which designates a subtype or variant of the type. • Each type has a default computer representation • For each numerical data type, F defines a set of integers to be used as kind type parameter values (i.e., the number 4 for real representation, number 8 for the higher-precision variant) Length : A variable of character data type has a string length property. • A character type declaration must specify string length A type declaration appears in parentheses after the type name. If no kind parameter is specified, F selects the default computer representa- tion Type name (Type properties) :: List of names

  17. Type properties: Kind & length • Other data attributes may be specified between the type properties and the double colon. Type name (Type properties), Attributes :: List of names Example: integer, parameter :: ARRAY_SIZE=12, SL=20 character (Len=SL), save :: Element_Name integer, dimension (ARRAY_SIZE) :: chart, list Exercise 1:Do exercises 1.2 on page 42 (Meissner’s book)!.

  18. Constants • A constant in a program may have an explicit form, or it may be represented by a name. EXPLICIT CONSTANTS A constant in a program has a fixed value during the execution of the program. Consider the cases that: 1) A value may need to be changed before the program is executed again. 2) A constant in a declaration, such as the size of an array or the length of a character string, may need to be revised. Therefore we prefer to use a named constant instead of an explicit constant to prevent searching for all the appearances of a certain constant within the program which is a tedious and an error-prone task.

  19. Constants • The name of a constant looks like the name of a variable and it must be listed in the type declaration • The keyword parameter designates a named constant • Houdini Principle: Don’t use magic numbers • use a named constant rather than a explicit constant • give always explanations ( use !)

  20. Declaration for a Named Constant • Declaration of a named constant is as follows: Type name, parameter :: List of initializations where each list item has the form Name = Value definition The value definition is an explicit constant. Examples: integer, parameter :: LENGTH=12 real, parameter :: PLANK=6.6260755e-34, PI=3.141593 real, parameter :: GRAVITY=9.807, AVAGADRO=6.0221367e23, & twoPI=2.0*PI integer, parameter :: A=20, HIGH=30, NEON=67 character (Len=2), parameter :: units=”Cm” ATTENTION: Continuation line with ampersand symbol.

  21. Exercise Exercise2: • Do exercises 1.3 on pages 46-47 (Meissner’s book). • Study pages between 49-65 (Ellis & Philips’s book).

  22. Simple Input & Output Read (unit = *, fmt = *) Input List Write (unit = *, fmt = *) Output List • An asterisk as the unit in a read or write control list designates the default input device (the keyboard) or the default output device (The terminal screen) • An asterisk as the format designates list-directed formatting. Input data values for on-line list-directed input are entered at the computer keyboard in free form. Consecutive values must be separated by blanks. For example: read (unit = *, fmt = *) Radii, I, Current, Top can be entered as 9.75 10 15.32 765.3

  23. Simple Input & Output • IMPORTANT: The items in an input list must be variable names. write (unit = *, fmt = *) ” Please enter the diameter of a circle” read (unit = *, fmt = *) Diameter write (unit = *, fmt = *) ” Diameter = ”, Diameter, ”circumference =”, & 3.1416*Diameter, ”Area = ”, 3.1416*Diameter**2

  24. NUMBER REPRESENTATION NUMERICAL PRESICION AND RANGE • There is an increasing demand for high precision • Computer hardware representation varies from 8-bit to 128 bit word length • Precision requirements should be considered for both different type of computer hardware and problem type we have. For example: 9 decimal digits of numerical precision can be satisfied by 1) Single-precision arithmetic if the computer word length is 64 bits, 2) double-precision arithmetic if the word length is 32 bits. • A Kind option provides flexible control of integer and real precision and range. • 1) Each data type has a default computer representation • 2) The kind type parameter of a data object determines its computer representation

  25. NUMBER REPRESENTATION • 3) Each kind is designated by a different integer kind selector value. • 4) A kind value may be specified in the type declaration for a variable or named constant. • A typical convention is that the kind selector value is the number of bytes (e.g., 4 or 8) occupied by the computer representation. • But it is recommended that these processor-dependent values not be used as kind selectors because of the problems that result when a program is moved to a different processor. • Therefore use named constants instead of explicit integer constants The intrinsic inquiry function selected_real_kind(P) returns the kind value for a processor representation that supplies at least P decimal digits of precision.

  26. NUMBER REPRESENTATION Example: selected_real_kind(6) returns a processor kind value that provides at least 6 decimal digits of precision A numerical constant may be followed by an underscore and a kind selector: 678_SHORT 12_LONG 3.141592_HIGH The kind selector must be a named constant of integer type EXAMPLES: integer, parameter :: NORMAL = selected_int_kind(9), & LOW = selected_real_kind(6), HIGH = selected_real_kind(15) integer (kind = NORMAL) :: First, Second

  27. NUMBER REPRESENTATION real (kind = LOW), parameter :: FOURTH = 12.0 Exercise3: Do the example 1.4 on page 58 to see the difference in kind selection Meissner’s book.

  28. Mixed-mode assignment • Assume that, b is a real variable whose value is 100.0, while c and d are integers having the values 9 and 10, respectively. a = b*c/d result is 90.0 a = c/d*b a gets 0 value. This phenomenon is known as integer division

  29. Application program • show programlist_directed_inputexample ! • Compile • Run

  30. Application program program list_directed_input integer :: int_1, int_2, int_3 real :: real_1, real_2, real_3 ! Initialize all variables int_1 = -1 int_2 = -2 int_3 = -3 real_1 = -1.0 real_2 = -2.0 real_3 = -3.0 ! Read data read *, int_1,real_1,int_2,real_2,int_3,real_3 ! Print new values print *, int_1,real_1,int_2,real_2,int_3,real_3 end program list_directed_input

  31. Program style and design A program must be correct, readable, and understandable. The basic principles for developing a good program are as follows: 1) Programs cannot be considered correct until they have been validated using test data. 2) Programs should be well structured • Use a top-down approach when developing a program for a complex problem. • Strive for simplicity and clarity 3) Each program unit should be documented • Include opening documentation • Use comments • Use meaningful identifiers • Label all output 4) A program should be formatted in a style that enhances its readability

  32. Program style and design 5) Programs should be readable and understandable • Do not use magic numbers • Use comments to describe the purpose of a program and variables 6) Programs should be general and flexible

More Related