chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] numbers egg slow?


From: Alex Shinn
Subject: Re: [Chicken-users] numbers egg slow?
Date: Fri, 07 Oct 2005 01:15:57 -0500
User-agent: Wanderlust/2.10.1 (Watching The Wheels) SEMI/1.14.6 (Maruoka) FLIM/1.14.6 (Marutamachi) APEL/10.6 Emacs/21.3 (i386-pc-linux-gnu) MULE/5.0 (SAKAKI)

At Thu, 06 Oct 2005 21:56:08 +0900, Daishi Kato wrote:
> 
> (expt) however is slow for fixnum arithmetic.
> I reviewed the "Bug in the numbers egg" thread again,
> understand the background, and am seeking the solution.

It would be nice to have a faster EXPT, but since there have already
been a number of bugs related to it we should probably start with a
very thorough test suite before trying to improve it.

> and there was a bug with the case such as (%power 2 2.1).

Oops, you're right.  But that branch is never actually reached in the
code, I shouldn't have even included it in the final %POWER
definition.  (expt 2 2.1) works fine.

> +(define (%fix-power base e)
> +  (define (square x) (%* x x))
> +  (if (negative? e)
> +    (/ 1 (%power base (- e)))
> +    (let lp ((res 1) (e2 e))
> +      (cond
> +        ((zero? e2) res)
> +        ((%fix-expt base e2))
> +        ((even? e2) ; recursion is faster than iteration here
> +         (%* res (square (lp 1 (arithmetic-shift e2 -1)))))
> +        (else
> +         (lp (%* res base) (- e2 1)))))))

It's probably better to check the result of %FIX-EXPT once before
entering the loop rather than on every iteration, since if it fails
once it will always fail.

-- 
Alex




reply via email to

[Prev in Thread] Current Thread [Next in Thread]