computing expontential as repeated multiplication for small integer exponent
Subject: computing expontential as repeated multiplication for small integer exponent
From: Robert Dodier
Date: Sun, 29 Jul 2007 20:03:18 -0600
Hello,
At present there is test suite case which fails (#69 in rtest8)
for some Lisp implementations, apparently due to different
results returned by the Lisp built-in function EXPT.
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?
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