Please check the website below for lecture notes, HWs and project assignments, - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

Please check the website below for lecture notes, HWs and project assignments,

Description:

ARRAY_SIZE and SL have parameter attribute. Element_Name has save ... kind defines the properties of integer, real and complex variables. kind (real variables) ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 44
Provided by: Makc9
Category:

less

Transcript and Presenter's Notes

Title: Please check the website below for lecture notes, HWs and project assignments,


1
  • Please check the website below for lecture notes,
    HWs and project assignments,
  • http//mimoza.marmara.edu.tr/byilmaz

2
FORTRAN - Lecture 2
  • Data types and Operations

3
Data Representation
  • Bits (0 and 1)
  • Bytes (8 bits)
  • Words (more than 1 byte)

4
Data types
  • numeric data types
  • real and integer types
  • complex type
  • non-numeric data types
  • character type
  • logical type

5
Data types
  • integer number
  • whole number which has no decimal point
  • (fixed point representation)
  • examples
  • 1
  • -24
  • 324
  • 0

6
Data types
  • integer number
  • the amount of memory devoted to storing integer
    depends on computer configuration.
  • 4 bytes integers are the most common, i.e.,
  • The smallest int. -2n-1-1
  • The biggest int. 2n-1
  • Note Attempts to use an integer larger than the
    largest or smaller than the smallest value result
    in error.

7
Data types
  • real number
  • number with decimal fractions - has a decimal
    point
  • (floating point or scientific representation)
  • Speed of sound 299 800 000 m/s 2.998 x 108
  • examples
  • 1.23456 0.123456e1 -0.012 1. 2e-2
  • 1234. 1.234e3

8
Data types
  • real number
  • 0.2998 X 109
  • mantissa exponent
  • The real numbers are stored in a computer in a
    similar way.
  • Ex
  • 4-byte computer 24 bits (mantissa) - 8
    (exponent)real_num mantissa x 2 exponent
  • 24 bits mantissa 223 7 significant decimal
    digits
  • 8 bits exponent ranges between 2-128 and 2127
    10-38 to 1038

9
Data types
  • real number
  • Ex 12345678.9 will be stored in 12345680.0
  • 1 000 000.0 and 1 000 000.9 is same
  • The difference is called ROUNd OFF error.
  • Ex Find the the max and minimum values can be
    stored in a 2 byte computer.

10
Data types
  • complex number
  • represented as a pair of real (floating point)
    numbers
  • first component represents the real part
  • second component represents the imaginary
    part of a complex data
  • examples
  • (6.94, -5.9) (3.942, 2.5e-3)

11
Data types
  • character
  • sequence of symbols from the Fortran character
    set - enclosed between double or single quotes
  • Character data consists of
  • 26 uppercase and 26 lowercase letters from A to Z
  • 10 digits 0-9
  • Common symbols (-_/ etc.
  • examples A character 123456
  • 1234abc

12
Data types
  • character
  • a character data is stored in 1 byte
  • The particular bit values may vary from computer
    to computer depending on the coding system used
    for the characters
  • ASCII (Am. Stand. Code for Information
    Interchange)
  • EBCDIC
  • UNICODE (2 byte)

13
Data types
  • logical
  • has the value
  • .TRUE. or .FALSE.

14
Program Structure
  • Heading
  • program, function, subroutine, module
  • Specification statements
  • implicit-type / explicit-type declarations
  • Executable statements
  • Internal subprograms
  • End statement

15
Program Structure
  • One Fortran line can contain 132 characters in
    Fortran 90/95, if longer lines are needed
    symbol can be used to divide the line
  • Ex output input1 input2
  • output input1
  • input2
  • ! is used for comments, when used the line is
    converted to a non-executable statement

16
Example
  • program celsius_to_fahrenheit
  • ! A program to convert a Celsius temperature to
    Fahrenheit
  • ! Variable declarations
  • real temp_C, temp_F
  • ! Ask for Celsius temperature
  • print , What is the Celsius temperature?
  • read , temp_C
  • ! Convert it to Fahrenheit
  • temp_F 9.0temp_C/5.0 32.0
  • ! Print both temperatures
  • print , temp_C, temp_F
  • end program celsius_to_fahrenheit

17
Constants and Variables - Names
  • A data object is either a constant that never
    changes or a variable that can change during
    program execution.
  • A variable occupies storage inside the computer
  • A numerical constant, a character constant, or a
    logical constant in a program may have an
    explicit form.
  • Alternatively a constant may be represented by a
    name

18
Constants and Variables - Names
  • Data object names
  • a name may contain up to 31 letters (upper case
    or lower case characters), digits and underscore
    characters
  • first character must be a letter
  • examples
  • X
  • Kinetic_Energy
  • aBc_defG1789

19
Type declarations
  • form
  • type name list of names
  • examples
  • integer Counts, Flag
  • real Speed, Distance, Time
  • complex Case
  • character First_name, Last_name
  • logical control

20
Type properties
  • form
  • type name (type properties) list of names
  • kind and length of a data object
  • examples
  • character (len10) First_name, Last_name

21
Type attributes
  • form
  • type name (type properties), attributes list
    of names
  • examples
  • integer, parameter ARRAY_SIZE 12, SL 20
  • character (lenSL), save Element_Name
  • integer, dimension(ARRAY_SIZE) Chart, List
  • Element_Name has string length 20
  • ARRAY_SIZE and SL have parameter attribute
  • Element_Name has save attribute
  • Chart and List have dimension attribute
    (dimension is 12)

22
Properties
  • Properties takes these keywords
  • len used for defining the length of characters
  • kind defines the properties of integer, real
    and complex variables
  • kind (real variables)
  • 1 single precission (approx. 7 significant
    digits, usually stored in 32 bits)
  • 2 double precission (approx. 14 significant
    digits, usually stored in 64 bits)

23
Properties
  • kind (integers)
  • 1 1 byte integer (range is from -27 to 27 - 1)
  • 2 2 byte integer (range is from -215 to 215 -
    1)
  • 3 4 byte integer (range is from -231 to 231 -
    1)
  • 4 8 byte integer (range is from -263 to 263 - 1)
  • constant data types can be assigned by placing
    an underscore followed by the value of its kind
  • 1234325424_4
  • also means integer number of type 4

24
Attributes
  • dimension points an array of variable
  • parameter states that its value is used as a
    constant
  • save saves the variable value - in function or
    subroutine returns to calling program
  • allocatable used for dynamic storage of variables
  • pointer used for accessing memory locations
  • target used for pointing memory for pointers
  • public declaration of variable that other
    subprograms or main program can access
  • private declaration of variables that no other
    program can use

25
Implicit none
  • this statement should be placed at the beginning
    of the declarations
  • every variable and named constant used in the
    program should then be defined by a type
    declaration

26
Initial values for variables
  • initially a variable has no value (it is
    undefined)
  • initial values can be specified in a type
    declaration
  • real a 1.25, b 2.5, c 5.0
  • during program execution a variable can be
    defined if it appears on the left side of an
    assignment statement
  • Variable name Expression

27
Assignment statement
  • mass 100.
  • Velocity 12.5
  • Kinetic_Energy 0.5 mass Velocity 2
  • Assignment is not a statement of algebraic
    equality
  • I I 1

28
Constants
  • named constants can be initialized within a
    declaration list
  • integer, parameter ARRAY_SIZE 12
  • the keyword parameter designates a named
    constant
  • explicit constants are given in statements
  • it is a good practice to use capital letters for
    all named constants
  • ARRAY_SIZE, PI, UNIV_GAS_CONSTANT

29
Constants
  • Houdini Principle
  • do not use magic numbers (other than 0 and 1)
  • use a named constant rather than an explicit
    constant
  • always explain the constants used by comment
    lines
  • (in comment line, the first nonblank character
    is an exclamation mark, !)

30
Arithmetic operations
  • addition and subtraction
  • X 5.0
  • J - 4
  • A B - C
  • multiplication and division
  • X 2.0
  • A / B
  • Old_vol ( Old_pres / New_pres ) ( New_temp /
    Old_temp )
  • exponentiation
  • A 2.0
  • 0.5 Mass Velocity 2

31
Operators
  • Operator Operation
  • addition, (plus ? identity)
  • - subtraction, (minus ? negation)
  • multiplication
  • / division
  • exponentiation
  • two operators must not appear side by side in an
    expression
  • A ( - B )

32
Importance of numerical data types
  • When all operands are real, the result type is
    real - when all operands are integers, the result
    type is integer
  • in integer division, the result is truncated to
    the next smaller integer
  • 9 / 5 ? 1
  • 9.0 / 5.0 ? 1.8
  • when a real expression is raised to an integer
    power, the result type is real
  • A 5 ? AAAAA ? A 5.0 less efficient

33
Arithmetic operations
  • examples
  • to calculate F ( 9 C / 5 ) 32
  • F 9.0 C / 5.0 32.0
  • to calculate a b2 c2
  • A B2 C2

34
Arithmetic operator priorities
  • Operator Priority
  • low
  • - low
  • medium
  • / medium
  • high

35
Priorities
  • Subexpressions enclosed in parentheses are
    computed first - innermost parenthesized
    subexpression is evaluated first
  • ( 5.0 3.0 ) / 2.0 ? 4.0
  • arithmetic operations are performed according to
    the operator priorities
  • 5.0 3.0 / 2.0 ? 6.5

36
Priorities
  • operations are performed in order from left to
    right - however two or more consecutive
    exponentiation operations are performed in order
    from right to left
  • 5.0 3.0 / 2.0 ? 7.5
  • 5.0 3.0 2.0 ? 59
  • function evaluation takes precedence over all
    other operations
  • abs ( A ) B / C
  • sqrt ( 4.0 ) 3.0 / 2.0 ? 3.0

37
Logical Calculations
  • Form
  • logical var. name logical expression
  • Relational Oper.s Combinational Oper.s
  • equal .AND.
  • / not equal .OR.
  • gt Greater than .EQ.
  • gt greater equal .NOT.
  • lt less than
  • lt less and equal
  • Note staments are executed from left to right

38
Cahracter Variables
  • Character (len3)ff1, ff2
  • ff1f ! ff1 f
  • ff2file01 ! ff2 fil
  • Concatenation Operator (//) combines two or more
    strings into a single large string,
  • Ex character(len10)a
  • character(len8)b,c
  • aABCDEFGHIJ
  • b12345678
  • ca(13)//b(45)//a(68)
  • cABC45FGH

39
Functions
  • type conversion functions
  • X real ( I 3 ) / 3.0
  • J int ( X ) I
  • numerical utility functions
  • abs (X) absolute value
  • max (X, 0.0, Z) produces largest argument value
  • min (J, K) produces smallest argument value
  • sign (1.0, X) transfers sign of 1st argument
    to the 2nd
  • modulo (K, J) gives remainder from division

40
Functions
  • mathematical functions
  • acos inverse cosine
  • asin inverse sine
  • atan inverse tangent
  • atan2 inverse tangent
  • cos cosine
  • cosh hyperbolic cosine
  • exp exponential
  • log natural logarithm
  • log10 common logarithm

41
Functions
  • mathematical functions (continued)
  • sin sine
  • sinh hyperbolic sine
  • sqrt square root
  • tan tangent
  • tanh hyperbolic tangent

42
Input and output
  • read statements
  • read , input-list
  • read (unit ,fmt ) input-list
  • write statements
  • print , output-list
  • write (unit , fmt ) output-list

43
Example
  • problem
  • write a program to read a Celsius temperature
    and convert it to Fahrenheit using the formula,
  • F ( 9 C / 5 ) 32
  • analysis
  • write down a structure plan first
  • read Celsius temperature (temp_C)
  • convert to Fahrenheit (temp_F)
  • print both temperatures

44
Example
  • solution
  • program celsius_to_fahrenheit
  • ! A program to convert a Celsius temperature to
    Fahrenheit
  • ! Variable declarations
  • real temp_C, temp_F
  • ! Ask for Celsius temperature
  • print , What is the Celsius temperature?
  • read , temp_C
  • ! Convert it to Fahrenheit
  • temp_F 9.0temp_C/5.0 32.0
  • ! Print both temperatures
  • print , temp_C, temp_F
  • end program celsius_to_fahrenheit
Write a Comment
User Comments (0)
About PowerShow.com