Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representation of data types

Similar presentations


Presentation on theme: "Representation of data types"— Presentation transcript:

1 Representation of data types
Integers (int) are represented as a signed binary number. Ex. (5)10 = (0101)2 (-5)10 = (?)2 Take the 1-complement of 0101, That is 1010, and add 1 which gives is the representation of -5 The left-most bit is named the sign bit. Normally an integer is stored in a memory ”cell” that contains 16 bits. I.e. INT_MAX is 215 = and INT_MIN is -215 = TDBA66, VT-03 Lecture Ch 3

2 Representation of double
real number = mantissa*2exponent Mantissa stored in finite number of bits and its value is between 0.5 and 1.0 or between -1.0 and -0.5 Round-off error and accuracy The minimum range of double is approximately to 1037 Check DBL_MIN and DBL_MAX in file float.h See Appendix B in text book TDBA66, VT-03 Lecture Ch 3

3 Finite arithmetic can fail
Cancellation error: adding values of very different magnitudes might not give the expected result (out-shift) Another cancellation error can occur when two similar values are subtracted (loosing precision) Arithmetic underflow and overflow: underflow when the value of an expression (in absolute value) is less than DBL_MIN overflow when a value exceeds DBL_MAX TDBA66, VT-03 Lecture Ch 3

4 peppar:~/c/Book-Figs> cat fig3_02V2.c
/* Modification of program in Fig. 3.2 * Find implementation's ranges for positive numeric data */ #include <stdio.h> #include <limits.h> /* definition of INT_MAX */ #include <float.h> /* definitions of DBL_MIN, DBL_MAX */ #include <math.h> /* definition of pow() */ int main(void) { int max_number; printf("Range of integer values of type int: %d . . %d\n", INT_MIN, INT_MAX); /* Compute 2 raised to 63 and compare */ max_number = pow(2,63)-1; printf("\n2 raised to 63 minus 1 equals %d\n", max_number); printf("Range of positive values of type double: %e . . %e\n", DBL_MIN, DBL_MAX); return (0); } TDBA66, VT-03 Lecture Ch 3

5 Note! –lm makes the mathematical library to be loaded
Compile, link and load Note! –lm makes the mathematical library to be loaded gcc -o fig3_02V2 fig3_02V2.c –lm peppar:~/c/Book-Figs> ./fig3_02V2 Range of integer values of type int: 2 raised to 63 minus 1 equals Range of positive values of type double: e e+308 peppar:~/c/Book-Figs> Output from program fig3_02V2.c TDBA66, VT-03 Lecture Ch 3

6 Evaluation of arithmetic expressions
Integer arithmetic Ex.1: 7/5*5 equals 1* (division) Ex.2: 7%5*5 equals 2*5 (reminder) The same for variables of int type. • Real arithmetic Ex.3: 8.0/5.0 equals (as expected) Mixed mode 8.0/5 also equals 1.6 TDBA66, VT-03 Lecture Ch 3

7 Mixed type assignment Ex.1: double x, y;
int tal; y=tal/5+6; /* expression of type int */ But the value of y is stored as a double Ex.2: tal=x*y-4; /* expression is of type double*/ But the value of tal is stored as an int (the integral part of the expression) TDBA66, VT-03 Lecture Ch 3

8 Casting A value of an expression can be explictly cast to another data type Ex.3: y=(double)tal/5+6; /* prevent from integer division */ Note the difference when writing Ex.4: y=(double)(tal/5+6); Here expression is evaluated using integer divide and the whole expression is cast to double TDBA66, VT-03 Lecture Ch 3

9 Figure 3.7 Using Casts to Prevent Integer Division
/* Computes a test average*/ #include <stdio.h> int main(void) { int total_score, num_students; double average; printf("Enter sum of students' scores> "); scanf("%d", &total_score); printf("Enter number of students> "); scanf("%d", &num_students); average = (double)total_score / (double)num_students; printf("Average score is %.2f\n", average); return (0); } Enter sum of students' scores> 1822 Enter number of students> 25 Average score is 72.88 TDBA66, VT-03 Lecture Ch 3

10 Priority of operators See Appendix C in text book
If the same priority the evaluation goes from left to right Ex.1: (a+b)/c is evaluated as ((a+b)/c) Ex.2: a+b/c is evaluated as (a+(b/c)) Ex.3: a*b/z is evaluated as ((a*b)/z) Ex.4: y-b/x-a is evaluated as ((y-(b/x))-a) Ex.5: (y-b)/(x-a) is eval. as ((y-b)/(x-a)) TDBA66, VT-03 Lecture Ch 3

11 Predefined math functions
See page 98 in text book Ex. write program to simulate one toss with an ordinary die ON THE WHITEBOARD TDBA66, VT-03 Lecture Ch 3

12 return 0; printf("\n\n"); } printf("%3d", two_sum); two_sum = die_1+ die_2; die_2= (int) floor(rand()/(double)RAND_MAX*6.0)+1; die_1= (int) floor(rand()/(double)RAND_MAX*6.0)+1; putchar('\n'); if (i % 20 == 0) for (i = 0; i < n; ++i) { scanf("%d", &n); "How many throws? "); "Simulate throwing two dice a number of times.", printf("\n%s\n%s", srand(time(NULL)); /* nitialize the random number generator */ int i, n, die_1, die_2, two_sum; { int main(void) #include <time.h> #include <math.h> #include <stdlib.h> #include <stdio.h> peppar:~/c/Ckod> cat slumpa.c TDBA66, VT-03 Lecture Ch 3

13 Simple user-written functions
Functions without parameters (neither input nor output) See Fig. 3.15 TDBA66, VT-03 Lecture Ch 3

14 printf("The square root of the second number is %.2f\n", second_sqrt);
second_sqrt = sqrt(second); scanf("%lf", &second); printf("Enter a second number> "); /* Get second number and display its square root */ printf("The square root of the number is %.2f\n", first_sqrt); first_sqrt = sqrt(first); scanf("%lf", &first); printf("Enter a number> "); /* Get a number and display its square root. */ instruct(); /*************Call of function instruct **************/ /* Display instructions */ sum_sqrt; /* output - square root of sum */ second_sqrt, /* output - square root of second input */ first_sqrt, /* output - square root of first input value */ double first, second, /* input - two data values */ { main(void) int void instruct(void); /* Displays user instructions */ #include <math.h> /* definition of sqrt */ #include <stdio.h> /* definitions of printf, scanf */ /* Performs three square root computations */ Figure Program with a User-Defined Function TDBA66, VT-03 Lecture Ch 3

15 /* Display the square root of the sum of the two numbers. */
Fig cont. /* Display the square root of the sum of the two numbers */ sum_sqrt = sqrt(first + second); printf( "The square root of the sum of the two numbers is %.2f\n", sum_sqrt); return (0); } /* Displays user instructions*/ void instruct(void) { printf("This program demonstrates the use of the \n"); printf("math library function sqrt (square root).\n"); printf("You will be asked to enter two numbers --\n"); printf("the program will display the square root of \n"); printf("each number and the square root of their sum.\n\n"); TDBA66, VT-03 Lecture Ch 3


Download ppt "Representation of data types"

Similar presentations


Ads by Google