From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#16999: calc crashes when computation limit is increased Date: Fri, 11 Sep 2020 12:29:54 +0200 Message-ID: <7CB65B96-318A-4641-A4F4-FBD4CB43EC22@acm.org> References: <64145024-4251-4F81-9345-4E2E064FBC1C@acm.org> Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.15\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_19A3E6C3-C92C-4261-8903-E278D04420C4" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="30184"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 16999@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Sep 11 12:30:15 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kGgJr-0007lY-2h for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 11 Sep 2020 12:30:15 +0200 Original-Received: from localhost ([::1]:43472 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1kGgJq-0006MW-4M for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 11 Sep 2020 06:30:14 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:59608) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1kGgJh-0006Kc-Vb for bug-gnu-emacs@gnu.org; Fri, 11 Sep 2020 06:30:05 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:59169) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1kGgJh-00006t-LI for bug-gnu-emacs@gnu.org; Fri, 11 Sep 2020 06:30:05 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1kGgJh-00058H-HG for bug-gnu-emacs@gnu.org; Fri, 11 Sep 2020 06:30:05 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 11 Sep 2020 10:30:05 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 16999 X-GNU-PR-Package: emacs Original-Received: via spool by 16999-submit@debbugs.gnu.org id=B16999.159982020219685 (code B ref 16999); Fri, 11 Sep 2020 10:30:05 +0000 Original-Received: (at 16999) by debbugs.gnu.org; 11 Sep 2020 10:30:02 +0000 Original-Received: from localhost ([127.0.0.1]:42477 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kGgJd-000571-SG for submit@debbugs.gnu.org; Fri, 11 Sep 2020 06:30:02 -0400 Original-Received: from mail83c50.megamailservers.eu ([91.136.10.93]:47522 helo=mail18c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1kGgJb-00056l-DA; Fri, 11 Sep 2020 06:30:00 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1599820197; bh=e81Ocsswo3sVnSVIL/vI1fogf2nBEIoro0U9986tjf4=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=cXyGsJ8vA7r316LTqbuqS+OOsW6kN48+5xrnV+2xhcaLpMVS9BLnYmcLVj1U1pfPf 6VBFgOLBbAMqLxOe/gzOftVQzkqQf6/XiqezO51nxD/EJlDprgfK1oCgfMWiH6As26 fKkkqhkLE4eyPY2V8PvnxEd4nA/LmpXLkJxZvkVw= Feedback-ID: mattiase@acm.or Original-Received: from [192.168.0.4] (c188-150-171-71.bredband.comhem.se [188.150.171.71]) (authenticated bits=0) by mail18c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 08BATtcu029452; Fri, 11 Sep 2020 10:29:56 +0000 In-Reply-To: <64145024-4251-4F81-9345-4E2E064FBC1C@acm.org> X-Mailer: Apple Mail (2.3445.104.15) X-CTCH-RefID: str=0001.0A782F29.5F5B51A5.004E, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=K8Zc4BeI c=1 sm=1 tr=0 a=SF+I6pRkHZhrawxbOkkvaA==:117 a=SF+I6pRkHZhrawxbOkkvaA==:17 a=M51BFTxLslgA:10 a=N54-gffFAAAA:8 a=EWPDJS0nAAAA:8 a=D_ldmLNfM3nHP4qRlzcA:9 a=QEXdDO2ut3YA:10 a=gczXvjW0MzpGF8ZVD5kA:9 a=B2y7HmGcmWMA:10 a=6l0D2HzqY3Epnrm8mE3f:22 X-Origin-Country: SE X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:187806 Archived-At: --Apple-Mail=_19A3E6C3-C92C-4261-8903-E278D04420C4 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 10 sep. 2020 kl. 18.02 skrev Mattias Engdeg=C3=A5rd : > Let me attempt a patch, and please forgive my reopening the bug. Here is that patch. Sadly mail to Florian Beck's address bounced but = perhaps someone else will see the proposal and subject it to scrutiny. = In short: 1. A sign mistake in the original code caused the infinite recursion. = Fixed. 2. Some parts of the computation used tail recursion, which limited = their applicability in elisp. Changed to use an imperative loop (alas). 3. The binomial coefficients have now been extended for all integral = arguments, using definitions from [1]; see also [2] and [3]. [1] M. J. Kronenburg; The Binomial Coefficient for Negative Arguments; = https://arxiv.org/abs/1105.3689 [2] D. Loeb, E. Damiani and O. D=E2=80=99Antona; Getting Results with = Negative Thinking; https://arxiv.org/abs/math/9502214 [3] David Fowler; The Binomial Coefficient Function; The American = Mathematical Monthly, Vol. 103, No. 1 (Jan., 1996), pp. 1-17 --Apple-Mail=_19A3E6C3-C92C-4261-8903-E278D04420C4 Content-Disposition: attachment; filename=0001-Calc-fix-binomial-coefficients-for-negative-argument.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0001-Calc-fix-binomial-coefficients-for-negative-argument.patch" Content-Transfer-Encoding: quoted-printable =46rom=20c451146726adfacdec69247c3da6dc4ebacd1b97=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20=3D?UTF-8?q?Mattias=3D20Engdeg=3DC3=3DA5rd?=3D=20= =0ADate:=20Fri,=2011=20Sep=202020=2011:43:15=20+0200=0A= Subject:=20[PATCH]=20Calc:=20fix=20binomial=20coefficients=20for=20= negative=20arguments=0A=20(bug#16999)=0AMIME-Version:=201.0=0A= Content-Type:=20text/plain;=20charset=3DUTF-8=0A= Content-Transfer-Encoding:=208bit=0A=0AFor=20some=20values=20outside=20= integers=200=E2=89=A4k=E2=89=A4n,=20(n=20choose=20k)=20gave=20wrong=0A= results,=20entered=20infinite=20recursion=20or=20used=20unreasonably=20= amounts=20of=0Astack=20space.=20=20This=20change=20fixes=20that=20and=20= extends=20the=20function=20to=20all=0Ainteger=20arguments=20using=20the=20= definitions=20from=20M.=20J.=20Kronenburg=0A= (https://arxiv.org/abs/1105.3689).=0A=0A*=20lisp/calc/calc-comb.el=20= (calcFunc-choose):=0AFix=20sign=20error=20to=20prevent=20infinite=20= recursion=20and=20extend=20function=20to=0Ahandle=20all=20integer=20= arguments.=0A(math-choose-iter,=20math-choose-float-iter):=20Rewrite=20= in=20iterative=20form;=0Ano=20TCO=20in=20elisp=20yet.=0A*=20= test/lisp/calc/calc-tests.el=20(calc-tests--fac,=20calc-tests--choose)=0A= (calc-tests--check-choose,=20calc-tests--explain-choose)=0A= (calc-tests--calc-to-number):=20New=20helper=20functions.=0A= (calc-choose):=20New=20test.=0A---=0A=20lisp/calc/calc-comb.el=20=20=20=20= =20=20=20|=2042=20+++++++++++++++-------=0A=20= test/lisp/calc/calc-tests.el=20|=2067=20= ++++++++++++++++++++++++++++++++++++=0A=202=20files=20changed,=2096=20= insertions(+),=2013=20deletions(-)=0A=0Adiff=20--git=20= a/lisp/calc/calc-comb.el=20b/lisp/calc/calc-comb.el=0Aindex=20= c5d4d0837e..ce676d17a9=20100644=0A---=20a/lisp/calc/calc-comb.el=0A+++=20= b/lisp/calc/calc-comb.el=0A@@=20-445,12=20+445,25=20@@=20calcFunc-choose=0A= =20=09=20=20=20(math-div=20(calcFunc-fact=20(math-float=20n))=0A=20=09=09= =20=20=20=20=20(math-mul=20(calcFunc-fact=20m)=0A=20=09=09=09=20=20=20=20= =20=20=20(calcFunc-fact=20(math-sub=20n=20m))))))=0A-=09((math-negp=20m)=20= 0)=0A-=09((math-negp=20n)=0A-=09=20(let=20((val=20(calcFunc-choose=20= (math-add=20(math-add=20n=20m)=20-1)=20m)))=0A+=20=20=20=20=20=20=20=20= ;;=20For=20the=20extension=20to=20negative=20integer=20arguments=20we=20= follow=0A+=20=20=20=20=20=20=20=20;;=20M.=20J.=20Kronenburg,=20The=20= Binomial=20Coefficient=20for=20Negative=20Arguments,=0A+=20=20=20=20=20=20= =20=20;;=20arXiv:1105.3689v2=0A+=20=20=20=20=20=20=20=20((and=20= (math-negp=20n)=20(not=20(math-negp=20m)))=0A+=20=20=20=20=20=20=20=20=20= ;;=20n<0=E2=89=A4m:=20(n=20choose=20m)=20=3D=20(-1)^m=20(-n+m-1=20choose=20= m)=0A+=09=20(let=20((val=20(calcFunc-choose=20(math-add=20(math-sub=20m=20= n)=20-1)=20m)))=0A=20=09=20=20=20(if=20(math-evenp=20(math-trunc=20m))=0A= =20=09=20=20=20=20=20=20=20val=0A=20=09=20=20=20=20=20(math-neg=20= val))))=0A+=20=20=20=20=20=20=20=20((and=20(math-negp=20n)=20= (math-num-integerp=20n))=0A+=20=20=20=20=20=20=20=20=20(if=20(math-lessp=20= n=20m)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=200=0A+=20=20=20=20=20=20=20= =20=20=20=20;;=20m=E2=89=A4n<0:=20(n=20choose=20m)=20=3D=20(-1)^(n-m)=20= (-m-1=20choose=20n-m)=0A+=20=20=20=20=20=20=20=20=20=20=20(let=20((val=20= (calcFunc-choose=20(math-sub=20(math-neg=20m)=201)=0A+=20=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20(math-sub=20n=20m))))=0A+=20=20=20=20=20=20=20=20=20= =20=20=20=20(if=20(math-evenp=20(math-sub=20n=20m))=0A+=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20val=0A+=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20(math-neg=20val)))))=0A+=09((math-negp=20m)=200)=0A=20=09= ((and=20(math-num-integerp=20n)=0A=20=09=20=20=20=20=20=20(Math-lessp=20= n=20m))=0A=20=09=200)=0A@@=20-467,20=20+480,23=20@@=20calcFunc-choose=0A=20= =09=20=20=20=20=20=20=20(math-choose-float-iter=20tm=20n=201=201)))))))=0A= =20=0A=20(defun=20math-choose-iter=20(m=20n=20i=20c)=0A-=20=20(if=20(and=20= (=3D=20(%=20i=205)=201)=20(>=20i=205))=0A+=20=20(while=20(<=3D=20i=20m)=0A= +=20=20=20=20(when=20(and=20(=3D=20(%=20i=205)=201)=20(>=20i=205))=0A=20=20= =20=20=20=20=20(math-working=20(format=20"choose(%d)"=20(1-=20i))=20c))=0A= -=20=20(if=20(<=3D=20i=20m)=0A-=20=20=20=20=20=20(math-choose-iter=20m=20= (1-=20n)=20(1+=20i)=0A-=09=09=09(math-quotient=20(math-mul=20c=20n)=20= i))=0A-=20=20=20=20c))=0A+=20=20=20=20(setq=20c=20(math-quotient=20= (math-mul=20c=20n)=20i))=0A+=20=20=20=20(setq=20n=20(1-=20n))=0A+=20=20=20= =20(setq=20i=20(1+=20i)))=0A+=20=20c)=0A=20=0A=20(defun=20= math-choose-float-iter=20(count=20n=20i=20c)=0A-=20=20(if=20(=3D=20(%=20= i=205)=201)=0A+=20=20(while=20(>=20count=200)=0A+=20=20=20=20(when=20(=3D=20= (%=20i=205)=201)=0A=20=20=20=20=20=20=20(math-working=20(format=20= "choose(%d)"=20(1-=20i))=20c))=0A-=20=20(if=20(>=20count=200)=0A-=20=20=20= =20=20=20(math-choose-float-iter=20(1-=20count)=20(math-sub=20n=201)=20= (1+=20i)=0A-=09=09=09=20=20=20=20=20=20(math-div=20(math-mul=20c=20n)=20= i))=0A-=20=20=20=20c))=0A+=20=20=20=20(setq=20c=20(math-div=20(math-mul=20= c=20n)=20i))=0A+=20=20=20=20(setq=20n=20(math-sub=20n=201))=0A+=20=20=20=20= (setq=20i=20(1+=20i))=0A+=20=20=20=20(setq=20count=20(1-=20count)))=0A+=20= =20c)=0A=20=0A=20=0A=20;;;=20Stirling=20numbers.=0Adiff=20--git=20= a/test/lisp/calc/calc-tests.el=20b/test/lisp/calc/calc-tests.el=0Aindex=20= c8cb97a8bc..5030a55472=20100644=0A---=20a/test/lisp/calc/calc-tests.el=0A= +++=20b/test/lisp/calc/calc-tests.el=0A@@=20-397,6=20+397,73=20@@=20= calc-sum-gcd=0A=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20(var=20n=20var-n)=20-1=201))=0A=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=208)))=0A=20=0A+(defun=20calc-tests--fac=20(n)=0A+=20=20= (apply=20#'*=20(number-sequence=201=20n)))=0A+=0A+(defun=20= calc-tests--choose=20(n=20k)=0A+=20=20"N=20choose=20K,=20reference=20= implementation."=0A+=20=20(cond=0A+=20=20=20((and=20(integerp=20n)=20= (integerp=20k))=0A+=20=20=20=20(if=20(<=3D=200=20n)=0A+=20=20=20=20=20=20= =20=20(if=20(<=3D=200=20k=20n)=0A+=20=20=20=20=20=20=20=20=20=20=20=20(/=20= (calc-tests--fac=20n)=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(*=20= (calc-tests--fac=20k)=20(calc-tests--fac=20(-=20n=20k))))=0A+=20=20=20=20= =20=20=20=20=20=200)=20=20=20=20;=200=E2=89=A4n=20%S,=20expected=20%S"=20n=20k=20got=20= expected)))=0A+=0A+(put=20'calc-tests--check-choose=20'ert-explainer=20= 'calc-tests--explain-choose)=0A+=0A+(defun=20calc-tests--calc-to-number=20= (x)=0A+=20=20"Convert=20a=20Calc=20object=20to=20a=20Lisp=20number."=0A+=20= =20(pcase=20x=0A+=20=20=20=20((pred=20numberp)=20x)=0A+=20=20=20=20= (`(frac=20,p=20,q)=20(/=20(float=20p)=20q))=0A+=20=20=20=20(`(float=20,m=20= ,e)=20(*=20m=20(expt=2010=20e)))=0A+=20=20=20=20(_=20(error=20"calc=20= object=20not=20converted:=20%S"=20x))))=0A+=0A+(ert-deftest=20= calc-choose=20()=0A+=20=20"Test=20computation=20of=20binomial=20= coefficients=20(bug#16999)."=0A+=20=20;;=20Integral=20arguments=0A+=20=20= (dolist=20(n=20(number-sequence=20-6=206))=0A+=20=20=20=20(dolist=20(k=20= (number-sequence=20-6=206))=0A+=20=20=20=20=20=20(should=20= (calc-tests--check-choose=20n=20k))))=0A+=0A+=20=20;;=20Fractional=20n,=20= natural=20k=0A+=20=20(should=20(equal=20(calc-tests--calc-to-number=0A+=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(calcFunc-choose=20= '(frac=2015=202)=203))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20(calc-tests--choose=207.5=203)))=0A+=0A+=20=20(should=20(equal=20= (calc-tests--calc-to-number=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20(calcFunc-choose=20'(frac=201=202)=202))=0A+=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20(calc-tests--choose=200.5=202)))=0A+=0A= +=20=20(should=20(equal=20(calc-tests--calc-to-number=0A+=20=20=20=20=20=20= =20=20=20=20=20=20=20=20=20=20=20=20(calcFunc-choose=20'(frac=20-15=202)=20= 3))=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (calc-tests--choose=20-7.5=203))))=0A+=0A=20(provide=20'calc-tests)=0A=20= ;;;=20calc-tests.el=20ends=20here=0A=20=0A--=20=0A2.21.1=20(Apple=20= Git-122.3)=0A=0A= --Apple-Mail=_19A3E6C3-C92C-4261-8903-E278D04420C4--