1 / 15

Representation of data types

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 1011. 1011 is the representation of -5 The left-most bit is named the sign bit.

amos
Download Presentation

Representation of data types

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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 1011. 1011 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 = 32767 and INT_MIN is -215 = -32768 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 10-37 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. 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: -2147483648 . . 2147483647 2 raised to 63 minus 1 equals 2147483647 Range of positive values of type double: 2.225074e-308 . . 1.797693e+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*5 (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 1.6 (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. peppar:~/c/Ckod> cat slumpa.c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> int main(void) { int i, n, die_1, die_2, two_sum; srand(time(NULL)); /* nitialize the random number generator */ printf("\n%s\n%s", "Simulate throwing two dice a number of times.", "How many throws? "); scanf("%d", &n); for (i = 0; i < n; ++i) { if (i % 20 == 0) putchar('\n'); die_1= (int) floor(rand()/(double)RAND_MAX*6.0)+1; die_2= (int) floor(rand()/(double)RAND_MAX*6.0)+1; two_sum = die_1+ die_2; printf("%3d", two_sum); } printf("\n\n"); return 0; } 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. Figure 3.15 Program with a User-Defined Function /* Performs three square root computations */ #include <stdio.h> /* definitions of printf, scanf */ #include <math.h> /* definition of sqrt */ void instruct(void); /* Displays user instructions */ int main(void) { double first, second, /* input - two data values */ first_sqrt, /* output - square root of first input value */ second_sqrt, /* output - square root of second input */ sum_sqrt; /* output - square root of sum */ /* Display instructions. */ instruct(); /*************Call of function instruct **************/ /* Get a number and display its square root. */ printf("Enter a number> "); scanf("%lf", &first); first_sqrt = sqrt(first); printf("The square root of the number is %.2f\n", first_sqrt); /* Get second number and display its square root. */ printf("Enter a second number> "); scanf("%lf", &second); second_sqrt = sqrt(second); printf("The square root of the second number is %.2f\n",second_sqrt); TDBA66, VT-03 Lecture Ch 3

  15. Fig. 3.15 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

More Related