Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there.

Similar presentations


Presentation on theme: "Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there."— Presentation transcript:

1 Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 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

2 Representation and Conversion of Numeric Types 4 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: mantissaX2 exponent

3 Representation and Conversion of Numeric Types 4 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 4 Because of the finite size of memory cells not all numbers can be represented 4 Advantages of double: –Contains a fractional part –Can represent much larger numbers

4 Representation and Conversion of Numeric Types 4 The minimum range of positive integer values is 1 to 32,767 (ANSI C) 4 The minimum range of positive double values is approximately 10 -37 to 10 37 4 ANSI C provides several other integer types short-32,767.. 32,767 unsigned short 0..65,535 unsigned0..65,535 long-2,147,483,647..2,147,483,647 unsigned long0..4,294,967,295

5 Representation and Conversion of Numeric Types 4 In addition to double, ANSI C defines the following floating point data types float10 -37..10 38 6 significant digits double10 -307..10 308 15““ long double10 -4931..10 4932 19““ 4 Data types giving a larger range of values require more storage and take a longer time to evaluate in arithmetic expressions

6 Numerical Inaccuracies 4 Due to the finite word size of a computer, not all fractional values can be represented exactly (consider 1/3) 4 The representational error will depend on the number of bits used in the mantissa 4 An equality comparison of two floating point numbers may result in an unexpected result

7 Numerical Inaccuracies 4 Addition of a very large number to a very small fraction may cancel out the smaller number, resulting in cancellation error 4 If two very small numbers are multiplied the result may be too small to represent, resulting in arithmetic underflow 4 Similarly, multiplying two very large numbers may result in arithmetic overflow

8 Automatic Conversion of Data Types 4 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

9 Explicit Conversion of Data Types 4 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);

10 Representation and Conversion of Type char 4 The char data type allows us to store single characters in a variable 4 In order to understand how C evaluates comparison operations involving characters, we need to know how characters are represented internally 4 The most common code for representing characters (and the one used in C is called ASCII)

11 Representation and Conversion of Type char 4 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


Download ppt "Representation and Conversion of Numeric Types 4 We have seen multiple data types that C provides for numbers: int and double 4 What differences are there."

Similar presentations


Ads by Google