[Openmcl-devel] ccl::log-e and bignum arg

Waldek Hebisch hebisch at math.uni.wroc.pl
Wed Jun 2 09:56:49 UTC 2010


R. Matthew Emerson wrote:
> I don't understand what CCL::LOG-E is doing when it gets a bignum.
> 
> Can anyone clue me in?  Any links, references, etc. appreciated.
> 
> If anyone
> (defun log-e (x)
>   (cond 
>     ((bignump x)
>      (if (minusp x)
         ;;; reduce to positive arguments
>        (complex (log-e (- x)) pi)

>        (let* ((base1 3)
>               (guess (floor (1- (integer-length x))
>                             (log base1 2)))
>               (guess1 (* guess (log-e base1))))
>          (+ guess1 (log-e (/ x (expt base1 guess)))))))
>     ...))
> 

The code uses formula log(a*b) = log(a) + log(b).  The line
with 'complex' takes as 'a' the sign of 'x' so the in the rest
there are only nonnegative numbers.  The second part takes
a = 3^guess where guess is choosen so that b is of moderate
magnitude.  log(3^guess) = guess*log(3) which above is called
guess1.

The purpose of second step presumably is to avoid overflow or
huge loss of precision converting bignum to floeating point number.
Since b is of moderate magnitude it can be converted to floating
point number without trouble.

Not so nice that the code uses single precision (Common Lisp default)
during intermediate calculations -- AFAICS it gives result with
lower precision that it could have.

-- 
                              Waldek Hebisch
hebisch at math.uni.wroc.pl 



More information about the Openmcl-devel mailing list