# Float number to binary

I would like to convert 0,347 and 0,9828 to binary, how can I do that?

I know that sucessive multiplication by 2 can do this, but this method seems very painful and even ineffective since the size of count.

This is for a question, and I think that sucessive multiplication is needed. But, in 0,347, I did more than 20 multiplication and did not reach result. Is this normal?

• In what setting are you converting the representations in? In program code? By hand? Somewhere else? Commented Apr 9, 2019 at 9:48
• @dkaeae By hand Commented Apr 9, 2019 at 14:05

If I understand your question correctly, you want to determine the binary representation of a floating-point number by hand. I assume you are talking about the IEEE 754 floating-point format, since this is the most common format.

You can indeed do this by successively multiplying with 2. However, just multiplying does not result in anything useful, you should also subtract one (1) every time the intermediate result is greater than or equal to 1. To understand this, you must understand how floating-point numbers are stored. This is explained pretty well on Wikipedia (and probably many other sources), including the algorithm you mean (see here).

To do this by hand is quite painful indeed. It's normal that you have to do more than 20 multiplications. You first need a few multiplications to normalize the number and determine the exponent (i.e. scale the number such that it has the form 1.xxxxx * 2^exponent). After that, each multiplication yields exactly one bit of the mantissa. A single-precision floating-point number has a 23-bit mantissa, so to convert the number 0.347 to single-precision floating-point, you need to do 25 multiplications. A double-precision floating-point has a 52-bit mantissa, so you'll need 54 multiplications!

NB: For sake of simplicity, I did not take into account numbers larger than 1, negative numbers, special numbers (such as NaN, +/- infinity, denormal numbers, etc.) and other aspects such as rounding. These cases seem to be out of the scope of your question.

First you look at the position of the decimal sign and a possible exponent, and you find that the result you want is “347” divided by $$10^3$$ and “9828” divided by $$10^4$$.

You convert “347” and “9828” to floating point - there is no multiplication by 2 involved, but multiplication by 10. You probably get the power of 10 by looking up a table.

Doing this with high precision is harder.

• This is for a question, and I think that sucessive multiplication is needed. But, in 0,347, I did more than 20 multiplication and did not reach result. Is this normal? Commented Apr 9, 2019 at 14:09
• I don’t know what you are multiplying, but I’d multiply x = 3*10+4, y = x*10+7. Then divide 347 /1000. Using a computer, not by hand. Commented Apr 9, 2019 at 15:50

Successive doublings is a good way, you can virtually do it mentally (double every digit modulo $$10$$, and if the next is $$5$$ or more, add a carry).

$$0.347\\0.694\\1.388\\2.776\\5.552\\11.104\\22.208\\44.416\\88.832\\177.664\\\cdots$$

So far, $$0.347\approx\dfrac{177}{512}=0.010110001$$. Straight multiplication by a large power of $$2$$ is also possible, but not necessarily easier/faster.

As you noticed, the process will never stop. This is because no power of $$10$$ is also a power of $$2$$, so

$$\frac n{10^k}2^l$$ is never an integer, and the given number is unlimited periodic fractional in base $$2$$. (Some more digits: $$0.0101100011010100111111011111001110110110010001011010000111001010110000001000001100010010011011101001011110001101010011111101111\cdots$$)

You can do faster with a table of digits over powers of $$10$$:

$$0.1\to0.00011001100\cdots\\0.2\to0.00110011001\cdots\\0.3\to0.01001100110\cdots\\\cdots\\0.01\to0.00000010100\cdots\\0.02\to0.00000101000\cdots\\\cdots$$

Yet another option is to do with a table of inverse powers of $$10$$ and perform a binary multiply:

$$0.347=347\cdot10^{-3}=101011011\times0.000000000100000110001\cdots$$