Binary Fractions and Precision

You might have looked at the interactive binary page and wondered what happens if we want to store numbers that aren't positive integers? After all, you've seen decimals and negative numbers on your computer. This page will show you how we can represent decimals/fractions and negative decimal numbers using binary. For more detail on how negative numbers are represented, have a look at the two's complement page. If you want to take this idea further, you can also look at normalised floating-point binary.

Fractions and Decimals

On the binary page, we said that in a number system based on tens, each column heading (units, tens, hundreds, etc.) is ten times the value of the column heading to its right, and in a number system based on twos, each column heading is two times the one to its right. This relationship continues to the right of the decimal point, so in denary the column headings are 1/10, 1/100, 1/1000, etc., and in binary they are 1/2, 1/4, 1/8, etc.

Negative numbers are most often represented using something called two's complement (although there are other methods), with the left-most bit indicating whether the number is positive (0) or negative (1). To create a one's complement binary number, simply take a binary number and swap the 0s and 1s - e.g. 01100101 becomes 10011010. If we then add 1 to the one's complement, we get the two's complement - e.g. 01100101 becomes 10011011.

Both these techniques are used below - click on the bits to create a positive or negative binary fraction and see its value:

+/-
0
.
1/2
0
1/4
0
1/8
0
1/16
0
1/32
0
1/64
0
1/128
0
= 0

At first sight, negative fractions in binary can look a bit confusing. Remember, though, that shifting binary numbers one place to the left (or right) multiplies (or divides) their value by two. For example, the binary representation of three (using three bits) is 011. The one's complement of this is 100, and the two's complement is 101, so minus three is 101. Shifting that two places to the right to give 1.01 is the same as dividing by four (diving by two and then by two again), and -3/4 is -0.75.

Precision

Looking at the digits above, can you work out how to represent 0.25? Or 0.75? What about -0.5? You will see that some numbers - e.g. 0.6 - can't be represented as an eight-bit binary number. This is a problem that we can run into when working with floating point numbers - e.g. have a look at the first draft of my standard-form page (and follow the instructions at the top).

You might have encountered strange results yourself when performing calculations, e.g.

0.3 - 0.1 isn't 0.2 according to Python

Computers aren't very good at working with floating-point numbers - that's something you need to consider where accuracy is important. If the above calculation was a financial transaction, for example, the bank might not be happy that 30p - 20p isn't 10p. Luckily there's often a workaround - in that case we can just work in pence rather than pounds and then format the result when we print it.

So why does it happen? You might be familiar with the idea of non-terminating decimals from your Maths class. Even some rational numbers, e.g. one-third, can't be expressed precisely in a fixed number of digits. We can write one-third as 0.3.(i.e. with a dot above the 3) to indicate that the 3 repeats forever, but a standard binary word doesn't allow us to do that.

Try using the digits above to represent 0.2 - it can't be done. Binary also has non-terminating decimals - it just happens with different numbers - and it's sometimes surprising when it happens with number that can be represented with one decimal place in denary.

Further Study

For a more in-depth discussion of number bases, look at the Number Bases page in the Mathematics section. You can also watch an introduction to binary and examples of how computers use binary on the AdvancedICT YouTube channel.

Why not practise your binary conversion skills with the Binary Breakout game? Or practice your programming skills by creating a program that uses these techniques? Try to create a program that will convert a decimal number to binary - I can think of at least two ways to do it (one of which uses bitwise Boolean logic). There is a selection of number base programs on the Python programming examples page.