Sie sind auf Seite 1von 11

ESc101: Fundamentals of Computing

2011-12-Monsoon Semester Lecture #35, October 31, 2011

Please switch off your mobile phones.

Announcements
Lab 12 starts from Thursday, 3rd November. Monday sections will do lab on 5th November (Saturday) instead y ( y) of 7th November (Idul Zuha) Lab exam in the week of 14th to 18th November Labs 11 and 12 will not be pre-announced to give you practice End-sem exam is on 25th November, 8:00 AM End sem Copies can be seen on 28th afternoon.

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap
Program in multiple source files External variables Static variables

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Global variables in case of multiple files


We have seen that variables could be declared at the beginning of the file before any function is defined.
We have been calling them global variables These can be used from any function in that file Technically correct term for them is external variables as they are external to each function which is using them If a global variable is defined in file1.c, how do we use it in functions in file2.c
W could declare them explicitly in each function that uses We ld d l h l l hf h them as follows: extern int roll_numbers [ ]; Or such a declaration could be done in the beginning of file2.c, before any function is defined
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 3

Recap: Scope of external variables


External variables are defined in one file, and are declared in all such files where a function may access them In the file where an external variable is defined, the scope is from the d fi i i till the end of the file f h definition ill h d f h fil
extern keyword is not used memory allocation takes place while compiling this file

In other files, there is only a declaration


extern keyword is to be used no memory allocation takes place if the declaration is outside all functions, then scope is from th d l ti i t id ll f ti th i f the declaration till the end of the file if the declaration is inside a function, then scope is from the declaration till the end of that function Defining global (external) variables in multiple files is an error
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 4

Recap: Static variables


Normally declared variables are called automatic variables
The memory is allocated for each invocation of function For a recursive function, memory is allocated for each recursive call function The memory is taken away when the function returns Previously stored value is not available to the next invocation of the same function

By declaring a variable static


the variable becomes a permanent variable memory allocation takes place only once, even across recursive calls y p y memory is not taken away after the function call, hence one can access value stored during the previous function call

there is no change in scope of the variable though


Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 5

Recap: Static variables


Within a function we can define static variables as: function,
static int roll_numbers[500];

If static is not used, and it is recursive function, then every call would have needed such a large memory area, and there th was no way to access what previous call to the t h t i ll t th function was doing.

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Recap: Four types of variables


There are four classes of variables in C
automatic external static register

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

Integer Representation
We represent integers as binary numbers
since computer memory can only store 0 or 1. For example: 56 will be stored as 00111000

How do we store negative numbers?


1st bit of memory is usually the sign bit In case of 8 bit memory, only 7 bits are for magnitude. Similarly 32-bit memory would have 31 bits for magnitude Hence the largest positive integer that can be stored in an integer variable on our PCs is: 2^31 1. There are variations on storing magnitude
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 8

Ones Complement
In 1s complement representation
Positive numbers are stored as mentioned previously
First bit is 0 Other bits store the binary representation of the number

Negative numbers are stored as complements of positive numbers


First determine the representation of the positive number Flip every bit N that the f Note h h first bit will become 1 for every negative number b ll b f b

Example:
+56 is 00111000 -56 is 11000111
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 9

Ones complement
Problems with 1 s complement 1s
addition always requires an extra carry operation multiplication requires that we convert the negative number to positive number, carry out multiplication, and then again convert the positive number to negative number (if needed) Hence inefficient computation A minor issue: there are two representations for 0
all 0s all 1s
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 10

Twos Complement
Positive numbers are stored as usual (as in 1s complement) To represent negative number
First find its 1s complement representation Add 1 to the 1s complement representation Example: To store -56
First consider 56, which is: 00111000 Take 1s complement: 11000111 Add 1 to it: 11001000

To find magnitude of a negative number


Take complement of the number (flip every bit) Add 1 to the result.
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 11

Floating point numbers


Use the scientific notation: f X b k b^k With this notation, we need to store f and k. We also need to decide the value of b. The most commonly used representation is:
Use 1 bit for sign Use 8 bits to store k (called exponent) Use 23 bits to store f (called mantissa) Value of b is taken as 2

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

12

Floating point numbers


Value of exponent can be from 0 to 255 (8 bit, unsigned) (8-bit Exponent is stored in excess 127 format, that is,
a stored value of 1 means that the exponent is -126 a stored value of 200 means that the exponent is 73 exponent can be from -127 to +126 So the range is from 2-127 to 2126, or 10-38 to 10+38 approx.

Advantages of excess 127 format


No need to deal with sign or 2s complement Comparing two floating point numbers becomes same as comparing two integers
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 13

Floating point numbers


Normalized form:
Let us consider a floating number in binary, 110.011 X 210101 This can also be written as:
1100.11 X 210100 or 11.0011 X 210110

In normalized form, the integer part of the fraction is exactly 1 The above fraction in normalized form will be:
1.10011 X 210111

Now, we can save one bit by not storing the integer part of the fraction, since it is always 1.
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 14

Floating point numbers


Problem in representing exact value
Consider an 8-bit format: 1 bit sign, 3 bit exponent, 4 bit mantissa Let us consider exponent to be 3 Mantissa of 0000 will mean 1.000 X 2^3 = 8.000 (in decimal) Mantissa of 0001 will mean 1.0001 X 2 ^3 = 8.500 (in decimal) So, the number 8.25 will be stored either as 8.0 or as 8.5 You lose precision Y l ii If you want higher precision
more bits for mantissa less bits for exponent, you lose ability to store large numbers.
Lec-35 Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon 15

Floating point numbers


There is no representation for 0.0 in our scheme 00
The smallest magnitude that can be stored is: 1.000 X 2-127

Spacing between floating point numbers (precision) is not uniform:


At the smallest level (exponent = -127), the spacing is 2-150 At the highest level (exponent = 126), the spacing is 2103

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

16

Floating point numbers


There are three types of errors:
Underflow: Trying to store exponent less than -127 Overflow: Trying to store exponent more than 126 Rounding off: Storing the nearest floating point number

Floating point arithmetic


The hardware has to do a lot more for floating point arithmetic compared to integer arithmetic ith ti dt i t ith ti Do not store numbers as floating point, unless you really need fractions

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

17

Floating point numbers


We use long instead of int when we need to store larger long int numbers But use of double instead of float
gives not just larger numbers (bigger exponent) but also higher precision (more bits for mantissa)

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

18

Floating point numbers


What to do if you need a small finite precision, say, only precision say up to 1st bit of mantissa?

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

19

10

Any Questions?

Lec-35

Dheeraj Sanghi, CSE Dept., IIT Kanpur ESc101, 2011-12-Monsoon

20

11

Das könnte Ihnen auch gefallen