110 likes | 238 Views
Representation and Conversion of Numeric Types. We have seen multiple data types that C provides for numbers: int and double What differences are there between numbers represented in these two ways? Integer operations are typically faster Integers require less storage space
E N D
Representation and Conversion of Numeric Types • We have seen multiple data types that C provides for numbers: int and double • What differences are there between numbers represented in these two ways? • Integer operations are typically faster • Integers require less storage space • Floating point arithmetic is subject to round-off error
Representation and Conversion of Numeric Types • Different numeric types are represented differently in the computer’s memory • Remember that each word of memory holds a string of 0’s and 1’s • An int is represented by a single binary string • A double is represented by two binary strings • One string for the mantissa • One string for the exponent • The number stored is then: mantissaX2exponent
Representation and Conversion of Numeric Types • The mantissa is a binary fraction between 0.5 and 1.0 for positive numbers and between -0.5 and -1.0 for negative numbers • Because of the finite size of memory cells not all numbers can be represented • Advantages of double: • Contains a fractional part • Can represent much larger numbers
Representation and Conversion of Numeric Types • The minimum range of positive integer values is 1 to 32,767 (ANSI C) • The minimum range of positive double values is approximately 10-37 to 1037 • ANSI C provides several other integer types • short -32,767 .. 32,767 • unsigned short 0..65,535 • unsigned 0..65,535 • long -2,147,483,647..2,147,483,647 • unsigned long 0..4,294,967,295
Representation and Conversion of Numeric Types • In addition to double, ANSI C defines the following floating point data types • float 10-37..1038 6 significant digits • double 10-307..10308 15 “ “ • long double 10-4931..104932 19 “ “ • Data types giving a larger range of values require more storage and take a longer time to evaluate in arithmetic expressions
Numerical Inaccuracies • Due to the finite word size of a computer, not all fractional values can be represented exactly (consider 1/3) • The representational error will depend on the number of bits used in the mantissa • An equality comparison of two floating point numbers may result in an unexpected result
Numerical Inaccuracies • Addition of a very large number to a very small fraction may cancel out the smaller number, resulting in cancellation error • If two very small numbers are multiplied the result may be too small to represent, resulting in arithmetic underflow • Similarly, multiplying two very large numbers may result in arithmetic overflow
Automatic Conversion of Data Types • In several situations, a numeric value is converted to another numeric type • When adding (for example) an int and a double, the int is first converted to a double • When assigning an expression of type double to a variable of type int, the expression is first evaluated as a double and then converted to an int to allow the assignment • Likewise, an int expression is evaluated before it is converted to double to be assigned to a double variable
Explicit Conversion of Data Types • The programmer can also cause a value to be converted to another type explicitly using the cast operator frac = (double)n1 / (double)d1; • The data type we want to convert to is inside of parentheses and we convert the value directly to the right (i.e. the cast has a high precedence) frac = (double)(n1 / d1);
Representation and Conversion of Type char • The char data type allows us to store single characters in a variable • In order to understand how C evaluates comparison operations involving characters, we need to know how characters are represented internally • The most common code for representing characters (and the one used in C is called ASCII)
Representation and Conversion of Type char • The ASCII character set consists of 256 characters • Each character is associated with one of the numbers from 0-255 • The number of the character defines the order used in comparisons • Only 95 of the characters are printable, the others are control characters (e.g. move the cursor to the top of the screen) • Cast a char to int to find its ASCII value