Download presentation
Presentation is loading. Please wait.
Published byMariah Banks Modified over 9 years ago
1
20061129 chap7 Chapter 7 Simple Data Types
2
20061129 chap7 2 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.
3
20061129 chap7 3 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.
4
20061129 chap7 4 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 * 2 exponent e.g. 4.0 = 0010 * 2 0001 = 2 * 2 1
5
20061129 chap7 5 Implementation-Specific Ranges for Positive Numeric Data Run demo (Fig. 7.2)
6
20061129 chap7 6 Integer Types in C
7
20061129 chap7 7 Floating-Point Types in C
8
20061129 chap7 8 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.
9
20061129 chap7 9 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.
10
20061129 chap7 10 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. e.g., 1000.0 + 0.0000001234 is equal to 1000.0 Arithmetic Underflow: an error in which a very small computational result is represented as zero. e.g., 0.00000001 * 10 -1000000 is equal to 0 Arithmetic Overflow: a computational result is too large. e.g., 999999999 * 10 9999999 may become a negative value on some machines.
11
20061129 chap7 11 Automatic Conversion of Data Types The data of one numeric type may be automatically converted to another numeric types. intk = 5, m = 4; n; double x = 1.5, y = 2.1, z;
12
20061129 chap7 12 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 = 0.0 frac = (double) n / (double) d; //frac = 0.5 frac = (double) (n / d); //frac = 0.0
13
20061129 chap7 13 Representation and Conversion of char equality operators relational operators 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.
14
20061129 chap7 14 ASCII (American Standard Code for Information Interchange) Each character has its own unique numeric code. American Standard Code for Information Interchange (ASCII) A widely used standard is called American Standard Code for Information Interchange (ASCII). (See Appendix A in the textbook) printable characters control characters 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.
15
20061129 chap7 15 Print Part of the Collating Sequence (Fig. 7.3)
16
20061129 chap7 16 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.
17
20061129 chap7 17 Accumulating Weekday Hours Worked (Fig 7.5)
18
20061129 chap7 18 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.
19
20061129 chap7 19 Six Roots for the Equation f(x) = 0 Case Study: Bisection Method for Finding Roots
20
20061129 chap7 20 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.
21
20061129 chap7 21 Case Study: Bisection Method for Finding Roots First, tabulate function values to find an appropriate interval in which to search for a root.
22
20061129 chap7 22 Bisect this interval Three possibilities that wrise when the Iinterval [xleft, xright] is Bisected
23
20061129 chap7 23 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.
24
20061129 chap7 24 Finding a Function Root Using the Bisection Method Run demo
25
20061129 chap7 25 Figure 7.11 Sample Run of Bisection Program with Trace Code Included
26
20061129 chap7 26 Homework #8 Due: 2006/12/9 複數運算 以長度 2 的一維陣列 ( float [2] ) ,來表示複數,並實作出加 減乘除、次方 ( 根號 ) 的運算,為強化乘除的計算,本題的乘 除、次方 ( 根號 ) 運算需使用極座標系統 ( 複數的 乘法 、 除 法 以及 指數 以及開方運算,在極坐標中會比在直角坐標中容 易得多,請見reference 的複數部份說明 ) 。乘法 除 法 指數 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 次方
27
20061129 chap7 27 Summary Representation and Conversion of Numeric Types Representation and Conversion of Type Char Enumerated Types Iterative Approximations
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.