fractions in binary. fractions binary doesn’t just do integers (whole numbers) it can also be used...

18
Fractions in Binary Fractions in Binary

Upload: marion-alexander

Post on 05-Jan-2016

215 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

Fractions in BinaryFractions in Binary

Page 2: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

Fractions Fractions

Binary doesn’t just do integers Binary doesn’t just do integers (whole numbers) it can also be used (whole numbers) it can also be used for fractions though it isn’t as for fractions though it isn’t as accurate as decimals in denaryaccurate as decimals in denarye.g. 136.75 could be expressed as e.g. 136.75 could be expressed as 136 + 1/2 + 1/4136 + 1/2 + 1/4

128 64 32 16 8 4 2 1 . 1/2 1/4 1/8 1/16128 64 32 16 8 4 2 1 . 1/2 1/4 1/8 1/16 1 0 0 0 1 0 0 0 . 1 1 0 01 0 0 0 1 0 0 0 . 1 1 0 0

This is binary notation – the decimal point is in a fixed point

Page 3: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

The problemThe problem

Clearly with a limited number of Clearly with a limited number of places after the decimal point it is places after the decimal point it is far less accurate as only a limited far less accurate as only a limited range of decimals can be range of decimals can be displayeddisplayed

The number of extra places after The number of extra places after the decimal point increase the decimal point increase accuracyaccuracy

Page 4: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

Fixed point exampleFixed point example

1 byte = 8 bits1 byte = 8 bits

If 4 bits were assigned after the If 4 bits were assigned after the decimaldecimal

Place (this does not take up any bits)Place (this does not take up any bits)

88 4 2 1 . 1/2 1/4 1/8 1/164 2 1 . 1/2 1/4 1/8 1/16

0 1 1 1 0 1 0 10 1 1 1 0 1 0 1

Page 5: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

TaskTask

Look at the table on page 195 Look at the table on page 195 showing binary fractionsshowing binary fractions

Do questions 1-3 on page 196Do questions 1-3 on page 196

Page 6: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

So how do you represent So how do you represent all of the other decimals all of the other decimals in binary?in binary?

Floating point binaryFloating point binary

Page 7: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

Floating Point BinaryFloating Point Binary

In decimal, you can use scientific In decimal, you can use scientific notation for really big numbersnotation for really big numbers

e.g. 1,2,0000,0000,0000e.g. 1,2,0000,0000,0000

Would be 0.12 x 10Would be 0.12 x 1013

Mantissa

Which holds the digits

Exponent

Which defines where to place the decimal point

In this case move 13 places to the right

Page 8: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

Floating Point Binary Floating Point Binary In binary, firstly bits are shared In binary, firstly bits are shared

between the mantissa and the between the mantissa and the exponentexponent

In the example below, 2 bytes (16 bits) In the example below, 2 bytes (16 bits) with 10 bits for the mantissa and 6 for with 10 bits for the mantissa and 6 for the exponentthe exponent

MantissaMantissa Exponent Exponent

0110100000 000011 = 0.1101 x 20110100000 000011 = 0.1101 x 2

3

This is a sign bit – if positive i.e. 0 move the decimal point right, if negative i.e. 1 move the decimal point left

There is always an imaginary decimal here

Sign bit

Page 9: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

Floating Point Binary Floating Point Binary

MantissaMantissa Exponent Exponent

0110100000 000011 = 0.1101 x 0110100000 000011 = 0.1101 x 22

= 110.1 = 110.1

= 6.5 in denary= 6.5 in denary

3

Page 10: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

TaskTask

Do Q4 on page196Do Q4 on page196

Rules:Rules:1.1. Place the point between the sign bit and Place the point between the sign bit and

the first digit of the mantissathe first digit of the mantissa2.2. Convert the exponent to its decimal form Convert the exponent to its decimal form

(positive or negative)(positive or negative)3.3. Move the point right if the exponent is Move the point right if the exponent is

positive or left if negativepositive or left if negative4.4. Convert the resulting binary number to Convert the resulting binary number to

denarydenary

Page 11: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

If the mantissa is If the mantissa is negativenegativeMantissaMantissa ExponentExponent

11101000001110100000 000011000011

1.1101 would become 1110.11.1101 would become 1110.1

-8 4 2 1 . ½-8 4 2 1 . ½

1 1 1 0 . 1 = -8+4+2+0.5 = -1.5 1 1 1 0 . 1 = -8+4+2+0.5 = -1.5

Negative sign bit

Move three places right

Negative leftmost bit

Page 12: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

What if the mantissa What if the mantissa and exponent are and exponent are negative?negative?MantissaMantissa ExponentExponent1100000000 1100000000 111110 111110

1.11.1 -2 -2

Move the point left and fill with 1’s as Move the point left and fill with 1’s as you do so:you do so:1.1111.111

-1+0.5+0.25+0.125 = -0.125-1+0.5+0.25+0.125 = -0.125

Page 13: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

TaskTask

Do question 5 on page 197Do question 5 on page 197

Page 14: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

NormalisationNormalisation

In order to get the most accurate In order to get the most accurate representation possible with a given size of representation possible with a given size of mantissa, no zeros should be put to the left mantissa, no zeros should be put to the left of the most significant bit (not including the of the most significant bit (not including the sign bit)sign bit)

E.g. in decimal:E.g. in decimal:0.034568x10 would be normalised to0.034568x10 would be normalised to

0.34568x100.34568x10A binary number in normalised form will have A binary number in normalised form will have the first bit of the mantissa not including the the first bit of the mantissa not including the sign bit as a 1sign bit as a 1

9

8

Page 15: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

To normalise a number To normalise a number in binaryin binary0 000110101 0000100 000110101 000010

1.1. put in the binary point and convert the exponent to put in the binary point and convert the exponent to decimal:decimal:

0.000110101 Exponent 20.000110101 Exponent 22.2. Move the number to the correct position and count Move the number to the correct position and count

the number of movements to the left to get the the number of movements to the left to get the binary point before the first 1binary point before the first 1

0.110101000 required 3 left places0.110101000 required 3 left places3.3. Take 3 away from the exponent of 2: 2-3 = -1Take 3 away from the exponent of 2: 2-3 = -14.4. Convert -1 back to binaryConvert -1 back to binary

0 110101000 111110 110101000 11111

Page 16: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

To normalise a To normalise a negative number in negative number in binarybinary With a negative number, the With a negative number, the

most significant bit not most significant bit not including the sign bit will be a 0including the sign bit will be a 0

Shift the number left until the Shift the number left until the first bit (not the sign bit) is a first bit (not the sign bit) is a zero then adjust the exponentzero then adjust the exponent

Page 17: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

To normalise a To normalise a negative number in negative number in binarybinary1 111100100 0000111 111100100 000011

1.1. put in the binary point and convert the exponent to put in the binary point and convert the exponent to decimal:decimal:

1.111100100 Exponent 31.111100100 Exponent 32.2. Move the number to the correct position and count Move the number to the correct position and count

the number of movements to the left to get the the number of movements to the left to get the binary point before the first 0binary point before the first 0

1.001000000 required 4 left places1.001000000 required 4 left places3.3. Take 4 away from the exponent of 3: 4-3 = -1Take 4 away from the exponent of 3: 4-3 = -14.4. Convert -1 back to binaryConvert -1 back to binary

1.001000000 111111.001000000 11111

Page 18: Fractions in Binary. Fractions Binary doesn’t just do integers (whole numbers) it can also be used for fractions though it isn’t as accurate as decimals

TaskTask

Do question 6 and 7 on page 198Do question 6 and 7 on page 198

Read the hint on page 198Read the hint on page 198 Do exercises on page 199Do exercises on page 199