[Openmcl-devel] Numerical precision lacking for log (base 10)

R. Matthew Emerson rme at clozure.com
Tue Jan 26 19:45:42 PST 2010


On Jan 26, 2010, at 9:44 PM, Sudhir Shenoy wrote:

> In the course of solving a Project Euler problem (#104), I was trying  
> to compute the first 9 digits of large Fibonacci numbers and could not  
> find an answer (the same code worked in SBCL). The reason seems to be  
> that CCL does not have an accurate log function.

Yes, LOG could certainly be smarter.

You will get better results with (log (sqrt 5.0d0) 10.0d0).

CCL is converting the integer 10 to a single-float and computing the natural log of that,
so that explains why you're seeing only about 6 accurate decimal digits (which is about what a single-float gives you).

When all the arguments to LOG are rational, the spec says that the result will be a single-float (or optionally an exact rational result if that is possible).

http://www.lispworks.com/documentation/HyperSpec/Body/12_acc.htm

By implication, when the arguments are *not* all rational, it's not necessary to convert rational arguments to single-floats...

There's a ticket about a similar issue with log:
http://trac.clozure.com/ccl/ticket/340

If nobody else fixes this first, I'll try to do it tomorrow.

> Specifically, in CCL (on both ppc and intel), "(log (sqrt 5.0d0) 10)"  
> returns 0.34948499731479665D0 whereas SBCL returns  
> 0.34948500216800943d0 (this agrees with my desk calculator). The  
> difference in the two seems to be that in the implementation of log,  
> CCL is using natural logarithms and SBCL is using logarithms to base  
> 2. Is this difference the reason for the discrepancies (precision loss  
> after the 6th decimal) or is the CCL numerical algorithm not the best?  
> Single precision seems to work much better in these computations in  
> CCL (mostly agrees with SBCL except for some numbers such as 2/3).
> 
> Cheers
> Sudhir
> 
> BTW, I tried other numbers and find the same precision loss, e.g.
> 
> No.		CCL						SBCL
> PI		0.4971498657903351D0		0.49714987269413385d0
> sqrt(10)	0.4999999930566223D0		0.5d0
> E		0.4342944758723105D0		0.43429448190325176d0
> 2/3		-0.17609124					-0.17609125
> 



More information about the Openmcl-devel mailing list