all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Fix for calculating large factorials
@ 2017-04-18  8:44 michael schuldt
  2017-04-18 18:28 ` Paul Eggert
  0 siblings, 1 reply; 2+ messages in thread
From: michael schuldt @ 2017-04-18  8:44 UTC (permalink / raw)
  To: emacs-devel


[-- Attachment #1.1: Type: text/plain, Size: 354 bytes --]

I was playing around with calc bignums and found that calculating the
factorial of
large numbers results in the error "Variable binding depth exceeds
max-specpdl-size".

I though this might be a good chance to try out the contribution process,
I've attached
a path which simply eliminates the troublesome recursiveness of
math-factorial-iter.


-Michael

[-- Attachment #1.2: Type: text/html, Size: 452 bytes --]

[-- Attachment #2: factorial.patch --]
[-- Type: text/x-patch, Size: 1080 bytes --]

From 9cfd3c86f2490e2033fee1c3647f4b01029ff047 Mon Sep 17 00:00:00 2001
From: mschuldt <mbschuldt@gmail.com>
Date: Tue, 18 Apr 2017 01:25:28 -0700
Subject: [PATCH] Non-recursive version of math-factorial-iter. Prevents error
 "Variable binding depth exceeds max-specpdl-size"

---
 lisp/calc/calc-comb.el | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/lisp/calc/calc-comb.el b/lisp/calc/calc-comb.el
index c84ff23..a033d27 100644
--- a/lisp/calc/calc-comb.el
+++ b/lisp/calc/calc-comb.el
@@ -362,11 +362,13 @@
     (math-gammap1-raw '(float -25 -2))))
 
 (defun math-factorial-iter (count n f)
-  (if (= (% n 5) 1)
-      (math-working (format "factorial(%d)" (1- n)) f))
-  (if (> count 0)
-      (math-factorial-iter (1- count) (1+ n) (math-mul n f))
-    f))
+  (while (> count 0)
+    (if (= (% n 5) 1)
+        (math-working (format "factorial(%d)" (1- n)) f))
+    (setq count (1- count)
+          f (math-mul n f)
+          n (1+ n)))
+  f)
 
 (defun calcFunc-dfact (n)   ; [I I] [F F] [Public]
   (cond ((Math-integer-negp n)
-- 
2.7.4


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2017-04-18 18:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-04-18  8:44 Fix for calculating large factorials michael schuldt
2017-04-18 18:28 ` Paul Eggert

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.