Tino APCS

How this Relates to Computers and Overflow Errors

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.

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.

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

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

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.

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

Let’s convert 7.25 to our binary representation.

25 24 23 22 21 2 0 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.

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 2 0 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


Dark Mode

Outline