Back Home Up Next

Numbers Fractions Negative numbers Floating point Binary arithmetic Codes

Fractional Numbers

 

Up to now we’ve regarded all numbers as unsigned integers. We need a means of dealing with the non-integer or fractional values we use in everyday life. Decimal numbers like 123.456 have a decimal point that divides the number into an integer part and a fractional part. Binary numbers are divided into integer and fractional parts by a binary point; and hexadecimal numbers by a hexadecimal point. As we’ve already stated, in the positional number system the weight of each digit is determined by its location in the number. If you move one place to the left, the weighting is multiplied by the base; if you move one place to the right, the weighting is divided by the base.

 

In the decimal system the value 0.110 is equal to 1/10, and the value 0.0110 is equal to 1/100. In the binary system the value 0.12 is equal to ½, and 0.012 is equal to ¼. In the hexadecimal system the value 0.116 is equal to 1/16 or 0.062510. The binary number 110.1012 can be expressed in decimal form by adding up the powers of two: 1 x 4 + 2 x 2 + 0 x 1 +1 x 1/2 + 0 x 1/4 + 1 x 1/8 = 6 5/8 = 6.625.

 

You can convert a binary fraction to decimal form by adding up the bits to the right of the binary point, scaled by the appropriate weighting (.5, .25, .125, .0625...). However, to convert a decimal fraction to binary form requires the following algorithm.

 

Converting Decimal Fractions to Binary Form

 

Take the decimal fraction and double it. This will give the result 0.xxx or 1.xxx. Record the integer part (i.e., the 0 or the 1), and then double the remaining fractional part. Continue in this way until the fractional part is .00000...0. The integers you have recorded make up the binary equivalent of the decimal fraction (the first integer recorded is the most-significant bit of the result). Consider the conversion of the decimal value 0.6875 to binary.

 

0.6875 x 2 = 1.3750

0.3750 x 2 = 0.7500

0.7500 x 2 = 1.5000

0.5000 x 2 = 1.0000

 

Therefore, 0.687510 = 0.10112.

 

 

Before leaving the topic of fractional numbers, we have to perform a demonstration. Let’s convert the decimal value 0.110 to binary form.

 

0.1000 x 2 = 0.2000

0.2000 x 2 = 0.4000

0.4000 x 2 = 0.8000

0.8000 x 2 = 1.6000

0.6000 x 2 = 1.2000

0.2000 x 2 = 0.4000

0.4000 x 2 = 0.8000

0.8000 x 2 = 1.6000

0.6000 x 2 = 1.2000 and so on.

 

The decimal fraction 0.110 cannot be converted into an exact binary representation of 0.110. The decimal number 0.110 is equal to the recurring binary fraction 0.000110011...2. No matter how many bits you take, you can never convert most decimal fractions to a binary equivalent. Does this matter? Consider now the algorithm:

 

X = 0

REPEAT

      X := X + 0.1

UNTIL X = 1

 

This algorithm tells us that the variable X is initially given the value 0, and is increased by 0.110 on each pass round the loop defined by the REPEAT...UNTIL construct. That is, X is given the successive values 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, and then the loop terminates when the condition X = 1 is satisfied. However, since the decimal value 0.110 cannot be represented exactly in binary form, X may not become equal to 1.0 exactly and the repetition never end. Even high-level language programmers sometimes have to be aware of the realities and restrictions of the underlying machine.

 

Fixed Point Arithmetic

 

Computers can handle fractional numbers with very little difficulty because fractional numbers behave exactly like integers. Suppose we take an 8-bit binary number and imagine that the binary point is located between the four most-significant bits and the four least-significant bits. This type of number is called fixed point, because the binary point is regarded as being located at a fixed position in the number. For example, the binary value 1001.1100 is equivalent to the decimal value 9.75, and is stored in memory as 10011100 because the binary point is imaginary. Only when you either read fixed point numbers into the computer or display them, does the binary point actually matter. Consider the addition of these two pairs of binary numbers.

 

Pair (a)                                                 Pair (b)

 

10011100         (156)                    1001.1100        (09.750)

00101010         (042)                    0010.1010        (02.625)

11000110         (198)                    1100.0110        (12.375)

 

Pair (a) consists of two 8-bit integers (156 and 42), and pair (b) consists of two fixed-point numbers with 4 integer bits to the left of the binary point and four fractional bits to the right of the binary point (9.75 and 2.625). However, both binary sums (a) and (b) are identical—apart from the location of the binary point. The same is true for decimal arithmetic; consider the equations 123 + 268 = 392 and 1.23 + 2.68 = 3.92. Fixed point arithmetic has the advantage that it is identical to conventional integer arithmetic (all you have to do is to keep track of the binary point). Unfortunately, fixed point arithmetic is not very satisfactory for dealing with either very large or very small numbers—the type of numbers used in scientific calculations by mathematicians, engineers, and scientists. Section 1.4 introduces floating point arithmetic that deals with very large and small numbers. But first, we are going to discuss how the computer deals with negative numbers.