unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* 22.1.50; calc-math.el patch: fix non-termination
@ 2007-09-26 17:54 Markus Triska
  2007-09-27  0:06 ` Jay Belanger
  0 siblings, 1 reply; 4+ messages in thread
From: Markus Triska @ 2007-09-26 17:54 UTC (permalink / raw)
  To: emacs-pretest-bug


With the OS I'm currently working on, the result of (expt 10.0 x) is
1.0e+INF for x >= 309, i.e., no range error is triggered. This patch
to calc-math.el prevents non-termination when loading the file.

2007-09-26  Markus Triska  <markus.triska@gmx.at>

	* calc/calc-math.el (math-largest-emacs-expt)
	(math-smallest-emacs-expt): more robust computation

diff --git a/lisp/calc/calc-math.el b/lisp/calc/calc-math.el
index 3e4743d..44ccbcc 100644
--- a/lisp/calc/calc-math.el
+++ b/lisp/calc/calc-math.el
@@ -54,28 +54,32 @@
 
 (defvar math-largest-emacs-expt
   (let ((x 1))
-    (while (condition-case nil
-               (expt 10.0 x)
-             (error nil))
+    (while (and (condition-case nil
+		    (expt 10.0 x)
+		  (error nil))
+		(< (expt 10.0 x) (expt 10.0 (* 2 x))))
       (setq x (* 2 x)))
     (setq x (/ x 2))
-    (while (condition-case nil
-               (expt 10.0 x)
-             (error nil))
+    (while (and (condition-case nil
+		    (expt 10.0 x)
+		  (error nil))
+		(< (expt 10.0 x) (expt 10.0 (* 2 x))))
       (setq x (1+ x)))
     (- x 2))
   "The largest exponent which Calc will convert to an Emacs float.")
 
 (defvar math-smallest-emacs-expt
   (let ((x -1))
-    (while (condition-case nil
-               (expt 10.0 x)
-             (error nil))
+    (while (and (condition-case nil
+		    (expt 10.0 x)
+		  (error nil))
+		(> (expt 10.0 x) 0.0))
       (setq x (* 2 x)))
     (setq x (/ x 2))
-    (while (condition-case nil
-               (expt 10.0 x)
-             (error nil))
+    (while (and (condition-case nil
+		    (expt 10.0 x)
+		  (error nil))
+		(> (expt 10.0 x) 0.0))
       (setq x (1- x)))
     (+ x 2))
     "The smallest exponent which Calc will convert to an Emacs float.")

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

* Re: 22.1.50; calc-math.el patch: fix non-termination
  2007-09-26 17:54 22.1.50; calc-math.el patch: fix non-termination Markus Triska
@ 2007-09-27  0:06 ` Jay Belanger
  2007-09-27 18:00   ` Richard Stallman
  2007-09-27 18:42   ` Markus Triska
  0 siblings, 2 replies; 4+ messages in thread
From: Jay Belanger @ 2007-09-27  0:06 UTC (permalink / raw)
  To: Markus Triska; +Cc: emacs-pretest-bug, jay.p.belanger


Markus Triska <markus.triska@gmx.at> writes:

> With the OS I'm currently working on, the result of (expt 10.0 x) is
> 1.0e+INF for x >= 309, i.e., no range error is triggered.

I don't know why I thought `expt' would necessarily give a range error
when it's too large; the documentation is silent on this matter.
I take it there are three possibilities for large x:  expt will give
an error, it will give infinity, or it will give 0.

> This patch to calc-math.el prevents non-termination when loading the
> file.

There was talk about adjusting `expt' to give a range error for large
x; if that doesn't happen then something like Markus's patch (I'll
tweak it to take care of the 0 case, for example) will be needed.

Using the 15 line convention, this patch is borderline tiny, but
Markus's name appears several times in the lisp Changelogs.  Do we
have copyright papers for him/you?

Jay

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

* Re: 22.1.50; calc-math.el patch: fix non-termination
  2007-09-27  0:06 ` Jay Belanger
@ 2007-09-27 18:00   ` Richard Stallman
  2007-09-27 18:42   ` Markus Triska
  1 sibling, 0 replies; 4+ messages in thread
From: Richard Stallman @ 2007-09-27 18:00 UTC (permalink / raw)
  To: jay.p.belanger; +Cc: emacs-pretest-bug, markus.triska, jay.p.belanger

We have papers from Markus Triska.  If nobody points out any problem
in this patch in 3 days, please install it.

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

* Re: 22.1.50; calc-math.el patch: fix non-termination
  2007-09-27  0:06 ` Jay Belanger
  2007-09-27 18:00   ` Richard Stallman
@ 2007-09-27 18:42   ` Markus Triska
  1 sibling, 0 replies; 4+ messages in thread
From: Markus Triska @ 2007-09-27 18:42 UTC (permalink / raw)
  To: jay.p.belanger; +Cc: emacs-pretest-bug

Jay Belanger <jay.p.belanger@gmail.com> writes:

> There was talk about adjusting `expt' to give a range error for large
> x; if that doesn't happen then something like Markus's patch (I'll
> tweak it to take care of the 0 case, for example) will be needed.

The 0 case cannot arise for math-largest-emacs-expt, and it is handled
for math-smallest-emacs-expt. I have now tested on another OS and
suggest the following improved patch, which should work in all cases:

2007-09-27  Markus Triska  <markus.triska@gmx.at>

	* calc/calc-math.el (math-largest-emacs-expt)
	(math-smallest-emacs-expt): more robust computation

diff --git a/lisp/calc/calc-math.el b/lisp/calc/calc-math.el
index 3e4743d..1cfc582 100644
--- a/lisp/calc/calc-math.el
+++ b/lisp/calc/calc-math.el
@@ -53,14 +53,17 @@
 ;;; is an Emacs float, for acceptable d.dddd....
 
 (defvar math-largest-emacs-expt
-  (let ((x 1))
+  (let ((x 1)
+        err)
     (while (condition-case nil
-               (expt 10.0 x)
-             (error nil))
+               (< (expt 10.0 x) (expt 10.0 (* 2 x)))
+             (error (setq err t)
+                    nil))
       (setq x (* 2 x)))
-    (setq x (/ x 2))
+    (unless err
+      (setq x (/ x 2)))
     (while (condition-case nil
-               (expt 10.0 x)
+               (< (expt 10.0 x) (expt 10.0 (1+ x)))
              (error nil))
       (setq x (1+ x)))
     (- x 2))
@@ -69,12 +72,12 @@
 (defvar math-smallest-emacs-expt
   (let ((x -1))
     (while (condition-case nil
-               (expt 10.0 x)
+               (> (expt 10.0 x) 0.0)
              (error nil))
       (setq x (* 2 x)))
     (setq x (/ x 2))
     (while (condition-case nil
-               (expt 10.0 x)
+               (> (expt 10.0 x) 0.0)
              (error nil))
       (setq x (1- x)))
     (+ x 2))

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

end of thread, other threads:[~2007-09-27 18:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-09-26 17:54 22.1.50; calc-math.el patch: fix non-termination Markus Triska
2007-09-27  0:06 ` Jay Belanger
2007-09-27 18:00   ` Richard Stallman
2007-09-27 18:42   ` Markus Triska

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).