DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 1 Tutorial Programming & Data Structure: CS 11001 Section - 1/A DO NOT POWER ON THE MACHINE Department of Computer Science and Engineering I.I.T. Kharagpur Spring Semester: 2009-2010 (27.01.2010)
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 2 Download Download the file tut270110.ps from Programming & Data Structures... of http://www.facweb.iitkgp.ernet.in/ goutam View the file using the command kghostview & or ggv &
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 3 Tutorial III What is IEEE 754 floating-point representation?
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 4 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 s exponent significand/mantissa 1 bit 8 bits 23 bits Single Precession 31 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 s exponent significand/mantissa 1 bit 11 bits 20 bits significand (continued) 32 bits Double Precession
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 5 Single Precession Data Fields Data Type Exponent Significand 0 0 0 0 nonzero ± denormal number 1-254 anything ± normalized number 255 0 ± 255 nonzero N an (not a number)
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 6 Double Precession Data Fields Data Type Exponent Significand 0 0 0 0 nonzero ± denormal number 1-2046 anything ± normalized number 2047 0 ± 2047 nonzero N an (not a number)
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 7 Single Precession Normalized Number Let the sign bit (31) be s, the exponent (30-23) be e and the mantissa (significand) (22-0) be m. The valid range of the exponent is 1 to 254 (if e is treated as an unsigned number). The exponent (e) is biased by 127 i.e. the actual value of the exponent is e 127. This gives a range: 2 1 127 = 2 126 to 2 254 127 = 2 127.
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 8 Single Precession Normalized Number The normalized significand is 1.m (binary dot). One is not present explicitly. The sign bit s = 1 is a -ve number and s = 0 is a +ve number. The final value of a normalized number is ( 1) s 1.m 2 e 127
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 9 Single Precession Denormal Number The interpretation of a denormal number is different. The content of the exponent part (e) is zero and the significand part (m) is non-zero. The value of a denormal number is ( 1) s 0.m 2 126 There is no implicit one in the significand.
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 10 Tutorial III.1 1. What is the representation of 27.75 in IEEE-754 single precession format? 2. What is the representation of +27.7? 3. What is the largest magnitude of a single precession normalized number? 4. What is the smallest magnitude of a single precession normalized number? 5. Answer the last two questions for denormal
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 11 numbers. 6. What is the value stored as a result of 1.0/0.0?
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 12 Tutorial III.2 Write a C program that computes the sum up to the n th term of the series, where n is an input. 1 2 + 1 + 1 4 + 1 2 + 1 6 + 1 3 + + 1 2k + 1 k + + 1 α. where 1 α = 1 m 1 2m if n = 2m, if n = 2m 1.
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 13 Tutorial III.3
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 14 What will be printed (a)? #include <stdio.h> int main() //./pds/lect/prog/temp2.c { printf("0%%10 = %d\n", 0%10); printf("4%%10 = %d\n", 4%10); printf("10%%4 = %d\n", 10%4); printf("-10%%4 = %d\n", -10%4); return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 15 What will be printed (b)? #include <stdio.h> int main() //./pds/lect/prog/temp3.c { int n = 4 ; float a = 2.5 ; char c = a ; // ASCII value 97 printf("%d*%f = %f\n", n, a, n*a); printf("%d*%f+%c = %f\n",n,a,c,n*a+c); return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 16 What will be printed (c)? #include <stdio.h> int main() //./pds/lect/prog/temp4.c { printf("1/3*30.0=%f\n", 1/3*30.0); return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 17 What will be printed (d)? #include <stdio.h> int main() //./pds/lect/prog/temp14.c { int n = 2147483647; printf("n+1: %d\n", n+1) ; return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 18 What will be printed (e)? #include <stdio.h> int main() //./pds/lect/prog/temp8.c { float a=1.0e-5, b=1.0e+5, c; c = a+b ; printf("%e + %e = %e\n", a, b, c); return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 19 What will be printed (f)? #include <stdio.h> int main(){ // temp32.c float a = 1.7 ; } if(a == 1.7) printf("equal\n") ; else printf("unequal\n") ; return 0 ;
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 20 What will be printed (g)? #include <stdio.h> int main() //./pds/lect/prog/temp9.c { int n = 10, m ; printf("enter an integer: "); scanf("%d", &m); // Enter zero printf("n/m: %d\n", n/m); return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 21 What will be printed (h)? #include <stdio.h> #include <math.h> int main() //./pds/lect/prog/temp10.c { float n = 10.0, m, r ; printf("enter a number: "); scanf("%f", &m); // Enter zero printf("n/m: %f\n", r = n/m); printf("atan(%f) = %f\n", r, atan(r)); return 0 ; }
DS Tutorial: III (CS 11001): Section 1 Dept. of CS&Engg., IIT Kharagpur 22 Tutorial III.6 Write a C program to print the ASCII values of a character in decimal, hex and octal notation. Char Decimal Hex Oct a 97 0x61 0141 A 65 0x41 0101