Saturday 24 January 2015

Unclear possible rounding in converting decimal to octal number



I am doing computer science homework that wants me to write 3 pieces of code. I just started and basically I have to write a program that converts a decimal number to octal number. I looked at the professors example of the process and a you tube example of how to convert a decimal to octal number and I am really confused. I need to first understand the math in order to write the program of course. So my professor gave an example of the number 255. So here is the steps of the professor:




Key: R=Remainder



Step 1: 255/8=31 R7



Step 2: 31/8=3 R7



Step 3: 3/8=0 R3



Then the answer is basically the remainders put together (not sum) backwards for an answer of 377.




However, when I do the same steps into my calculator, this is what happens.



Step 1: 255/8=31 R8 Calculator gives 31.875



Step 2: 31/8=3 R8 Calculator gives 3.875



Step 3: 3/8=0 R3 Calculator gives .375



So at first I thought my calculator was just rounding up and I would just have to account for that when programming. However, the rule doesn't hold up in the last step 3. So my little rule was to subtract 1 from what ever remainder my calculator gives. So step 1 and 2 would then match up with the professors but then step 3 would fail as it actually matches up originally. So I looked on you tube for tutorial on this topic and still the same sort of logic just with a different number. Then I thought, ok so subtract 1 each time until the last step. In the last step leave it. I looked on this forum for another example, and my new rule still fails and was actually the way I originally would have done it in the first place. Here is a link to this sites forum of this topic that I used:




Decimal Number to Octal



So lets look at this forums steps of converting the number 9243. The forums steps is as follows:



Step 1: 9243/8=1155 R3 My calculator answer is 1155.375



Step 2: 1155/8=144 R3 My calculator answer is 144.375



Step 3: 144/8=18 R0 My calculator answer is 18




Step 4: 18/8=2 R2 My calculator answer is 2.25



Step 5: 2/8= 0 R2 My calculator answer is .25



Answer is 22033



So this forums answer is doing it exactly like what I expect but then the other 2 examples of you tube and my professor has something a little different. So as you can see, this is kind of confusing and if anyone can help me out, that would be greatly appreciated. Thanks in advance.



P.S My tag maybe wrong as there was no tag specifically for octal number, or converting, or just decimal, or anything related to this thread.



Answer



Your calculator does full decimal division while what you want for base conversion is integer division with remainder. When your prof writes $255/7=31R7$ your calculator is continuing the division and writing $7/8=0.875$ To use your calculator for this you should take the integer part of the quotient for the next division step, then multiply the fractional part by the new base to get the remainder, so you would do $255/8=31.875$, so you pass $31$ on to the next step and the remainder is $0.875\cdot 8=7$. At the end when you do $3/8=0.375$ the quotient is $0$ so you know to stop and the remainder is $0.375 \cdot 8=3$. In the conversion of $9243$ when you get $18.0$ as the quotient the remainder is $0$ and when you get $2.25$ the remainder is $0.25 \cdot 8=2$


No comments:

Post a Comment

real analysis - How to find $lim_{hrightarrow 0}frac{sin(ha)}{h}$

How to find $\lim_{h\rightarrow 0}\frac{\sin(ha)}{h}$ without lhopital rule? I know when I use lhopital I easy get $$ \lim_{h\rightarrow 0}...