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

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?

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

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

but I really need to know the result

Whats the actual number?

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.

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

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 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]

Have you tried Wolfram Alpha?

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.

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.

In Matlab, use the log1p function.

Need help with homework fast?

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

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

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.