A function for decimal to binary conversion

758 Views Asked by At

I want to convert a decimal (base 10) number to its binary (base 10) equivalent. The binary string has to be of infinite length. Is any of the following functions correct for non-negative integers $x$: $$ x = \sum_{i=0}^\infty 2^i $$ or $$ x = \sum 2^i ; i \in \{ 0,1,2,...\} $$ for unique $i$, in both cases.

Edit: I know that a more appropriate function would be $$ x = \sum_{i=0}^\infty y_i2^i ; y_i \in \{ 0,1\} $$ but I wanted to know if any of the above two formulations would be equivalent to this.
Thanks

3

There are 3 best solutions below

6
On BEST ANSWER

If your $x$ is between $0$ and $1$, you can write $x=\sum_{i=1}^\infty a_i2^{-i}$ where $a_i \in \{0,1\}$ are binary digits of the expansion. If it is not, you can add the integral part of $x$ converted to binary to this expression. You can't have an infinite binary string to the left of the fraction point as the value would be infinite.

0
On

Trying to catch the direction of the question, probably with something like taking advantage of the fact that 10=5*2 y making use of modular arithmetic to calculate the decimal overflow, i could'n t imagine other way without entry in a kind of complex "iterating" stuff... and then transform to binary.

pd: Like a detour... https://en.wikipedia.org/wiki/Quote_notation

0
On

I have a function that does just that:

This function counts the number of digits of the binary representation of 'n':$$P(n)=⌈\log_{2}(n)⌉-⌈\log_{2}(n)-⌊\log_{2}(n)⌋⌉+1$$

And this function computes the decimal representation of the n in binary:

$$B(n)=\sum_{i=1}^{P(n)} (⌊\frac{n}{2^{i-1}}⌋\mod2)10^{i-1}$$