ç¬¬ä¸�ç« _

This preview shows pages 1–10. Sign up to view the full content.

Problem Solving and Program Design in C Chap. 7 Simple Data Types Chow-Sing Lin

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Representation and Conversion of Numeric Types Can the data type double be used for all numbers? Yes But on many computers, operations involving integers are faster than those involving numbers of type double Less storage space is needed to store type int values Operations with integers are always precise When dealing with type double numbers Some loss of accuracy or round-off error may occur Dr. Chow-Sing Lin 2 Simple Data Types - CH7
Representation and Conversion of Numeric Types (Cont.) All data are represented in memory as binary strings Strings of 0s and 1s The format of type double values is analogous to scientific notation Also called floating-pointed format Dr. Chow-Sing Lin 3 Simple Data Types - CH7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Representation and Conversion of Numeric Types (Cont.) The mantissa and exponent are chosen so that the following formula is correct Because of the finite size of a memory cell Not all real numbers in the range allowed can be represented precisely as type double values double format is that a much larger range of numbers can be represented as compared to type int Dr. Chow-Sing Lin 4 Simple Data Types - CH7 Real number = mantissa X 2 exponent
Representation and Conversion of Numeric Types (Cont.) Determine the exact int and double ranges The %e format specifier in the second call to printf calls for the values associated with the names DBL_MIN and DBL_MAX to be printed in scientific notation Dr. Chow-Sing Lin Simple Data Types - CH7 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Integer Types in C Dr. Chow-Sing Lin Simple Data Types - CH7 6 Type Range in Typical Microprocessor Implementation short -32,767 . . 32,767 unsigned short 0 . . 65,535 int -32,767 . . 32,767 unsigned 0 . . 65,535 long -2,147483,647 . . 2,147,483,647 unsigned long 0 . . 4,294,967,295
Floating-Pint Types in C ANSI C defines three floating-point types float Values must have at least six decimal digits of precision double and long double Both type double and long double values must have at least ten decimal digits Dr. Chow-Sing Lin Simple Data Types - CH7 7

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Floating-Pint Types in C (Cont.) Type Approximate Range* Significant Digits* float 10 -37 .. 10 38 6 double 10 -307 .. 10 308 15 long double 10 -4931 .. 10 4932 19 Dr. Chow-Sing Lin Simple Data Types - CH7 8 In a typical microprocessor-based C implementation
Numerical Inaccuracies Representational error (round-off error ) Certain fraction cannot be represented exactly in the decimal number system E.g., the fraction 1/3 is 0.333333… Some fraction cannot be represented exactly as binary numbers in the mantissa of the type double format An error due to coding a real number as a finite number of binary digits The round error will depend on the number of binary digits (bits) used in the mantissa The more bits, the smaller the error Dr. Chow-Sing Lin Simple Data Types - CH7 9

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}