Skip to main content
ICT
Lesson A21 - Number Systems
 
Main Previous Next
Title Page >  
Summary >  
Lesson A1 >  
Lesson A2 >  
Lesson A3 >  
Lesson A4 >  
Lesson A5 >  
Lesson A6 >  
Lesson A7 >  
Lesson A8 >  
Lesson A9 >  
Lesson A10 >  
Lesson A11 >  
Lesson A12 >  
Lesson A13 >  
Lesson A14 >  
Lesson A15 >  
Lesson A16 >  
Lesson A17 >  
Lesson A18 >  
Lesson A19 >  
Lesson A20 >  
Lesson A21 >  
Lesson A22 >  
Lesson AB23 >  
Lesson AB24 >  
Lesson AB25 >  
Lesson AB26 >  
Lesson AB27 >  
Lesson AB28 >  
Lesson AB29 >  
Lesson AB30 >  
Lesson AB31 >  
Lesson AB32 >  
Lesson AB33 >  
Vocabulary >  
 

G. How this Relates to Computers and Overflow Errors page 9 of 11


  1. Modern computers are binary computers. They are made up of switches. Each switch is either on or off at any one time. Everything we put into the computer (numbers and letters) must be converted to binary numbers.

  2. At some point in time when using a computer, there are not going to be enough bits to accurately represent the decimal numbers in the real world. We will look at this in a smaller scenario, in order to simplify this discussion.

  3. Let’s hypothesize that we have 8 bits for our numbers and there are no negative numbers.

    27
    26
    25
    24
    23
    22
    21
    20
    128
    64
    32
    16
    8
    4
    2
    1

  4. The numbers available to us range from 0000 0000 to 1111 1111 (binary), which is 0 to 255 (decimal). This is not very large for an integer. Attempting to put a larger number into one of our integers would result in an overflow error. Look back at the charts in Lesson A3 for minimum and maximum values for the different types of primitive data types.

  5. Another computer problem arises when using numbers that have decimal places. Again we will simplify the real process. We will allocate in our 8-bit storage 6 bits for the mantissa (the part to the left of the decimal place) and 2 bits for the fractional portion. We can still have overflow if we try to put a number that is too large for our storage. This storage will hold numbers from 0 to 63.75.

    25
    24
    23
    22
    21
    20
    2-1
    2-2
    32
    16
    8
    4
    2
    1
    1/2
    1/4

  6. Let’s convert 7.25 to our binary representation.

    25
    24
    23
    22
    21
    20
    2-1
    2-2
    0
    0
    0
    1
    1
    1
    0
    1

    Perfect!

    Now try 7.4. The first 6 bits are the same, 000111, but what are we going to do for the decimal places? We have four choices:

    00 -> 0
    01 -> .25
    10 -> .5
    11 -> .75

    We have no other choices. So it looks like we would have to choose between .25 and .5. This is called round-off error. Converting from decimal numbers with fractional parts to a binary representation can cause errors. We are using a very simplified version here, so the errors would usually occur much further out in the decimal places. Most of the time we would not see these errors but as programmers, we should be aware of them. This is why it is never a good idea to compare two floating numbers using “==”. It is much safer to compare the two and check for a certain level of accuracy.

    (Math.abs(x - y)< .000000001) is better than (x == y)

    This is also why a programmer should choose the best data type for the job.

  7. The third type of error is an under-flow error. Let's use our 8-bit storage scheme again, and try to store the decimal number .1.

    25
    24
    23
    22
    21
    20
    2-1
    2-2
    0
    0
    0
    1
    1
    1
    0
    1

The first 6-bits are 000000. Remember the four choices of decimals:

00 -> 0
01 -> .25
10 -> .5
11 -> .75

The number we are trying to store is too small for our storage scheme. Converting from decimal numbers to binary does not work for numbers very close to 0.

Answers to Practice Questions:

10 110 1012 -> 2658
3F116 -> 0011 1111 00012
3528 -> 011 101 0102
482 -> 0001 1110 00102
(Hint: convert to hexadecimal 1st)
482 -> 7428
482 -> 1E216
100012 -> 1710
57768 -> 307010
3DB16 -> 98710
110 111 0102 -> 6728
1011 0010 11112 -> B2F16
3FA16 -> 0011 1111 10102
7128 -> 111 001 0102

 

Main Previous Next
Contact
 © ICT 2006, All Rights Reserved.