Newsgroups: sci.math Subject: Re: Logartihm base 2 Date: 25 Apr 1997 14:48:01 +0200 Organization: Institut fuer Neuroinformatik, Ruhr-Universitaet Bochum, Germany Lines: 30 To: aschrum@imnet.com X-Newsreader: Gnus v5.3/Emacs 19.33 "Allan G. Schrum" writes: > Does anyone have a method for computing a logarithm to an aribitrary > base? More specifically, how about a logarithm to base 2. Yes, I know if > you take a log (of any base) and divide by log(2) you will convert it to > a log base 2, but I am looking for a direct-calculation method for the > log(x) base 2. The numbers that I am dealing with are very big and it is > not computationally feasible to divide by log(2) to compute the > logarithm to the base 2. > > A series or other such method is certainly what I am looking for. How about the binary method? First normalize your number, shifting it left until it fits into a single precision number. The number of shifts gives you the integral part of your logarithm if you subtract it from some constant. Then the next bit: square your number into a double precision number. Put its most significant bit into the next lower bit of the result. Normalize again (which means shifting left by one if the last step gave a zero bit). Throw away the low word of the result, rounding its highest bit into the high word. Take that and start this paragraph over. Gives pretty good precision, just a few LSB of error. -- David Kastrup Phone: +49-234-700-5570 Email: dak@neuroinformatik.ruhr-uni-bochum.de Fax: +49-234-709-4209 Institut für Neuroinformatik, Universitätsstr. 150, 44780 Bochum, Germany