

Well, this is a geometric series of fractions, and the sum of such series lies in the boundaries, so the maximum number such sum can give us is 1. If the maximum number of x digits is 1, than we can simply replace x’s with 1’s and write the sum as: Let’s think of how big the sum of these numbers can be. To determine what digit it has let’s take a look at the remaining summands: Okay, here we’ve singled out x1, and we know that it can be either 1 or 0. Let’s do that:Īnd we can then move 1/2 to the left part It’s immediately obvious that we can simply factor out 1/2 in the right part of expression. The first thing to notice here is that negative powers of 2 give us fractions with the denominator of 2 with positive powers. Similarly to integer part, let’s pretend we don’t know how this number is represented in the binary and write it out with unknown digits replaced with x:Īs with integers, our task is to find all x’s by singling out x’s. I’m going to use the fractional number 0.375 from the first part of the article. To show why we multiply by 2 and take the integer part when converting fractions to binary, I’ll also be using base-q expansion form for fractions. Make sure you understand how we arrive at that representation as we will need it when exploring how the algorithm of converting from binary to decimal works. We can also put the calculations for those four steps into one representation like this So in this way you can see how we arrived at the algorithm described in in the beginning. Let’s take the steps outlined above and move the 2 to the left part of the expressions: Remember that we started with the idea to show why the algorithm that involves diving by 2 works.

So the number 12 in binary using the algorithm described above is represented as 1100. It’s clear now that the remainder in each step corresponds to the value of x’s in the corresponding positions: first remainder corresponds to the first x, second remainder to the second x and so on. Here is how our conversion looks by steps: Since we end up with the quotient of 0, there’s nothing else to work with and this was our last step. But, since for our algorithm we need a quotient, let’s rewrite the previous expression so that it has a quotient: Since the quotient is equal to 1, there’s only one summand left, so let’s rewrite the previous expression: Let’s follow this pattern and see what we get. We can continue factoring 2 until the quotient is zero. Let’s rewrite it and also factor out 2 again: Here, applying the same logic from above we can see that x1is equal to 0. We can write out the polynomial inside the parenthesis as a separate statement: Let’s continue finding out the remaining x’s. It’s also easy to see the sum of the values inside the parenthesis is equal to 6. Since all summands from x1 to xN are multiples of two, we can factor out 2 to single out x1. Next, we need to find the value for the x1. Here we have number 12 which is even, so x0 is zero. Now, using this information we can infer the digit for the x0 - if the integer being converted is even, then x0 is equal to 0, if it’s odd - then x0 must be 1. The first thing we need to notice here is all summands except for the last one will be even numbers, because they all are multiples of two. First, let’s pretend we don’t know how it is represented in the binary and write it out with unknown digits replaced with x: Converting binary integer to decimalĪs it turns out, we can use this base-q expansion form to convert a number from decimal to binary system. Because of that fact, you’re never going to have 0.1+0.2 = 0.3. Depending on how many bits of precision are available, the floating-point approximations of 0.1 and 0.2 could be slightly less or greater than there corresponding decimal representations, but never equal. In order to store them as a IEEE-754 floating point they have to be rounded to the number of available bits for mantissa - 10 bits for half-precision, 23 bits for single-precision or 52 bits for double-precision. For example, denominators of 0.1 (1 / 10) and 0.2 (1 / 5) are not powers of two, so these numbers can’t be finitely represented in a binary format. Only fractions with a denominator which is a power of two can be finitely represented in a binary form. So, 0.375 in decimal system is represented as 0.011 in binary. Now, let’s just write out the resulting integer part at each step - 0.011. Here is an example of such conversion using the fraction 0.375. Then just write out the integer parts from the results of each multiplication. Continue multiplying by 2 until you get a resulting fractional part equal to zero.

To convert fraction to binary, start with the fraction in question and multiply it by 2 keeping notice of the resulting integer and fractional part.
