Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
0.d-1d-2d-3...
That is, it has some digits after the radix point. We're going to apply the following algorithm to solve the problem. For now, just assume that you can use negative indexes into arrays.
i n d e x=1; w h i l e(n u m>0) { n u m=n u m*b a s e;/ /S h i f tr a d i xp o i n tt or i g h tb y1 . i f(n u m> =1) / /C a s e1 :n u mi sg r e a t e rt h a no re q u a lt o1 { d i g i t [i n d e x]=( i n t )n u m; / /k e e po n l yi n t e g e rp a r t n u m=( i n t )n u m; / /g e tr i do fv a l u el e f to fr a d i xp o i n t } e l s e / /n u mi sl e s st h a n1 d i g i t [i n d e x]=0; i n d e x -; }
The key step is to realize what you're doing. You're multiplying by the base, which is equivalent to shifting the radix point over by 1. Let's imagine what this does:
d-1 . d-2d-3...
At this point, d-1 is now left of the radix point. Since we multiplied by K, the only values d-1 can be are 0,..., K-1. So, the idea is to see what value is left of the radix point. In a program, we can cast a floating point value to an int, to get the value left of the radix point (i.e., to throw away the rest of the fraction). That value will be di.
www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/fracToBaseK.html 1/2
5/24/13
We know that if, after we multiply by K, num > 0, then di must be between 1 and K - 1. Once we determine what di is, and that it's value is greater than 1, then we need to get rid of the value left of the radix point. Otherwise, when we make the check whether num > 0 in the ifstatement, once it's true, it will be always true. We don't want that to happen. Unfortunately, this algorithm may not terminate. For example, if you try to convert 0.3 to base 2, you'll end in an infinite loop. Fortunately, since 0.3 is rational, there will be a repeated set of digits, so with careful coding, you can find stop the loop once you see a set of digits repeat. The loop terminates once the value reaches 0. So, let's apply this algorithm to convert 0.625 to base 2. Multiply 0.625 by 2. This results in 1.25. This value is bigger than 1, so find the integer part, which is 1. Set d-1 = 1 (i.e., set it to the integer part). Since 1.25 is greater than or equal to 1, get rid of the integer part. This results in 0.25. Decrement the index to -2. Multiply 0.25 by 2 to get 0.5. This value is smaller than 1, so let d-2 = 0 (i.e., set it to the integer part, left of the radix point). Since 0.5 is less than 1, leave it alone. Decrement the index to -3. Multiply 0.5 by 2 to get 1.0. This value is greater than or 1, so let d-3 = 1 (i.e., set it to the integer part, left of the radix point). Since 1.0 is greater than or equal to 1, get rid of the integer part. This results in 0.0. We're at 0, so exit the loop. The value we get is 0.1012. If you convert it back to base 10, you will find that it does equal 0.625.
Summary
The key idea of converting a fraction (i.e., a value with a finite number of digits right of the radix point) is to multiply repeatedly by K, and determine if the result is bigger than 1 or not. If so, store the integer part in an array. If not, store 0 in the array. Then, subtract off the integer part. Repeat until the number is 0. If the number is 0, it may simply be a repeated fraction. To convert arbitrary floating point in base 10 to arbitrary bases, solve the problem by converting the integer part to base K and the fractional part to base K. Add the two parts together.
www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/fracToBaseK.html
2/2