0

I am attempting to work out, by using a manual method, how to apply the floor() and ceiling() functions to log2(2096) (2096 is used as an example).

My understanding is this (and I am very much a beginner):

First, by using a scientific calculator calculating what log2(2096) is by; 3.32 * log(2096) = 11.027

And then from here I can see that that floor = 11 and ceiling = 12 so ceiling(x) = floor(x) +1

But when I review my answer, I am incorrect (usually due to how I calculate log2(2096). How do I remedy this?

  • Your answer is correct but this example is a bad one. Suppose you had to calculate $\log_2(2049)$, since 2049 is close to $2048=2^{11}$, any small error may result in you getting floor as 10 instead of 11. – Gautam Shenoy Mar 18 '13 at 10:12
  • @GautamShenoy This is why I think multiplying by 3.32 might be wrong as when I do log(2049) on the calculator I get ~3.311 which when I multiply it by 3.32 I get ~10.999. Should I substitute 3.32 for something higher? – Switchkick Mar 18 '13 at 10:45
  • 1
    Do you know what that 3.32 is? It is an approximation of $\log_2 10$. This is an irrational number. So all you have to do is replace it with a more accurate version. – Gautam Shenoy Mar 18 '13 at 11:21
  • 1
    Use the change of base formula for logarithms. $$\log_a(b)=\frac{\log_cb}{\log_ca}$$ So for your case, a would be 2, b would be 2096, and c would be 10 – Brian Silva Mar 18 '13 at 12:35
  • @BrianSilva this just makes plain and simple sense to me now. Thank you! – Switchkick Mar 21 '13 at 06:40

1 Answers1

1

As Gautam Shenoy said in comments, the issue here is that the formula $$\log_2 n \approx 3.32 \log_{10} n \tag1$$ introduces a numerical error. With $n=2049$, the left side of (1) is $11.0007 \dots$ but the right side is $10.9943\dots$ Taking the floor or ceiling in such a situation exacerbates the problem.

Possible remedies:

Use the more precise value of $\log_2(10) = 3.32192809\dots$

(If writing a code) replace computation of logarithm with something like

k=1  
l=0  
while n>k do l++; k*=2  
return l