chapter 7 simple date types instructor: kun-mao chao ( 台大資工 趙坤茂 )
TRANSCRIPT
Chapter 7Simple Date Types
Instructor:Kun-Mao Chao(台大資工 趙坤茂 )
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-2
Representation and Conversion of Numeric Types• Simple data typeSimple data type is the data type used to store a
single value.– e.g., int, double, and char.
• Operations involving integer are faster than those involving double.
• Operations with integer are always precise, whereas loss of accuracy may occur when dealing with double.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-3
Internal Formats of Type int and Type double• IntegersIntegers are represented by standard binary
numbers.– e.g., 7 = 01101.
• DoublesDoubles are represented by two sections: mantissamantissa and exponentexponent.– real number = mantissa * 2exponont
– 4.0 = 0010 * 20001 = 2 * 21
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-4
Integer Types in C
Type Range
short -32767 .. 32767
unsigned short 0 .. 65535
int -32767 .. 32767
unsgined 0 .. 65535
long -2147483647 .. 2147483647
unsgined long 0 .. 4294967295
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-5
Floating-Point Types in C
Type Range
float 10-37 .. 1038
double 10-307 .. 10308
long double 10-4931 .. 104932
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-6
An Example to Print Ranges for Positive Numeric DataYou can determine the maximal or minimal int and double values of your C environment by the following program.
%e is the format in scientific notation
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-7
Numerical Inaccuracies
• The representation errorrepresentation error (or round-off round-off errorerror) is an error due to coding a real number by a finite number of digits.– e.g., the faction of 1/3 is 0.3333…
– It is impossible to code the precise value of 1/3.
• The representation error depends on the number of digits used in the mantissamantissa.– The more bits, the smaller the error.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-8
An Example of the Representation Error• for (trial = 0.0;
trial != 10.0; trial = trial + 1) {
…}
• The result of adding 0.1 for one hundred times is not exactly 10.0.– The above loop may fail to terminatefail to terminate on some
computers.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-9
Other Problems When Manipulating Numbers• Cancellation errorCancellation error is an error resulting from
applying an operation to a large number and a small number, and the effect of the smaller number is lost.– e.g., 1000.0 + 0.0000001234 is equal to 1000.0.
• Arithmetic underflowArithmetic underflow is an error in which a very small computational result is represented as zero.– e.g., 0.00000001 * 10-1000000 is equal to 0.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-10
Other Problems When Manipulating Numbers• Arithmetic overflowArithmetic overflow is an error in which the
result is too large to be represented.– The error may be detected in compiling time or run
time.– Some machine may let the arithmetic overflow occur
and the computational result is unexpected.– e.g., 999999999 * 109999999 may become a negative
value in some machine.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-11
Automatic Conversion of Data Types (1/2)• The data of one numeric type may be automatically
converted to another numeric type.
e.g.,int k = 5, m = 4, n;double x = 1.5, y = 2.1, z;
Context Example Explanation
Expression with binary operator and operands of different types
k + xvalue is 6.5.
Value of k is converted to double before the operation.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-12
Automatic Conversion of Data Types (2/2)
Context Example Explanation
Assignment with type double target variable and type int expression
z = k / m;expression value is 6.5; z is 1.0.
Expression is evaluated first. Then the result is converted to double.
Assignment with type int target variable and type double expression
n = x * y;expression value is 3.15; n is 3.
Expression is evaluated first. Then the result is converted to int.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-13
Explicit Conversion of Data Types
• C also provides an explicit type conversion operation called castcast.
• For example, frac = n1 / d1;if n1 = 2 and d1 = 4, frac would be 0.
• CastCast: place the name of the desired type in parentheses immediately before the value to be converted.– e.g., frac = (double)n1 / (double)d1;– frac is evaluated to 0.5.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-14
Representation and Conversion of Type char (1/2)• Character values can be compared by the
equality operatorsequality operators == and !=, or by the relational operatorsrelational operators <, <=, >, and >=.
e.g., letter = ‘A’; if (letter < ‘Z’) …
• Character values may also be compared, scanned, printed, and converted to type int.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-15
Representation and Conversion of Type char (2/2)• Each character has its own unique numeric code.• A widely used standard is called American American
Standard Code for Information Interchange Standard Code for Information Interchange (ASCII)(ASCII). (See Appendix A in the textbook)– The printable charactersprintable characters have codes from 32 to
126, and others are the control characterscontrol 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 the corresponding code values in ASCII.
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-16
An Example of the Conversion between char and intC permits conversion of type char to type int and vice versa.
Convert char to int
Convert int to char
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-17
Enumerated Types (1/2)
• Enumerated typeEnumerated type is a data type whose list of values is specified by the programmer.
• Syntax: typedef enum {identifier_list} enum_type;
• e.g., typedef enum{entertainment, rent, food, clothing}expense_t;
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-18
Enumerated Types (2/2)• In the above example, a new data type expense_t is
created, and we can declare a variable with this type:
e.g., expense_t expense_kind;
• The variable expense_kind can be manipulated as any other integer.
e.g., switch(expense_kind){ case entertainment:
printf(“entertainment”);break;
case rent: case food: …
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-19
Function Parameters (1/2)
• When invoking a function call, we can include function function parametersparameters in the parameter list.
• Declaring a function parameterfunction parameter is accomplished by simply including the prototype of the functionprototype of the function in the parameter list.
A function parameter
Copyright ©2004 Pearson Addison-Wesley. All rights reserved. 7-20
Function Parameters (2/2)
• Suppose there are two existed functions: double sqrt(double)double sqrt(double) , and double sin(double)double sin(double).
• evaluateevaluate is a function that includes a function parameter as shown in the previous slide.
• We can invoke evaluateevaluate as follows.evaluateevaluate(sqrtsqrt, 0.25, 25.0, 100.0);evaluateevaluate(sinsin, 0.0, 3.14159, 0.5*3.14159);