1 / 27

Simple Data Types and Numeric Representations in C

This chapter explores simple data types, numeric representations and conversions in C. Topics include the need for multiple numeric types, representation of integers and doubles, numerical inaccuracies, manipulation of large and small real numbers, automatic and explicit conversion of data types, and representation and conversion of characters.

gohara
Télécharger la présentation

Simple Data Types and Numeric Representations in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 7 Simple Data Types

  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. 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. 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

  5. Implementation-Specific Ranges for Positive Numeric Data • Run demo (Fig. 7.2)

  6. Integer Types in C

  7. Floating-Point Types in C

  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. 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. 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 * 109999999 may become a negative value on some machines.

  11. 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;

  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. 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.

  14. 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.

  15. Print Part of the Collating Sequence (Fig. 7.3)

  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. Accumulating Weekday Hours Worked (Fig 7.5)

  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. Six Roots for the Equation f(x) = 0 • Case Study: Bisection Method for Finding Roots

  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. Case Study: Bisection Method for Finding Roots • First, tabulate function values to find an appropriate interval in which to search for a root.

  22. Bisect this interval • Three possibilities that wrise when the Iinterval [xleft, xright] is Bisected

  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. Finding a Function Root Using the Bisection MethodRun demo

  25. Figure 7.11 Sample Run of Bisection Program with Trace Code Included

  26. 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 次方

  27. Summary • Representation and Conversion of Numeric Types • Representation and Conversion of Type Char • Enumerated Types • Iterative Approximations

More Related