1

I'm given the number: $$8.881784197001252 \cdot 10^{-16}$$ I know it is $2^{-50}$ but suppose I didn't know. I need to convert it to binary.

One way is to apply the school taught algorithm on the fractional part. That'd be:

$.881784197001252\cdot2=1.763568394 \to 1$

$.763568394 \cdot 2=1.527136788 \to 1$

$.527136788 \cdot 2=1.054273576 \to 1$

$.054273576 \cdot 2= 0.108547152 \to 0$

$.108547152 \cdot 2=0.217094304 \to 0$

$...$

It may go on forever so I'd need to determine when to stop.

I then thought I could write the number as: $$8881784197001252 \cdot 10^{-31}$$ Now the number is an integer and I can precisely convert it. It's: $$11111100011011110111110001000000010001011000000100100$$ The problem is that I don't know how to convert the base and its exponent from base 10 to base 2. I can approximate it after some attempts. It should be between $2^{-100}$ and $2^{-99}$.

So what I'm asking is: what is the correct approach to convert that number? And, if different from the former, what method does the computer use?

zcb
  • 11
  • 2

1 Answers1

1

There is more than one correct approach to convert the number. The former is a correct algorithm for the problem. This kind of base conversion would usually be implemented in a library for the programming language you are using. There might be many such implementations out there, and I don't know what all of them are doing.

I would not recommend the latter approach, as I expect computing $2^\alpha$ where $\alpha=31 \log_2 10$ will be painful.

See also The math behind converting from any base to any base without going through base 10? and https://math.stackexchange.com/q/48968/14578 and https://en.wikipedia.org/wiki/Positional_notation#Base_conversion and https://en.wikipedia.org/wiki/Binary_number#Conversion_to_and_from_other_numeral_systems.

D.W.
  • 159,275
  • 20
  • 227
  • 470