lee-06-chap7
Download
Report
Transcript lee-06-chap7
Chapter 7
Simple Data Types
20061129 chap7
Objectives
No programming language can
predefine all the data types that a
programmer may need.
C allows a programmer to create new
data types.
Simple Data Type: a data type used to
store a single value.
20061129 chap7
2
Representation and Conversion
of Numeric Types
int vs. double: why having more than one
numeric type is necessary?
Can the data type double be used for all
numbers?
Operations involving integers are faster than
those involving numbers of type double.
Operations with integers are always precise,
whereas some loss of accuracy or round-off
error may occur when dealing with double
numbers.
20061129 chap7
3
Internals Formats
All data are represented in memory as binary strings.
Integers are represented by standard binary numbers.
For example, 13 = 01101
Doubles are represented by two sections:
mantissa and exponent.
real number = mantissa * 2exponent
e.g. 4.0 = 0010 * 20001 = 2 * 21
20061129 chap7
4
Implementation-Specific Ranges
for Positive Numeric Data
Run demo (Fig. 7.2)
20061129 chap7
5
Integer Types in C
20061129 chap7
6
Floating-Point Types in C
20061129 chap7
7
Numerical Inaccuracies
Certain fractions cannot be represented
exactly in the decimal number system
e.g., 1/3= 0.33333……
The representational error (round-off error)
will depend on the number of binary
numbers in the mantissa.
20061129 chap7
8
Example: Representational
Error
for (trial = 0.0;
trial != 10.0;
trial = trial + 0.1) {
….
}
Adding 0.1 one hundred times in not exactly 10.0.
The above loop may fail to terminate on some
computers.
trail < 10.0: the loop may execute 100 times on one
computer and 101 times on another.
It is best to use integer variable for loop control
whenever you can predict the exact number of times
a loop body should be repeated.
20061129 chap7
9
Manipulating Very Large and Very
Small Real Numbers
Cancellation Error: an error resulting from applying an
arithmetic operation to operands of vastly different
magnitudes; effect of smaller operand is lost.
Arithmetic Underflow: an error in which a very small
computational result is represented as zero.
e.g., 1000.0 + 0.0000001234 is equal to 1000.0
e.g., 0.00000001 * 10-1000000 is equal to 0
Arithmetic Overflow: a computational result is too large.
e.g., 999999999 * 109999999 may become a negative value on
some machines.
20061129 chap7
10
Automatic Conversion of Data Types
The data of one numeric type may be
automatically converted to another numeric types.
int
k = 5,
m = 4;
n;
double x = 1.5,
y = 2.1,
z;
20061129 chap7
11
Explicit Conversion of Data Types
C also provides an explicit type
conversion operation called a cast.
e.g.,
int n = 2, d = 4;
double frac;
frac = n / d;
frac = (double) n / (double) d;
frac = (double) (n / d);
20061129 chap7
//frac = 0.0
//frac = 0.5
//frac = 0.0
12
Representation and Conversion of char
Character values can be compared by
the equality operators == and !=, or by
the relational operators <, <=, >, and >=.
e.g., letter = ‘A’;
if (letter < ‘Z’) …
Character values may also be compared,
scanned, printed, and converted to type
int.
20061129 chap7
13
ASCII (American Standard Code for
Information Interchange)
Each character has its own unique numeric code.
A widely used standard is called American Standard
Code for Information Interchange (ASCII). (See
Appendix A in the textbook)
The printable characters have codes from 32 to 126, and
others are the control characters.
For example, the digit characters from ‘0’ to ‘9’ have
code values from 48 to 57 in ASCII.
The comparison of characters (e.g., ‘a’<‘c’)
depends on the corresponding code values in ASCII.
20061129 chap7
14
Print Part of the Collating
Sequence (Fig. 7.3)
20061129 chap7
15
Enumerated Types
Good solutions to many programming
problems require new data types.
Enumerated type: a data type whose
list of values is specified by the
programmer in a type declaration.
20061129 chap7
16
Accumulating Weekday Hours Worked
(Fig 7.5)
20061129 chap7
17
Iterative Approximations
Numerical Analysis: to develop algorithms for
solving computational problems.
Finding solutions to sets of equations,
Performing operations on matrices,
Finding roots of equations, and
Performing mathematical integration.
Many real-world problems can be solved by
finding roots of equations.
20061129 chap7
18
Six Roots for the Equation f(x) = 0
Case Study: Bisection Method for
Finding Roots
20061129 chap7
19
Function Parameters
The bisection routine would be far more useful if
we could call it to find a root of any function.
Declaring a function parameter is accomplished
by simply including a prototype of the function in
the parameter list.
20061129 chap7
20
Case Study: Bisection Method for
Finding Roots
First, tabulate function values to find an
appropriate interval in which to search
for a root.
20061129 chap7
21
Bisect this interval
Three possibilities that wrise when the
Iinterval [xleft, xright] is Bisected
20061129 chap7
22
Epsilon
A fourth possibility is that the length of
the interval is less than Epsilon.
Epsilon is a very small constant.
In this case, any point in the interval is
an acceptable root approximation.
20061129 chap7
23
Finding a Function Root Using
the Bisection Method
Run demo
20061129 chap7
24
Figure 7.11 Sample Run of
Bisection Program with Trace
Code Included
20061129 chap7
25
Homework #8
Due: 2006/12/9
複數運算
以長度 2 的一維陣列 ( float [2] ) ,來表示複數,並實作出加
減乘除、次方 ( 根號 ) 的運算,為強化乘除的計算,本題的乘
除、次方 ( 根號 ) 運算需使用極座標系統 ( 複數的 乘法 、 除
法 以及 指數 以及開方運算,在極坐標中會比在直角坐標中容
易得多,請見reference 的複數部份說明 ) 。
作業要求 :
1. 使用者輸入二對 X,Y 代表二複數 a = (X 1 +iY 1 ), b = (X 2
+iY 2 )
2. 計算出 a+b
3. 計算出 a/b
4. 使用者輸入欲計算 a 次方大小 (exp)
5. 計算出 a 的 exp 次方
20061129 chap7
26
Summary
Representation and Conversion of
Numeric Types
Representation and Conversion of
Type Char
Enumerated Types
Iterative Approximations
20061129 chap7
27