Fortran: Genel Bilgiler - PowerPoint PPT Presentation

1 / 23
About This Presentation
Title:

Fortran: Genel Bilgiler

Description:

A Closer Look at the Specification Part. A Closer Look at the Execution Part ... They are partitioned into a mantissa and an exponent, ... – PowerPoint PPT presentation

Number of Views:41
Avg rating:3.0/5.0
Slides: 24
Provided by: kine4
Category:

less

Transcript and Presenter's Notes

Title: Fortran: Genel Bilgiler


1
Fortran Genel Bilgiler
  • Dr. Mürtezaoglu

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

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
Introduction
  • 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 be compiled
  • chad2-13adamm 26gt f90 Temp.f90 NAg Fortran 90
    compiler v2.2. New Debugger 'dbx90' and run
  • chad2-13adamm 27gt a.out Please type in the temp
    in F 45 This is equal to 7 C and 280 K

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(LEN12) -- 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 gt 0 then there are two real roots and
    ,
  • if D is lt 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 bb - 4ac IF (D 0) THEN ! one root
  • PRINT, "Root is ", -b/(2.0a) ELSE IF (D gt 0)
    THEN ! real roots
  • PRINT, "Roots are",(-bSQRT(REAL(D)))/(2.0a),
    "and", (-b-SQRT(REAL(D)))/(2.0a) ELSE ! complex
    roots
  • Real_Part -b/(2.0a)
  • ! D lt 0 so must take SQRT of -D
  • Imag_Part (SQRT(REAL(-D))/(2.0a))
  • 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
  • uxaadamm 35gt a.out
  • Type in values for a, b and c
  • 1 -3 2 Roots are 2.0000000 and 1.0000000
  • uxaadamm 36gt a.out
  • Type in values for a, b and c
  • 1 -2 1 Root is 1.0000000
  • uxaadamm 37gt 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
  • uxaadamm 38gt 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 gt
    (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.0a)
  • the compiler generates a compile-time or syntax
    error
  • uxaadamm 40gt f90 Quad.f90
  • NAg Fortran 90 compiler v2.2.
  • Error Quad.f90, line 16
  • Implicit type for RAEL_PART
  • detected at RAEL_PART_at_
  • Error Quad.f90, line 24
  • Symbol REAL_PART referenced but never set
  • detected at QES_at_ltend-of-statementgt
  • f90 terminated - errors found by pass 1

22
Introduction
  • Bugs - Run-time Errors
  • If we had typed
  • Real_Part -b/(.0a)
  • then the program would compile but we would get a
    run-time error,
  • uxaadamm 43gt 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.f90 This
  • checks the program syntax
  • generates executable code in a file a.out
  • Switches or flags can be supplied to compiler
  • -o lt output-filename gt gives executable a
    different name, for example, Quad
  • f90 Quad.f90 -o Quad-time report execution
    times f90 Quad.f90 -o Quad time For more
    information about the compiler type
  • man f90
Write a Comment
User Comments (0)
About PowerShow.com