1 / 23

Fortran: Genel Bilgiler

Fortran: Genel Bilgiler. Dr. Mürtezaoğlu. ENF 102 Bilgisayar Programlama Dili. Fortran 90. Introduction. Introduction What is a Computer? What is Hardware and Software? Telling a Computer What To Do Some Basic Terminology How Does Computer Memory Work? Numeric Storage

Télécharger la présentation

Fortran: Genel Bilgiler

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. Fortran: Genel Bilgiler Dr. Mürtezaoğlu

  2. ENF 102 Bilgisayar Programlama Dili Fortran 90

  3. Introduction • Introduction • What is a Computer? • What is Hardware and Software? • Telling a Computer What To Do • Some Basic Terminology • How Does Computer Memory Work? • Numeric Storage • Programming Languages • High-level Programming Languages • An Example Problem • An Example Program • Analysis of Program • A Closer Look at the Specification Part • A Closer Look at the Execution Part • How to Write a Computer Program • A Quadratic Equation Solver - The Algorithm • A Quadratic Equation Solver - The Program • A Quadratic Equation Solver - The Testing Phase • Points Raised • Bugs - Compile-time Errors • Bugs - Run-time Errors • Compiler Switches

  4. Introduction • What is a Computer? • A simple computer may look like this: • memory (RAM) -- used to store values during execution of a program, • CPU (Central Processor Unit) -- does the `work', • disc drive -- `permanently' stores files, • keyboard -- allows user to input information, • VDU -- visually outputs data,

  5. Introduction • What is Hardware and Software? • A computer system comprises hardware and software. • Hardware is the physical medium, for example: • circuit boards • processors • keyboard • Software are computer programs, for example: • operating system • editor • compilers • a Fortran 90 program

  6. Introduction • Telling a Computer What To Do • To get a computer to perform a specific task it must be given a sequence of unambiguous instructions or a program. • We meet many examples of programs in everyday life, for example, instructions on how to assemble a bedside cabinet. These instructions are generally numbered, meaning that there is a specific order to be followed, they are also (supposed to be) precise so that there is no confusion about what is intended: • insert the spigot into hole `A', • apply glue along the edge of side panel, • press together side and top panels • attach toggle pin `B' to gromit `C' • ... and so on • If these instructions are not followed `to the letter', then the cabinet would turn out wonky.

  7. Introduction • Some Basic Terminology • It is necessary to cover some terminology. Hopefully, much of it will be familiar -- you will hear many of the terms used throughout the course. • Bit is short for Binary Digit. Bits have value of 1 or 0, (or on or off, or, true or false), • 8 Bits make up 1 Byte, 1024 Bytes make up 1 KByte (1 KiloByte or 1K), (``Why 1024?'' I hear you ask. Because . • 1024 KBytes make up 1 MByte (1 MagaByte or 1M), • 1024 MBytes make up 1 GByte (1 GigaByte or 1G), • all machines have a wordsize -- a fundamental unit of storage, for example, 8-bits, 16-bits, etc. The size of a word (in Bytes) differs between machines. A Pentium based machine is 32-bit. • a flop is a floating point operation per second. A floating point operation occurs when two real numbers are added. Today, we talk of megaflops or even gigaflops. • parallel processing occurs when two or more CPUs work on solution of the same problem at the same time.

  8. Introduction • How Does Computer Memory Work? • Here the wordsize is 8-bits: • A computers memory is addressable, • each memory location will contain some sort of `value', • Each location has a specific `number' (represented as hexadecimal [base-16], e.g., 3F2C), • Fortran 90 allows (English) names to be given to memory locations, • the value of a location can be read from or written to. • The CPU can say, `fetch the contents of memory location 3F2C' or `write this value to location 3AF7'.

  9. Introduction • Numeric Storage • In general, there are two types of numbers used in Fortran 90 programs INTEGER s (whole numbers) and REAL s (floating point numbers). • INTEGER s are stored exactly, often in range (-32767, 32767). • REAL s are stored approximately. • They are partitioned into a mantissa and an exponent, • Exponent can only take a small range of values. • You can get numeric exceptions: • overflow -- exponent is too big, • underflow -- exponent is too small. • In Fortran 90 you can decide what numeric range is to be supported. • CHARACTER s are stored differently.

  10. Introduction • Programming Languages • Programming languages must be: • totally unambiguous (unlike natural languages, for example, English), • expressive -- it must be fairly easy to program common tasks, • practical -- it must be an easy language for the compiler to translate, • simple to use. • All programming languages have a very precise syntax (or grammar). • This ensures all syntactically-correct programs have a single meaning.

  11. Introduction • High-level Programming Languages • Assembler code is a Low-Level Language. • Fortran 90, FORTRAN 77, ADA, C and Java are High-Level Languages. • a program is a series of instructions to the CPU, • could write all programs in assembler code but this is a slow, complex and error-prone process, • high-level languages are more expressive, more secure and quicker to use, • the high-level program is compiled (translated) into assembler code by a compiler.

  12. To convert from F (Fahrenheit) to C (Centigrade) we can use the following formula: To convert from C to K (Kelvin) we add 273. The program would accept a Fahrenheit temperature as input and produce the Centigrade and Kelvin equivalent as output. PROGRAM Temp_Conversion IMPLICIT NONE INTEGER :: Deg_F, Deg_C, K PRINT*, "Please type in the temp in F“ READ*, Deg_F Deg_C = 5*(Deg_F-32)/9 PRINT*, "This is equal to", Deg_C, "C" K = Deg_C + 273 PRINT*, "and", K, "K" END PROGRAM Temp_Conversion This program, called Temp.f90, can becompiled: chad2-13{adamm} 26> f90 Temp.f90 NAg Fortran 90 compiler v2.2. New Debugger: 'dbx90' and run: chad2-13{adamm} 27> a.out Please type in the temp in F 45 This is equal to 7 C and 280 K Introduction

  13. Introduction • Analysis of Program • The code is delimited by PROGRAM ... END PROGRAM statements. Between these there are two distinct areas. • Specification Part • specifies named memory locations (variables) for use, • specifies the type of the variable, • Execution Part • reads in data, • calculates the temp in C and K and • prints out results.

  14. Introduction • A Closer Look at the Specification Part • IMPLICIT NONE -- this should always be present. Means all variables must be declared. • INTEGER :: Deg_F, Deg_C, K -- declares three INTEGER (whole number) variables. Other variable types: • REAL -- real numbers, e.g., 3.1459, , • LOGICAL -- take vaules .TRUE. or .FALSE., • CHARACTER -- contains single alphanumeric character, e.g., 'a', • CHARACTER(LEN=12) -- contains 12 alphanumeric characters, a string, • Fortran 90 is not case sensitive. • K is the same as k and INTEGER is the same as integer.

  15. Introduction • A Closer Look at the Execution Part • This is the part of the program that does the actual `work'. • PRINT*, "Please type in the temp in F" -- writes the string to the screen, • READ*, Deg_F -- reads a value from the keyboard and assigns it to the INTEGER variable Deg_F, • Deg_C = 5*(Deg_F-32)/9 -- the expression on the RHS is evaluated and assigned to the INTEGER variable Deg_C, • * is the multiplication operator, • - is the subtraction operator, • / is the division operator, (takes longer than *) • = is the assignment operator. • PRINT*, "This is equal to", Deg_C, "C" -- displays a string on the screen followed by the value of a variable (Deg_C) followed by a second string ("C"). • By default, input is from the keyboard and output to the screen.

  16. Introduction • How to Write a Computer Program • There are 4 main steps: • specify the problem, • analyse and break down into a series of steps towards solution, • write the Fortran 90 code, • compile and run (i.e., test the program). • It may be necessary to iterate between steps 3 and 4 in order to remove any mistakes. • The testing phase is very important.

  17. Introduction • A Quadratic Equation Solver - The Algorithm • The problem Write a program to calculate the roots of a quadratic equation of the form: • The roots are given by the following formula • The algorithm • READ values of a, b and c, • if a is zero then stop as we do not have a quadratic, • calculate value of discriminant • if D is zero then there is one root: , • if D is > 0 then there are two real roots: and , • if D is < 0 there are two complex roots: and , • PRINT solution.

  18. Introduction • A Quadratic Equation Solver - The Program • PROGRAM QES • IMPLICIT NONE INTEGER :: a, b, c, D • REAL :: Real_Part, Imag_Part • PRINT*, "Type in values for a, b and c" • READ*, a, b, c • IF (a /= 0) THEN ! Calculate discriminant • D = b*b - 4*a*c IF (D == 0) THEN ! one root • PRINT*, "Root is ", -b/(2.0*a) ELSE IF (D > 0) THEN! real roots • PRINT*, "Roots are",(-b+SQRT(REAL(D)))/(2.0*a),& "and", (-b-SQRT(REAL(D)))/(2.0*a) ELSE ! complex roots • Real_Part = -b/(2.0*a) • ! D < 0 so must take SQRT of -D • Imag_Part = (SQRT(REAL(-D))/(2.0*a)) • PRINT*, "1st Root", Real_Part, "+", Imag_Part, "i" • PRINT*, "2nd Root", Real_Part, "-", Imag_Part, "i" • END IF • ELSE ! a == 0 PRINT*, "Not a quadratic equation" • END IF • END PROGRAM QES

  19. Introduction • A Quadratic Equation Solver - The Testing Phase • The output from the program is as follows: • uxa{adamm} 35> a.out • Type in values for a, b and c • 1 -3 2 Roots are 2.0000000 and 1.0000000 • uxa{adamm} 36> a.out • Type in values for a, b and c • 1 -2 1 Root is 1.0000000 • uxa{adamm} 37> a.out • Type in values for a, b and c • 1 1 1 • 1st Root -0.5000000 + 0.8660254 i • 2nd Root -0.5000000 - 0.8660254 i • uxa{adamm} 38> a.out • Type in values for a, b and c • 0 2 3 Not a quadratic equation • Its can be seen that the `test data' used above exercises every line of the program. This is important in demonstrating correctness.

  20. Introduction • Points Raised • The previous program introduces some new ideas, • comments -- anything on a line following a ! is ignored, • & -- means the line is continues, • IF construct -- different lines are executed depending on the value of the Boolean expression, • relational operators -- == (`is equal to') or > (`is greater than'), • nested constructs -- one control construct can be located inside another. • procedure call -- SQRT(X) returns square root of X. • type conversion -- in above call, X must be REAL. In the program, D is INTEGER, REAL(D) converts D to be real valued. • To save CPU time we only calculate the discriminant, D, once.

  21. Introduction • Bugs - Compile-time Errors • In previous program, what if we accidentally typed: • Rael_Part = -b/(2.0*a) • the compiler generates a compile-time or syntax error: • uxa{adamm} 40> f90 Quad.f90 • NAg Fortran 90 compiler v2.2. • Error: Quad.f90, line 16: • Implicit type for RAEL_PART • detected at RAEL_PART@= • Error: Quad.f90, line 24: • Symbol REAL_PART referenced but never set • detected at QES@<end-of-statement> • [f90 terminated - errors found by pass 1]

  22. Introduction • Bugs - Run-time Errors • If we had typed • Real_Part = -b/(.0*a) • then the program would compile but we would get a run-time error, • uxa{adamm} 43> a.out • Type in values for a, b and c • 1 1 1 • *** Arithmetic exception: Floating divide by zero - aborting • Abort • It is also possible to write a program that gives the wrong results!

  23. Introduction • Compiler Switches • Compiler is invoked by f90 • f90 Quad.f90This: • checks the program syntax • generates executable code in a file a.out • Switches or flags can be supplied to compiler: • -o < output-filename >: gives executable a different name, for example, Quad • f90 Quad.f90 -o Quad-time: report execution times f90 Quad.f90 -o Quad –timeFor more information about the compiler type: • man f90

More Related