chapter 7 simple date types j. h. wang ( 王正豪 ), ph. d. assistant professor dept. computer...

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

Upload: randolph-thornton

Post on 12-Jan-2016

228 views

Category:

Documents


0 download

TRANSCRIPT

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

Chapter 7Simple Date Types

J. H. Wang (王正豪 ), Ph. D.

Assistant Professor

Dept. Computer Science and Information Engineering

National Taipei University of Technology

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

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

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

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 2exponent

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-4

Figure 7.1 Internal Formats of Type int and Type double

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-6

Figure 7.2 Program to Print Implementation-Specific Ranges for Positive Numeric Data

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-7

Floating-point Format (Cont’d)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-8

Floating-point Format (Cont’d)

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

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.

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

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

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

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

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

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;

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

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?

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

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);

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

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

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

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.

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-18

Figure 7.3 Program to Print Part of the Collating Sequence

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

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

• Exampletypedef enum

{entertainment, rent, utilities, food, clothing,automobile, insurance, miscellaneous}

expense_t;expense_t expense_kind;

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

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.

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-21

Figure 7.4 Enumerated Type for Budget Expenses

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-22

Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-23

Figure 7.4 Enumerated Type for Budget Expenses (cont’d)

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

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.

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-25

Enumerated Types (Cont’d)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-26

Enumerated Types (Cont’d)

• Example– sunday < monday– wednesday != friday– tuesday >= Sunday

• Exampleday_t tomorrow;if (today == saturday)

tomorrow = sunday;else

tomorrow = (day_t)(today + 1);

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

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

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-28

Figure 7.5 Accumulating Weekday Hours Worked

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-29

Figure 7.6 Six Roots for the Equation f(x) = 0

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-30

Figure 7.7 Using a Function Parameter

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-31

Figure 7.8 Change of Sign Implies an Odd Number of Roots

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-32

Figure 7.9 Three PossibilitiesThat Arise When the Interval [xleft, xright] Is Bisected

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-33

Figure 7.10 Finding a Function Root Using the Bisection Method

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-34

Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-35

Figure 7.10 Finding a Function Root Using the Bisection Method (cont’d)

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-36

Figure 7.11 Sample Run of Bisection Program with Trace Code Included

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-37

Figure 7.12 Geometric Interpretation of Newton's Method

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-38

Figure 7.13 Approximating the Area Under a Curve with Trapezoids

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

Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-39

Figure 7.14 Finite State Machine for Numbers and Identifiers