Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Project 02 Materials / C08_YourID_Exercise08

.docx
Скачиваний:
22
Добавлен:
14.10.2016
Размер:
1.26 Mб
Скачать

exercise 08: why does 0.1 have problems?

Application Development

Name: <your name goes here>

Purpose: This exercise introduces the basic idea behind floating point values and the notion that binary values have repeating fractions that differ from decimal repeating fractions.

Perform each of the following activities. If you have questions, issues, or doubts, please ask for help and do not just guess!

  1. Be sure the file name of this document starts with your School ID which is followed by a single blank and that is followed by “Exercise08.doc”.

  2. Be sure you have replaced the “<your name goes here>” (including the “<” and the “>”) with your name.

You will need the help of this table to solve your assignment.

Powers of 2

Binary equivalent

Decimal equivalent

2-1

0.1

0.5

2-2

0.01

0.25

2-3

0.001

0.125

2-4

0.0001

0.0625

2-5

0.00001

0.03125

2-6

0.000001

0.015625

2-7

0.0000001

0.0078125

2-8

0.00000001

0.00390625

2-9

0.000000001

0.001953125

2-10

0.0000000001

0.0009765625

2-11

0.00000000001

0.00048828125

2-12

0.000000000001

0.000244140625

2-13

0.0000000000001

0.0001220703125

2-14

0.00000000000001

0.00006103515625

2-15

0.000000000000001

0.000030517578125

To determine the binary equivalent of a given value we use the following algorithm:

  1. Create a variable named “remainder” and initialize it to the value (fraction) that we wish to represent in binary (base two).

  2. Establish a variable named “significantBit” starting with a value of -1.

  3. If remainder is zero or significantBit exceeds -15, stop.

  4. Is 2significantBit > remainder? (Remember that significantBit is negative!)

  • If so, set that bit in the answer to 0, decrement significantBit and return to step 3.

  • If not, set that bit in the answer to 1, subtract 2significantBit from the remainder producing a new remainder, decrement significantBit and return to step 3.

In general, a number less than one could be represented by:

x = ?*2-1 + ?*2-2 + ?*2-3 + ?*2-4 + ?*2-5 + … + ?*2-n + …

So, if we want to represent the number 0.375, we would start with

0.375 = ?*2-1+ ?*2-2 + ?*2-3 + ?*2-4+ ?*2-5 + …

remainder = 0.375

significantBit = -1

Since the remainder is not zero and significant bit has not exceeded -15, we decide if ?*2-1 should be 1*2-1 or 0*2-1. To make this decision, see if 1*2-1 is larger than the “yet to be represented” portion of the number, remainder. In this case, 0.375.

2-1 is 0.5 which it larger than 0.375, so the first term will be 0*2-1.

0.375 = 0*2-1 + 0.375

remainder = 0.375

significantBit = -2

Since the remainder is not zero and significant bit has not exceeded -15, we do it again.

Is 1*2-2 too big? 2-2 is 0.25

1*2-2 is not too big... so this term is 1*2-2

0.375 = 0*2-1 + 1*2-2 + 0.125

remainder = 0.125

significantBit = -3

Since the remainder is not zero and significant bit has not exceeded -15, we do it again.

Is 1*2-3 too big? 2-3 is 0.125

1*2-3 is not too big... so this term is 1*2-2

0.375 = 0*2-1 + 1*2-2 + 1*2-3 + 0

remainder = 0

significantBit = -4

Since the remainder is zero, we stop.

Now using the same approach find the binary equivalent of the following:

  1. 0.288

  1. 0.77

  2. Use the same approach to find the binary equivalent for the fractional value 1/10.

  1. When you are finished, look at the results (including the pattern of bits) and estimate how many binary bits will be required to completely express this value.

  1. The pattern of bits should remind you of a similar situation with fractional values being represented when expressing the value as a decimal (base ten) real value with a decimal point. Show an example of a fractional value that has this same characteristic and explain what it is?

  1. Congratulations! You have just completed the eighth exercise. Save your work and upload this exercise to the LMS following the instructions given in Exercises 1 and 2. Please be sure you have changed the name at the top of this document and properly renamed it to start with your StudentID before you began editing it, right?)

©iCarnegie - Distribution or copying without permission is prohibited.

Соседние файлы в папке Project 02 Materials