From: cameron@symcom.math.uiuc.edu (Cameron Smith)
Subject: Re: LOGS AND CALCULATORS
Organization: none
Date: Thu, 1 Oct 1992 17:05:10 GMT

In article <4328.2ACA0959@puddle.fidonet.org>,
Jon.Church@f455.n109.z1.fidonet.org (Jon Church) asks
several questions about computing transcendental functions.
I answered most of them in a previous posting; now I address
his first question:

> How do you calculate the natural log by hand?

Consider the identity

  (          2    3    4    5            n )                   n+1
  ( 1 + x + x  + x  + x  + x  +  ...  + x  )  (1 - x)  =  1 - x

Suppose that n is fairly large, and x is less than 1 in absolute
value (i.e. it's a "fraction").  Then the term "x^(n+1)" on the
right-hand side is a fraction multiplied by itself many times,
so it is very, very small.  So just forget about it!

We are, remember, computing an *approximation*, so some leeway
is permitted.  After all, *exact* values of logarithms are typically
irrational numbers, and you can't compute *them* exactly anyway!
So let's drop that pesky last term, and then divide both sides
of the resulting equation by (1-x).  We get

      1                 2    3    4    5            n
   -------  =  1 + x + x  + x  + x  + x  +  ...  + x
    1 - x

(you may -- in fact, I hope you do! -- recognize this as a truncated
geometric series).  Remember, this approximate equation is only "valid"
for small x (i.e. x with absolute value less than 1), because that's the
condition that allowed us to ignore that pesky term.  (Alternatively,
remember that the geometric series only converges when its common ratio
is less than 1 in absolute value -- *not* coincidentally!)  Now integrate
both sides of the equation; you get

                          2      3      4      5              n+1
                         x      x      x      x              x
   - log( 1-x ) =  x  +  --  +  --  +  --  +  --  +  ...  +  ----
                         2      3      4      5              n+1

Now as a series for computing logarithms, this ain't so hot.
It's not a very good approximation; it takes a fairly large number
of terms (i.e. a fairly large value of n) to get a good degree
of accuracy.  Furthermore, it's only valid for x between -1 and 1
(i.e. x with absolute value less than 1).  But by doing a bit of
algebra, we can improve both its range of application and its
"convergence" (which may be loosely defined as the rate at which
computing extra terms builds up extra accuracy in the result).

Substitute (-x) for x in the above equation and you get

                          2      3      4      5                n+1
                         x      x      x      x                x
 - log( 1+x ) =  - x  +  --  -  --  +  --  -  --  +  ...  +/-  ----
                         2      3      4      5                n+1

(the terms on the right that have even degree stay positive,
because (-1)^2 = (-1)(-1) = +1, but the terms that have odd
degree become negative; the sign of the last term depends on
whether (n+1) is even or odd).  Now *subtract* the new series
from the original one.  On the right-hand side, all the even degree
terms cancel out, while the terms of odd degree are doubled.  On the
left-hand side, we get

   - log( 1-x )  -  [ - log( 1+x ) ]
   
   =   - log( 1-x )  +  log( 1+x )
   
   =   log( 1 / (1-x) )  +  log( 1+x )
   
          (  1 + x  )
   =   log( ------- )
          (  1 - x  )

(In the second step we used the fact that the log of 1/a is the
negative of the log of a, and in the third step we used the fact
that the sum of two logs is the log of the product.)  Putting the
left-hand and right-hand computations together, we get

                          (         3      5             2n + 1  )
      (  1 + x  )         (        x      x             x        )
   log( ------- )  =   2  (  x  +  --  +  --  +  ...  + -------  )
      (  1 - x  )         (        3      5             2n + 1   )

This is still valid only for values of x between -1 and 1, remember.
But now suppose that a is any positive real number.  If you solve
the equation

    ( 1 + x )
    ---------  =  a
    ( 1 - x )

for x in terms of a, you get

    ( a - 1 )
    ---------  =  x
    ( a + 1 )

(try it and see).  We've already assumed that a is positive; if in
fact it's bigger than 1 we can easily see that this equation makes
x a positive fraction (the numerator and denominator are both positive,
and the denominator is bigger).  It's a little harder to see that if
a is between 0 and 1 the equation makes x a negative fraction, but it's
true.  So: for any positive real number a whatsoever, we can come up
with an x that has the properties that (1) its absolute value is less
than 1, and (2) it satisifies the equation

    ( 1 + x )
    ---------  =  a
    ( 1 - x )

and therefore, for this choice of x,

                    (         3      5             2n + 1  )
                    (        x      x             x        )
   log( a )  =   2  (  x  +  --  +  --  +  ...  + -------  )
                    (        3      5             2n + 1   )

For example, suppose that we want to calculate the natural log of 5.
Then a = 5, so x = (5 - 1) / (5 + 1) = 4/6 = 2/3.  Now with x = 2/3,
the series on the right gives us

   (  2   1    8   1    32   1    128     1    512     1    2048        )
 2 (  - + - * -- + - * --- + - * ----  +  - * ----- + -- * ------ + ... )
   (  3   3   27   5   243   7   2187     9   19683   11   177147       )

If we calculate this far (six terms of the series), we get

 36559324
 --------
 22733865

or roughly 1.60814379781.  The actual value of log(5) is about
1.6094379124341, so we were off by .0013, for a relative error
of 0.08 per cent (yes, less than one tenth of one percent).

You'd have to compute about 22 terms of the original series we
derived (the one for - log( 1-x ), using 4/5 as the value for x)
to compute log(5) as accurately as the six terms we've computed
in this series.

This is the best easy way I know to compute natural logarithms
"by hand" (you might still want to use a four-function calculator
to help compute and sum the terms of the series).  If you're concerned
about the term we dropped in the original equation, don't worry;
it's easy to show that for any fixed x with absolute value less
than 1, the fraction (x^n)/n tends to 0 as n gets large -- in other
words, the more terms you take, the smaller the error you've committed,
and "in the limit" (i.e., if you could compute infinitely many terms)
the series would indeed exactly equal the function.  So this really
is a valid way of computing approximate values of logarithms.

Hope this helps--
--Cameron Smith
  cameron@symcom.math.uiuc.edu

P.S. If somebody knows a better way that is still (somewhat) suitable
     for manual calculation, PLEASE POST IT!