# [Maxima] computing expontential as repeated multiplication for small integer exponent

Bill Wood william.wood3 at comcast.net
Sun Jul 29 22:22:55 CDT 2007

```On Sun, 2007-07-29 at 20:03 -0600, Robert Dodier wrote:
. . .
> I find that the attached patch for Maxima's EXPTB makes the
> result of rtest8 #69 come out the same for the Lisp
> implementations I have tested (SBCL, GCL, Clisp).
> Before, GCL and Clisp agreed and SBCL was different.
>
> This is somewhat cheesy so I thought I would gather 2nd
> opinions before committing. Comments? Maybe there is
> a better way to go about resolving this problem?

Do you have test data that demonstrates the variance in behavior?  I'll
run tests on CMUCL if you like.
>
> best
> Robert Dodier
>
> PS.
> Index: src/simp.lisp
> ===================================================================
> RCS file: /cvsroot/maxima/maxima/src/simp.lisp,v
> retrieving revision 1.41
> diff -u -r1.41 simp.lisp
> --- src/simp.lisp   4 Jul 2007 02:37:00 -0000   1.41
> +++ src/simp.lisp   30 Jul 2007 01:52:02 -0000
> @@ -652,14 +652,25 @@
>
>  ;; I (rtoy) think EXPTB is meant to compute a^b, where b is an
>  ;; integer.
> +
> +(defvar *exptb-max-integer-exponent* 16)
> +
>  (defun exptb (a b)
>    (cond ((equal a %e-val)
>      ;; Make B a float so we'll get double-precision result.
>      (exp (float b)))
> +
> +    ;; Compute a^b as a repeated product when a is a float and b is a
> small positive integer.
> +    ;; This makes results for different Lisp implementations agree
> (otherwise EXPT differs).
> +    ((and (floatp a) (integerp b) (not (minusp b)) (< b
> *exptb-max-integer-exponent*))
> +     (let ((product 1))
> +       (dotimes (i b) (setq product (* product a)))
> +       product))
> +
>     ((or (floatp a) (not (minusp b)))
>      (expt a b))
>     (t
> -    (setq b (expt a (- b)))
> +    (setq b (exptb a (- b)))
>      (*red 1 b))))
>
>  (defmfun simplus (x w z)       ; W must be 1
> _______________________________________________
> Maxima mailing list
> Maxima at math.utexas.edu
> http://www.math.utexas.edu/mailman/listinfo/maxima
```