Tải bản đầy đủ (.ppt) (320 trang)

Fortran 95 2003 for scientists and engineers

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (864.24 KB, 320 trang )

Fortran 95-2003 for Scientists and
Engineers
Transparencies prepared by Anthony
T. Chronopoulos
CHAPTER 1
CS1073
Chapter 1 Intro. to Computers and the Fortran Language
The Computer
In summary, the major components of the computer are:
CPU
Main Memory
Secondary Memory
Input Devices (e.g. keyboard, tapes etc)
Output Devices (e.g. display, monitor, tapes etc).
Chapter 1 Intro. to Computers and the Fortran Language
The CPU
The Central Processing Unit is "heart“ (or better “brain”) of a computer.
The CPU consists of three main parts:
The Control Unit - coordinates activities of the computer
The Arithmetic Logic Unit (ALU) - performs the calculations
Registers - store a small amount of data and instructions
Chapter 1 Intro. to Computers and the Fortran Language

Main Memory (RAM)
It is larger than the Registers and smaller than the hard drive.
It temporarily stores the program currently being run by the computer and also the data
required by the programs.
RAM is volatile, i.e. when power is interrupted, then what was stored in RAM is lost.
Chapter 1 Intro. to Computers and the Fortran Language
Secondary Memory
It is a permanent (non-volatile) storage.


The hard drive is the best example, but also USB, CDs, tapes, etc. The size of hard drives is
larger than that of RAM. However, accessing data stored on a hard drive (or other
secondary memory) takes much longer (5-10 times) than from RAM.
Chapter 1 Intro. to Computers and the Fortran Language
Computer Languages
Each computer has its own machine language (Assembly) used to execute very basic
operations. Operations are: load and store (data, to and from memory), and add, subtract,
multiply, or divide.
The problem with a machine language is that it is very difficult to program and use, and also
it can be unique for a computer.
Chapter 1 Intro. to Computers and the Fortran Language
Thus, computer scientists design high-level language
that are easy to program and use. The programs must
be converted to a machine-language (by compilers and
linkers) for the computer to run them. Examples are
Fortran, C, C++, Java etc.
The benefit of a high-level language (e.g. Fortran) is that a program and can be compiled on
any machine that has a Fortran compiler.
Chapter 1 Intro. to Computers and the Fortran Language
Data Representation in a Computer
Data is represented by millions of switches, each of which can be either ON (1) or OFF (0). 0
and 1 are the two binary digits (bits).
We use a combination of 0's and 1's to represent the other numbers (and characters as
well).The smallest common combination of bits (0's and 1's) is called a byte. A byte is a
group of 8 bits. A word is a group of 2, 4, or 8 bytes. Our PCs have 4-byte words.
Chapter 1 Intro. to Computers and the Fortran Language

The Binary Number System
The number system used by humans is the decimal system. The decimal system is a base=10
system.

There are 10 digits (0-9). Each digit in a number represents a power of 10.
The number 221 is:
2 * 10^2+ 2 * 10^1 + 1 * 10^0
(where 10^i is 10 raised to exponent i =0,1,2, ).
Chapter 1 Intro. to Computers and the Fortran Language
Similarly, converting a number from binary (base 2) to decimal (base 10).
The number 101:
1 * 2^2+ 0 * 2^1 + 1 * 2^0 = 4 +0+ 1 = 5
If n bits are available, then those bits can represent 2^n possible values.
e.g. One byte (n=8 bits) can represent 256 possible values. Two bytes (n=16 bits) can
represent 65,536 possible values.
Chapter 1 Intro. to Computers and the Fortran Language
Which of the following ranges would best describe the possible values of a byte?
(1) 0 , , 255 (2) 1 , , 256 (3) -127 , , 128
(4) -128 , , 127 . The answer is (4).
The reason is that the computer must store both
positive and negative integers. The following
example illustrates how this can be achieved.
Chapter 1 Intro. to Computers and the Fortran Language
Example from the car odometer/milometer. (Note: content not in Book
will not be used in Exams)
Milometer readings during travel.
(a) 0 0 0 0 0 0 0 0 initial milometer reading
(b) 0 0 0 0 0 0 0 2 after two miles
(c) 0 0 0 0 0 0 0 1 after reversing one mile
(d) 0 0 0 0 0 0 0 0
after reversing one more
mile
(e) 9 9 9 9 9 9 9 9
after reversing one more

mile
Chapter 1 Intro. to Computers and the Fortran Language
e.g. Storage of 8-digit integers, in base=10 system:
(a) 5 0 0 0 0 0 0 0 represents -50 000 000
(b) 5 0 0 0 0 0 0 1 represents -49 999 999
(c) 9 9 9 9 9 9 9 9 represents -1
(d) 0 0 0 0 0 0 0 0 represents 0
(e) 0 0 0 0 0 0 0 1 represents +1
(f) 4 9 9 9 9 9 9 9 represents +49 999 999
Chapter 1 Intro. to Computers and the Fortran Language
e.g. Storage of (4-bit) integers:
1000 -8
1001 -7
1110 -2
1111 -1
0000 0
0001 +1
0010 +2
0111 +7
When using the binary system, the effect is that
if the first binary digit (or bit) is a one then the number is negative,
while if it is zero the number is positive.
Chapter 1 Intro. to Computers and the Fortran Language

Two other number systems that are also useful are octal (base 8) and hexadecimal (base
16).

Table 1-1 shows the conversion between these systems for the decimals: 0,1, …,15
Chapter 1 Intro. to Computers and the Fortran Language
Types of Data

Three common types of data are stored in a computer's memory: character, integer, real
Each type has unique characteristics and takes up a different amount of memory.
Chapter 1 Intro. to Computers and the Fortran Language
Character Data
A typical system for representing character data may include the following:
Letters: A through Z and a through z
Digits (0, 1, 2, 3, 4, 5, 6, 7, 8, 9)
Miscellaneous symbols, e.g. (", ', ?, ., <, >, =, %, &)
Chapter 1 Intro. to Computers and the Fortran Language

In the past, it has been common to use just one byte of memory to represent a maximum of
256 characters (ASCII).

To represent characters found in many languages, one can use use 2 bytes of memory
which allows 65,536 possible characters (Unicode).
Chapter 1 Intro. to Computers and the Fortran Language
Integer Data
Integer data ( e.g. -1, -355, 0, 1993) are represented exactly on computers. However, only a
finite number can be stored. Most machines will use 4 bytes of memory to represent
integers.
Smallest n-bit integer: -2^(n-1)
Largest n-bit integer: 2^(n-1) - 1
e.g. n=32 for 4-byte numbers.
Chapter 1 Intro. to Computers and the Fortran Language
With 4 bytes of memory we can represent numbers in the approx. range [-2.15*billion,
2.15*billion].
Outside this range we get arithmetic overflow.
Integer data limitations: (1) No fractional parts.
(2) It is not possible to represent very large positive integers or very small negative integers.
Chapter 1 Intro. to Computers and the Fortran Language

Real Data
The real data type stores numbers in a format similar to scientific notation.
For example, the speed of light in a vacuum is about 299,800,000 meters per second. The
number in scientific notation would be 2.998 * 10^8 (m/s) meters per second.
Chapter 1 Intro. to Computers and the Fortran Language
A format similar to scientific notation will be used to represent real numbers in FORTRAN.
Real numbers occupy 4 bytes of memory. These 4 bytes will be divided as follows:
3 byte for the fraction (or mantissa)
1 byte exponent
The mantissa will be a number between -1 and 1.
The exponent will contain the power of 2 required to scale the number to its actual value.
Integer numbers are stored in the computer as they are (e.g. 1321 ).
The real numbers are converted. Consider any non-zero real number as
a fraction lying between 0.1 and 1.0, called the mantissa, which is multiplied
or divided by 10 a certain number of times, where this number is called the
exponent. e.g. 17877.0 (in fraction, base=10 form): 0.17877x10^5
Summary: Numbers represented on the computers
10
b
a ×

exponent
mantissa
The same technique as was used for integers to distinguish positive and
negative numbers will be used for both the mantissa and the exponent.
Chapter 1 Intro. to Computers and the Fortran Language
Precision and Range
Precision refers to the number of significant digits that can be preserved in a number (on a
computer).
Based on the number of bits (or bytes) in the mantissa,

3 byte mantissa gives about 7 significant digits (between 1.0 and -1.0) e.g. 12345678.4 is stored
as
12345678.0. The difference (i.e. 0.4) is called the round-off error.

×