Commit da5e0050 authored by Paul Eggert's avatar Paul Eggert
Browse files

cl-extra fixes for most-negative-fixnum

* lisp/emacs-lisp/cl-extra.el (cl-gcd, cl-lcm, cl-random):
Don't mishandle an argument equal to most-negative-fixnum,
whose absolute value equals itself.
(cl-gcd, cl-lcm): Use dolist rather than doing it by hand.
parent efc262f5
...@@ -299,22 +299,21 @@ If so, return the true (non-nil) value returned by PREDICATE. ...@@ -299,22 +299,21 @@ If so, return the true (non-nil) value returned by PREDICATE.
;;;###autoload ;;;###autoload
(defun cl-gcd (&rest args) (defun cl-gcd (&rest args)
"Return the greatest common divisor of the arguments." "Return the greatest common divisor of the arguments."
(let ((a (abs (or (pop args) 0)))) (let ((a (or (pop args) 0)))
(while args (dolist (b args)
(let ((b (abs (pop args)))) (while (/= b 0)
(while (> b 0) (setq b (% a (setq a b)))))) (setq b (% a (setq a b)))))
a)) (abs a)))
;;;###autoload ;;;###autoload
(defun cl-lcm (&rest args) (defun cl-lcm (&rest args)
"Return the least common multiple of the arguments." "Return the least common multiple of the arguments."
(if (memq 0 args) (if (memq 0 args)
0 0
(let ((a (abs (or (pop args) 1)))) (let ((a (or (pop args) 1)))
(while args (dolist (b args)
(let ((b (abs (pop args)))) (setq a (* (/ a (cl-gcd a b)) b)))
(setq a (* (/ a (cl-gcd a b)) b)))) (abs a))))
a)))
;;;###autoload ;;;###autoload
(defun cl-isqrt (x) (defun cl-isqrt (x)
...@@ -431,7 +430,7 @@ Optional second arg STATE is a random-state object." ...@@ -431,7 +430,7 @@ Optional second arg STATE is a random-state object."
;; Inspired by "ran3" from Numerical Recipes. Additive congruential method. ;; Inspired by "ran3" from Numerical Recipes. Additive congruential method.
(let ((vec (aref state 3))) (let ((vec (aref state 3)))
(if (integerp vec) (if (integerp vec)
(let ((i 0) (j (- 1357335 (% (abs vec) 1357333))) (k 1)) (let ((i 0) (j (- 1357335 (abs (% vec 1357333)))) (k 1))
(aset state 3 (setq vec (make-vector 55 nil))) (aset state 3 (setq vec (make-vector 55 nil)))
(aset vec 0 j) (aset vec 0 j)
(while (> (setq i (% (+ i 21) 55)) 0) (while (> (setq i (% (+ i 21) 55)) 0)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment