Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: RE: log to the base 2


From   wgould@stata.com (William Gould, Stata)
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: RE: log to the base 2
Date   Wed, 18 Oct 2006 10:53:18 -0500

In a discussion about obtaining log base 2, Rodrigo Alfaro
<ralfaro76@hotmail.com> wondered how Stata calculates logs.
Rodrigo wrote,

> What do you mean with easy?
> 
> I think that the famous book of Abramovitch "Handbook of Formula...." (maybe
> the reference there... I have it in some backup file) explains that the
> algorithm for the approximation of logarithm works better for base 10 than
> other lower base (e, 2 or whatever). For that reason, log10 is computed and
> log or ln is obtained as a second result using the property of logarithms.
> Therefore the natural was added and the unnatural or common is the original.
> I don't know how Stata works here (interesting question), but if you take a
> regular scientific calculator you will see log10 and ln not log2 or log5...
> the chip was produced in 60 (maybe before) with the 'tables' for log10.

Modern computers in fact obtain logs from log base 2, using 

         log_y(x) = log_2(x)/log_2(y)

They use log base 2 as the mother function because of how floating-point 
numbers are stored.  Floating point number z is stored as (s, e), where

         z  = s * 2^e,  -2 < s < 2

Understand, you may write 200, but your computer stores that as 
1.5625*2^7, and it is 1.5625 and 7 that your computer stores.  
(Of course, what the computer actually stores is in binary, and 
the numbers are 1.1001 and 111 in base 2.)

In any case, 

         log_2(z) = log_2(s * 2^e)
                  = log_2(s) + log_2(2^e)
                  = log_2(s) + e

Thus, one need only obtain log_2(s) where 0<s<2, and that is not a difficult
problem.

-- Bill
wgould@stata.com
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index