Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University.

Similar presentations


Presentation on theme: "Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University."— Presentation transcript:

1 Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University of Technology

2 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-2 Representation and Conversion of Numeric Types Simple data type –a data type used to store a single value Differences between numeric types –integers are faster –storage space –loss of accuracy with type double numbers –data represented in memory is computer dependent

3 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-3 Floating-point Format double divided into two sections: the mantissa and the exponent mantissa –between 0.5 and 1.0 for positive numbers and between -0.5 and -1.0 for negative –real number = mantissa X 2 exponent

4 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-4 Figure 7.1 Internal Formats of Type int and Type double

5 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-5 Floating-point Format (Cont’d) Actual ranges vary from one implementation to another The ANSI standard for C specifies the minimum range of positive values of type int is from 1 to 32,767. The minimum range specified for positive values of type double is from 10 -37 to 10 37.

6 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-6 Figure 7.2 Program to Print Implementation- Specific Ranges for Positive Numeric Data

7 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-7 Floating-point Format (Cont’d)

8 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-8 Floating-point Format (Cont’d)

9 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-9 Numerical Inaccuracies Some fractions cannot be represented exactly as binary numbers in the mantissa Representational error –an error due to coding a real number as a finite number of binary digits The representational error (sometimes called roundoff error) will depend on the number of binary digits (bits) used in the mantissa.

10 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-10 Numerical Inaccuracies (Cont’d) The result of adding 0.1 one hundred times may not be exactly 10.0, so the following loop may fail to terminate. for (trial = 0.0; trial != 10.0; trial = trial + 0.1) {... } If the loop repetition test is changed to trial < 10.0, the loop may execute 100 times on one computer and 101 times on another. It is best to use integer variables for loop control

11 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-11 Numerical Inaccuracies (Cont’d) When you add a large real number and a small number, the larger number may “cancel out” the smaller number. 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 arithmetic overflow –an error that is an attempt to represent a computational result that is too large

12 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-12 Automatic Conversion of Data Types int k = 5, m = 4, n; double x = 1.5, y = 2.1, z;

13 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-13 Explicit Conversion of Data Types cast –an explicit type conversion operation Assume int nt, d1; Compare –frac = (double)n1 / (double)d1; –frac = n1 / d1; converting only one would be sufficient, because the rules for evaluation of mixed-type expressions would then cause the other to be converted as well. –Note: assignment is not included in this rule But, frac = (double)(n1 / d1); resulting in the loss of the fractional part!! Why?

14 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-14 Explicit Conversion of Data Types (Cont’d) We sometimes include casts that do not affect the result but simply make clear to the reader the conversions that would occur automatically. Equivalent (int sqrt_m, m;) –sqrt_m = sqrt(m); –sqrt_m = (int)sqrt((double)m);

15 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-15 Representation and Conversion of Type char Character values may also be compared, scanned, printed, and converted to type int. Each character has its own unique numeric code; the binary form of this code is stored in a memory cell. Character Codes in Appendix A: ASCII, EBCDIC, CDC

16 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-16 Representation and Conversion of Type char (Cont’d) Order relationship –'0' < '1' < '2' < '3' < '4' < '5' < '6' < '7' < '8' < '9' –'A' < 'B' < 'C' <... < 'X' < 'Y' < 'Z' –'a' < 'b' < 'c' <... < 'x' < 'y' < 'z' not necessarily consecutive.

17 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-17 Representation and Conversion of Type char (Cont’d) In ASCII, the printable characters have codes from 32 (code for a blank or space) to 126 (code for the symbol ~). The other codes represent nonprintable control characters. –Sending a control character to an output device causes the device to perform a special operation such as returning the cursor to column one. C permits conversion of type char to type int and vice versa. collating sequence –a sequence of characters arranged by character code number

18 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-18 Figure 7.3 Program to Print Part of the Collating Sequence

19 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-19 Enumerated Types Enumerated type –a data type whose list of values is specified by the programmer in a type declaration Example typedef enum {entertainment, rent, utilities, food, clothing, automobile, insurance, miscellaneous} expense_t; expense_t expense_kind;

20 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-20 Enumerated Types (Cont’d) Enumeration constant –an identifier that is one of the values of an enumerated type expense_t causes the enumeration constant entertainment to be represented as the integer 0, rent to 1, utilities to 2, and so on.

21 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-21 Figure 7.4 Enumerated Type for Budget Expenses

22 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-22 Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

23 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-23 Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

24 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-24 Enumerated Types (Cont’d) Enumeration constants must be identifiers; they cannot be numeric, character, or string literals. An identifier cannot appear in more than one enumerated type definition. Relational, assignment, and even arithmetic operators can be used with enumerated types.

25 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-25 Enumerated Types (Cont’d)

26 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-26 Enumerated Types (Cont’d) Example –sunday < monday –wednesday != friday –tuesday >= Sunday Example day_t tomorrow; if (today == saturday) tomorrow = sunday; else tomorrow = (day_t)(today + 1);

27 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-27 Enumerated Types (Cont’d) C handles enumerated type values just as it handles other integers, no range checking to verify that the value stored in an enumerated type variable is valid. –today = saturday + 3; is invalid

28 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-28 Figure 7.5 Accumulating Weekday Hours Worked

29 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-29 Figure 7.6 Six Roots for the Equation f(x) = 0

30 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-30 Figure 7.7 Using a Function Parameter

31 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-31 Figure 7.8 Change of Sign Implies an Odd Number of Roots

32 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-32 Figure 7.9 Three Possibilities That Arise When the Interval [xleft, xright] Is Bisected

33 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-33 Figure 7.10 Finding a Function Root Using the Bisection Method

34 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-34 Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

35 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-35 Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

36 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-36 Figure 7.11 Sample Run of Bisection Program with Trace Code Included

37 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-37 Figure 7.12 Geometric Interpretation of Newton's Method

38 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-38 Figure 7.13 Approximating the Area Under a Curve with Trapezoids

39 Copyright ©2004 Pearson Addison-Wesley. All rights reserved.7-39 Figure 7.14 Finite State Machine for Numbers and Identifiers


Download ppt "Chapter 7 Simple Date Types J. H. Wang ( 王正豪 ), Ph. D. Assistant Professor Dept. Computer Science and Information Engineering National Taipei University."

Similar presentations


Ads by Google