2

I have number $-1e35$, and I'm supposed to convert it to binary. The answer is : $-1.10101001010110100101101...\text{e}–117.$

I can't figure out how to get this! and how we can calculate numbers partially as you see here. Originally it is a programming practice, but what I care about is the mathematical part.

I tried to solve it like this: $\log 10^{35}=\log 2^x.$ The result is: $116.666$.

My question is not exactly how to convert. I know how to convert smaller numbers, but I am confused with scientific notation, and how to represent part of the number.

amWhy
  • 209,954
shayan
  • 145

1 Answers1

2

When you represent a number in decimal scientific notation, the base of the exponent is $10$, so $1E35=1\cdot 10^{35}$. In binary, the base is $2$, so you are trying to solve $1\cdot 10^{35}=m2^e$ where $1 \lt m \lt 10_2$ is the mantissa and $e$ is an integer exponent. To find $e$ we can take logs: $$1 \cdot 10^{35} = m2^e\\ 35 \log_2(10)=e+ \log_2 (m)\\e=\lfloor35 \log_2(10)\rfloor=116_{10}$$ Now to get the mantissa you can just subtract off powers of $2$ $$10^{35}-2^{116}=16923250263442757943512058732478464\\ \lfloor \log_2(16923250263442757943512058732478464)\rfloor=113$$ so we start with $1.001_2$ because the exponent dropped by $3$ $$16923250263442757943512058732478464-2^{113}=6538656546373102686451066074038272\\ \lfloor \log_2(6538656546373102686451066074038272)\rfloor=112$$ so our mantissa becomes $1.0011_2$ and so on until you get tired, your word fills up, or you get to the end. The final answer is $$1.001101000010011000010111001011000111010011011000001000101011100001111000111111101_2E116$$

Ross Millikan
  • 374,822
  • i am trying to repair the math part of my brain , I've been away for a long time , in the sentence : so we start with 1.001(2) , 2 is the base ?? – shayan Feb 03 '16 at 19:12
  • and another question , why you have rounded it to 116? – shayan Feb 03 '16 at 19:36
  • 1
    Yes, binary means base 2. I took the floor to get $116$. We have $2^{116} \lt 10^{35} \lt 2^{117}$ and want $1 \lt m \lt 2$. – Ross Millikan Feb 03 '16 at 20:12
  • @RossMillikan I obtain $e = 35 \log_2 10 - \log_2 m$, then it is not clear why $e=\lfloor35 \log_2(10)\rfloor=116_{10}$, please, can you explain better? Thanks! – JB-Franco May 25 '20 at 17:06
  • 1
    @JB-Franco: Take the base $10$ log of the first centered equation, which gives $35=e\log_{10}2+\log_{10}m$ Now use $\log_{10}2=\frac 1{\log_210}$ to move that term over and take the floor to get rid of $\log_{10}m$, which is between $0$ and $1$ – Ross Millikan May 25 '20 at 17:12
  • @RossMillikan ok, I understand the passages, but still I don't understand why we have to get rid of $\log_{10} m$, and also why the mantissa is between $1 \lt m \lt 10_2$. If I am right, the mantissa is it the part, for example in half precision, having 10 bits in it? – JB-Franco May 25 '20 at 17:38
  • @JB-Franco: We need an integer result for $e$ because that is the way the notation is described. – Ross Millikan May 25 '20 at 17:42
  • does the floor always decreasing in each step? – JB-Franco May 25 '20 at 19:00
  • @JB-Franco: Do you mean does it decrease after we have found each digit in the expansion? Yes. We had $1 \le m \lt 2$ and we subtracted off $1$, so now it is less than $1$. We need to multiply by at least one factor of $2$ to get it back into range. – Ross Millikan May 25 '20 at 19:07
  • And also are these passages valid also for cases different from $1$, similar to $2 \times 10^{35}$, or with negative exponents? – JB-Franco May 25 '20 at 19:08