Practice
Senior Example Problem 3: Bicimals
Converting decimal fractions to binary
- The conversion of a decimal fraction to its binary equivalent is achieved by repeated multiplications by the number 2, noting the carry digit.
Example
- Consider the following example of converting the decimal fraction 0.625 to its binary equivalent.
- Thus the binary fraction equivalent of decimal 0.625 is binary 0.101.
- Most decimal fractions do not convert exactly and need to be truncated at some stage. For this exercise you will stop after 30 places.
| Step 1 |
Write the number .625 down and draw a horizontal line beneath it and a vertical line between the decimal point and the 6.
|
|---|---|
| Step 2 |
Multiply the fraction (shown on the right of the vertical line) 625 by 2 and write the answer (1250) directly beneath it. The "carry" 1 is written below the decimal point, to the left of the vertical line. If there is no carry, then 0 is written to the left of the vertical line.
|
| Step 3 |
Repeat step 2, multiplying the fractional part of the number (shown to the right of the vertical line) by 2, and writing down the "carry" to the left of the vertical line. The carry will be either "0" or "1". Continue doing this either thirty times or until the fraction to the right of the vertical line becomes zero. In the example the process stops after the third multiplication.
|
Instructions
- Write a program to convert decimal fractions to binary fractions
- Your program should recieve and convert input decimal numbers until the number 0.0 is entered. No output should be displayed after 0.0 is detected.
- Enter the following data either manually, or via file redirection using the following UNIX text file problem3input.txt, or one you create.
- For each data element, your program should output the binary equivalent (with no more than 30 decimal places), one per line.
- No other output should be produced by your program other than the numbers.
- Your output should be based in the following input.
Data
0.0625
0.1
0.01
0.0125
0.8
0.0