# how to calculate logarithm base 2 of number very close to 1

**URL:** https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811
**Category:** Factual Questions
**Created:** [April 18, 2011, 7:29am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811 "2011-04-18T07:29:07Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![the\_healer](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@the\_healer](https://boards.straightdope.com/u/the_healer)
#### Post date: [April 18, 2011, 7:29am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/1 "2011-04-18T07:29:07Z")

</div>

I need to calculate logarithm base 2 of number very close to 1. for example  
log2(0.9999999999999999999999999999999…). The expected result is around 2^-80.  
I can’t calculate it even with Matlab.  
Anyone can help me or tell me where I can ask this question?

---

<div class="post-metadata">

### Author: ![friedo](https://avatars.discourse-cdn.com/v4/letter/f/8edcca/32.png) [@friedo](https://boards.straightdope.com/u/friedo)
#### Post date: [April 18, 2011, 7:32am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/2 "2011-04-18T07:32:10Z")

</div>

0.9999999999999999999999999999999… is equal to one, unless you don’t actually mean “…”.

Log[sub]2[/sub] of 1 is pretty easy.

---

<div class="post-metadata">

### Author: ![the\_healer](https://avatars.discourse-cdn.com/v4/letter/t/d26b3c/32.png) [@the\_healer](https://boards.straightdope.com/u/the_healer)
#### Post date: [April 18, 2011, 7:36am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/3 "2011-04-18T07:36:58Z")

</div>

but I really need to know the result

---

<div class="post-metadata">

### Author: ![Simplicio](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@Simplicio](https://boards.straightdope.com/u/Simplicio)
#### Post date: [April 18, 2011, 8:11am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/4 "2011-04-18T08:11:42Z")

</div>

> [@the\_healer](#):
>
> but I really need to know the result

Whats the actual number?

---

<div class="post-metadata">

### Author: ![Simplicio](https://avatars.discourse-cdn.com/v4/letter/s/c37758/32.png) [@Simplicio](https://boards.straightdope.com/u/Simplicio)
#### Post date: [April 18, 2011, 8:21am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/5 "2011-04-18T08:21:57Z")

</div>

I think I would just approximate the function with its derivitive, f(a-e)=f(a)-f’(a)\*e. So if you want a=log2(1-e),

a is approximately = -ln(2)\*e

err…I think, check my math.

---

<div class="post-metadata">

### Author: ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)
#### Post date: [April 18, 2011, 9:35am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/6 "2011-04-18T09:35:02Z")

</div>

> [@Simplicio](#):
>
> I think I would just approximate the function with its derivitive, f(a-e)=f(a)-f’(a)\*e. So if you want a=log2(1-e),
> 
> a is approximately = -ln(2)\*e
> 
> err..I think, check my math.

Your variables are a bit wonky but the idea is right. f(1+x) = f(1) + x\*f’(1) for small x. Plugging in log2 for the equation, you get log2(1+x) = log2(1) + x/ln(2) = x/ln(2). So log2(0.999) = log2(1-0.001) = -0.001/ln(2) =~ -0.001443. Put another way, log2(x) = (x-1)\*1.44269504 for x close to 1.

---

<div class="post-metadata">

### Author: ![Sage\_Rat](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/sage_rat/32/399_2.png) [@Sage\_Rat](https://boards.straightdope.com/u/Sage_Rat)
#### Post date: [April 18, 2011, 10:56am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/7 "2011-04-18T10:56:38Z")

</div>

[http://www.ttmath.org/online\_calculator](http://www.ttmath.org/online_calculator)

I got:

log(0.999999999999999; 2) = 0.000000000000001442695040888964128707445125484076715735949 443262181715907668254515541183600667829599927212979023238198 822572497286463865138342025328811096096342347440663573000609 426906195738196626253591932574631143031016729212544366372617 870460414673855671086411066595739984138285781245770704151479 049947987992248470262399603359441939405606501247516271322228 897178565056598424307710220948686176030633044439547205812729 316368378396758284976041913557680104033736588039191899120329 948088920984812202294383173353027198853150314942318165504988 908358816757815036256630282086590182633858107794046002517470 282988127452852237554924447384019

You might also check out some of the other options listed on this page:

[http://en.wikipedia.org/wiki/Arbitrary-precision\_arithmetic](http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic)

---

<div class="post-metadata">

### Author: ![BigT](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/bigt/32/12044_2.png) [@BigT](https://boards.straightdope.com/u/BigT)
#### Post date: [April 18, 2011, 11:15am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/8 "2011-04-18T11:15:15Z")

</div>

The real problem seems to be precision. I mean, you can figure out binary logarithms in Windows Calculator due to the division identity function:  
log[sub]y[/sub] x = (log[sub]n[/sub] x)/(log[sub]n[/sub] y) for n \> 0.

I just did it in Windows Calculator.  
2[sup]2[sup]-80[/sup][/sup] = 1.0000000000000000000000005733579  
log 1.0000000000000000000000005733579 / log 2 = 8.2717930055897339235366409819068e-25  
ln 8.2717930055897339235366409819068e-25 / ln 2 = -80.000002288265450326402515099512

But, as you see, there’s the problem. you only get about 5 decimal places of precision in each logarithm. Even if I execute it all as one function, I get log[sub]2[/sub] (log[sub]2[/sub] 2[sup]2[sup]-80[/sup][/sup]) = -80.00000000000000012413013184653, which is only accurate to the 15th decimal place, while the original answer doesn’t even deviate from 1 until the 24th.

Enter a program called TTCalc, which can handle precision up to 306 valid decimal digits at the highest precision setting. It can also handle base 2 logarithms natively. Here are the results of the above calculations (using the program’s notation):

2^(2^-80)  
1.00000000000000000000000057335790940497963733901464400142546462114787933891298487511422418833945989010599952953218477956396808118174176264229766997132697041138686715915305724275075956123440209170975144272732078325389219183750455347899889286911916547100836175214422313872111355569601341820786448313185541444

log(1.000…444;2)  
8.271806125530276748714086920699628535658121109008789062499999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999992934745388900199049316678e-25

log(8.271…678e-25;2)  
-80.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000123225902969263877393498

That is accurate to the 281st decimal place. I doubt the approximation functions are even that accurate. So, unless you are supposed to be using those functions, I would just [download TTCalc](http://ttcalc.sourceforge.net/downloads.html) or, if you only have few calculations, use the online version linked in the post above.

[sub]Ellipses are used for presentation purposes only. I used copy and paste.[/sub]

---

<div class="post-metadata">

### Author: ![Thudlow\_Boink](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/thudlow_boink/32/320_2.png) [@Thudlow\_Boink](https://boards.straightdope.com/u/Thudlow_Boink)
#### Post date: [April 18, 2011, 12:22pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/9 "2011-04-18T12:22:23Z")

</div>

Have you tried [Wolfram Alpha](http://www.wolframalpha.com/)?

---

<div class="post-metadata">

### Author: ![Uncertain](https://avatars.discourse-cdn.com/v4/letter/u/6a8cbe/32.png) [@Uncertain](https://boards.straightdope.com/u/Uncertain)
#### Post date: [April 18, 2011, 12:57pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/10 "2011-04-18T12:57:13Z")

</div>

Calculating the natural log (from which you can easily get log base 2) of a number close to 1 is a fairly standard numerical function. Matlab has a function called log1p that calculates ln(1+z) and should solve your problem.

---

<div class="post-metadata">

### Author: ![Manlob](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@Manlob](https://boards.straightdope.com/u/Manlob)
#### Post date: [April 18, 2011, 2:26pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/11 "2011-04-18T14:26:16Z")

</div>

First find natural log with:

ln(1-x) = -x -(x^2)/2 - x(^3)/3 - (x^4)/4 - …

If x is very small, approximating ln(1-x) with one term of the series should be good enough.

Then divide by ln(2) to convert to log base 2.

---

<div class="post-metadata">

### Author: ![ultrafilter](https://avatars.discourse-cdn.com/v4/letter/u/3d9bf3/32.png) [@ultrafilter](https://boards.straightdope.com/u/ultrafilter)
#### Post date: [April 18, 2011, 4:46pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/12 "2011-04-18T16:46:28Z")

</div>

> [@Manlob](#):
>
> First find natural log with:
> 
> ln(1-x) = -x -(x^2)/2 - x(^3)/3 - (x^4)/4 - …
> 
> If x is very small, approximating ln(1-x) with one term of the series should be good enough.
> 
> Then divide by ln(2) to convert to log base 2.

This is probably what the Matlab function that **Uncertain** mentioned does, but I’d use that instead of trying to roll my own.

---

<div class="post-metadata">

### Author: ![Happy\_Fun\_Ball](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/happy_fun_ball/32/17664_2.png) [@Happy\_Fun\_Ball](https://boards.straightdope.com/u/Happy_Fun_Ball)
#### Post date: [April 18, 2011, 5:52pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/13 "2011-04-18T17:52:06Z")

</div>

In Matlab, use the log1p function.

---

<div class="post-metadata">

### Author: ![Lestrade](https://avatars.discourse-cdn.com/v4/letter/l/94ad74/32.png) [@Lestrade](https://boards.straightdope.com/u/Lestrade)
#### Post date: [April 18, 2011, 6:24pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/14 "2011-04-18T18:24:07Z")

</div>

Need help with homework fast?

---

<div class="post-metadata">

### Author: ![Punoqllads](https://avatars.discourse-cdn.com/v4/letter/p/d2c977/32.png) [@Punoqllads](https://boards.straightdope.com/u/Punoqllads)
#### Post date: [April 18, 2011, 6:35pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/15 "2011-04-18T18:35:07Z")

</div>

If you have access to a Unix machine (say, a Linux box or run Terminal on a Mac), run bc -l. Type in scale=200 or so, and then use the l() function for your number and divide by l(2). E.g.:

[punoqllads@redacted perl]$ bc -l  
bc 1.06  
Copyright 1991-1994, 1997, 1998, 2000 Free Software Foundation, Inc.  
This is free software with ABSOLUTELY NO WARRANTY.  
For details type `warranty’.  
scale=200  
l(.999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)/l(2)  
-.000000000000000000000000000000000000000000000000000000000000000000  
00000000000000000000000000000000000000000000000000000000000000000000  
000000000000000000000144269504088896340735992468100189213742664595

---

<div class="post-metadata">

### Author: ![vinniepaz](https://avatars.discourse-cdn.com/v4/letter/v/b4bc9f/32.png) [@vinniepaz](https://boards.straightdope.com/u/vinniepaz)
#### Post date: [April 19, 2011, 4:12am UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/16 "2011-04-19T04:12:27Z")

</div>

> [@Punoqllads](#):
>
> If you have access to a Unix machine (say, a Linux box or run Terminal on a Mac), run bc -l.

Hey, cool! I came here to suggest his. One of my professors wrote this program. It’s an extremely impressive piece of software

---

<div class="post-metadata">

### Author: ![Manlob](https://avatars.discourse-cdn.com/v4/letter/m/96bed5/32.png) [@Manlob](https://boards.straightdope.com/u/Manlob)
#### Post date: [April 19, 2011, 1:34pm UTC](https://boards.straightdope.com/t/how-to-calculate-logarithm-base-2-of-number-very-close-to-1/578811/17 "2011-04-19T13:34:46Z")

</div>

When using bc, you can divide by l(10) for log base 10 to help convert to scientific notation, since (last I checked) bc couldn’t do that.

Notice how all the significant figures in the result Punoqllads gives match the result obtained by approximating ln(1-x) by -x.
