Description
Fractional numbers can also be converted from one base to another. To convert a fractional number to another base, you should use the algorithm described below.
Algorithm
As you know from the previous stage, in order to convert a number from one base to another, first, we need to convert it to decimal if it’s not decimal yet, and only then convert it to another base. The same applies to fractional numbers.
Let’s imagine you have a fractional number where is the integer part, is the fractional part, and are some digits or letters, depending on the base of the number.
In this case, we have 2 digits (letters) in the integer part and 4 digits (letters) in the fractional part. In other cases, the number of digits (letters) in parts can be different.
To convert the number into decimal, we need to:
- Split the number into two parts: integer and fractional;
- Convert the integer part into decimal using the method from the previous stage;
- Convert the fractional part into decimal using the following formula:
where is a base of the number you want to convert into decimal.
The more digits (letters) in the fractional part, the more addends in the formula. If the fractional part has letters, then you should use their number representation: ‘a’ – 10, ‘b’ – 11, c – ‘12’, and so on.
As a result, you should get a decimal number no greater than 1. If it is greater than 1, you did something wrong.
To see the decimal representation of the source number, you can sum the decimal integer and the decimal fractional parts.
To convert a decimal fractional number into any other base, we need to:
- Split the decimal number into two parts: integer and fractional;
- Convert the integer part into the new base;
- Convert the fractional part from decimal to any other base.
Let’s elaborate on the third step and convert the fractional part of 0.5168 into base 19 as an example.
Multiply the fractional part by the new base: . The integer part of the result is the first digit (or letter if the integer part is greater than 9) in the fractional part of a number in the new base. In this case, the first digit in the fractional part is 9.
Take the fractional part from the result of the multiplication and multiply it by the new base again: . For numbers greater than 9, you should use their letter representation: 10 – ‘a’, 11 – ‘b’, 12 – ‘c’, and so on. In this case, the second digit (letter) is f (15).
To calculate the rest of the digits (letters), you should repeat this action. In this stage, your program should output only 5 digits (letters) in the fractional part:
The final result looks like this:
Example 1:
to base 7.
- : the first fraction symbol is 1.
- Remove integer part. : the second fraction symbol is 4.
- Remove integer part. : the third fraction symbol is 3.
- The same for the next symbols.
So, the number in base 7 is
Example 2:
to base 36.
- : the first fraction symbol is 8.
- : the second fraction symbol is '15' and 'f' in base 36.
- : the third fraction symbol is 9.
So, the number in base 36 is
Example 3:
to base 36.
- Convert the fractional part to base 10 using the formula above:
- Convert this number to base 36 like in example 2.
This stage is auto-graded. The grader will input three lines: the source radix, the source number, and the target radix. Then it will check that your output is the correct number representation in the given radix. Don't forget to round up the result to 5 decimal places! If there is no fractional part in the initial number, you don't need to show the fractional part.
Also, numbers in radix 1 cannot have a fractional part so they're tested only as the integer part, like it was in the previous stage.
Examples
Example 1:
Input:
10
0.234
7
Output:
0.14315
Example 2:
Input:
10
10.234
7
Output:
13.14315
Example 3:
Input:
35
af.xy
17
Output:
148.g88a8
Example 4:
Input:
16
aaaaa.0
24
Output:
22df2.00000
Example 5:
Input:
21
4242
6
Output:
451552
Example 6:
Input:
1
1111111
10
Output:
7