So far we have learned how to represent any integer for some arbitrary base b.
But what about representing non-integer values, such as 1/2 or 3/8? At first
the idea of representing a fractional value in binary may seem strange, but let's
remind ourselves how we would represent these values using standard decimal
notation.
3/8 as an example, we first observe that our value is less than 1,
so we write down 0 followed by a decimal point. If our value was greater than
1 then we would write down the decimal notation of that portion greater than
zero, and leave the remaining "fractional value" after the decimal place for the
following steps.0.0 and
1.0. We divide up that unit into 10 sections (counting the initial section as the
0-th) and observe that the value falls within the 3rd section, so we write
down a 3 to have 0.3 so far.0.3 and 0.4. If we again divide that
range into 10 sections, this time we find that the value lies in the 7th
section, so we write down a 7 to have 0.37 so far.0.37 and 0.38. If we again divide that
range into 10 sections, this time we find that the value lies exactly at the
lower bound of the 5th section, so we write down a 5 to have 0.375 which is
our final answer.So let's follow that same process to obtain the representation of 3/8 in binary.
1 we write down 0 followed by a radix point
(no longer called a decimal point since we are no longer in decimal).0.0 and 1.0, we divide that range by 2 (with the
initial section called the 0-th) and see that the value lies in the 0-th half.
So we write down a 0 to have 0.0 so far.0.0 and 0.1, which
correspond to the decimal values 0.0 and 0.5. We see that our value lies in
the latter half of this range, so our answer so far becomes 0.01.0.01 and 0.10.
We observe that our value lies exactly at the lower bound of the latter section, and
so our final answer is 0.011.Be sure not to refer to values to the right of a radix point as decimals
when working in bases other than 10, as this will only cause widespread confusion
and chaos.
Input Data
First line will be Q, the quantity of testcases.
Q lines will follow, each containing two space-separated integers in the format
X Y which describe some fraction X / Y.
Answer
Should consist of Q space-separated values corresponding to the binary representations
of each given fraction.
Truncate all results to the first 10 digits following the radix point.
Example
input data:
5
1 2
3 8
2 3
83 89
89 13
answer:
0.1 0.011 0.1010101010 0.1110111010 110.1101100010