aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLuc Teirlinck2003-11-26 19:23:58 +0000
committerLuc Teirlinck2003-11-26 19:23:58 +0000
commit2c1385edb656815975976a72081e4b134ed716e9 (patch)
treefd07119ee77464ef46e75076724cf1b765dce791
parent44d23d8d3467c1985de441afe3cbd8abd4874ba0 (diff)
downloademacs-2c1385edb656815975976a72081e4b134ed716e9.tar.gz
emacs-2c1385edb656815975976a72081e4b134ed716e9.zip
(number-sequence): Improve handling of floating point arguments
(suggested by Kim Storm). Allow negative arguments.
-rw-r--r--lisp/subr.el45
1 files changed, 33 insertions, 12 deletions
diff --git a/lisp/subr.el b/lisp/subr.el
index f5e5891b96a..9dc6c2ba6ec 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -191,20 +191,41 @@ If N is bigger than the length of X, return X."
191 191
192(defun number-sequence (from &optional to inc) 192(defun number-sequence (from &optional to inc)
193 "Return a sequence of numbers from FROM to TO (both inclusive) as a list. 193 "Return a sequence of numbers from FROM to TO (both inclusive) as a list.
194INC is the increment used between numbers in the sequence. 194INC is the increment used between numbers in the sequence and defaults to 1.
195So, the Nth element of the list is (+ FROM (* N INC)) where N counts from 195So, the Nth element of the list is \(+ FROM \(* N INC)) where N counts from
196zero. 196zero. TO is only included if there is an N for which TO = FROM + N * INC.
197If INC is nil, it defaults to 1 (one). 197If TO is nil or numerically equal to FROM, return \(FROM).
198If TO is nil, it defaults to FROM. 198If INC is positive and TO is less than FROM, or INC is negative
199If TO is less than FROM, the value is nil. 199and TO is larger than FROM, return nil.
200Note that FROM, TO and INC can be integer or float." 200If INC is zero and TO is neither nil nor numerically equal to
201 (if (not to) 201FROM, signal an error.
202
203This function is primarily designed for integer arguments.
204Nevertheless, FROM, TO and INC can be integer or float. However,
205floating point arithmetic is inexact. For instance, depending on
206the machine, it may quite well happen that
207\(number-sequence 0.4 0.6 0.2) returns the one element list \(0.4),
208whereas \(number-sequence 0.4 0.8 0.2) returns a list with three
209elements. Thus, if some of the arguments are floats and one wants
210to make sure that TO is included, one may have to explicitly write
211TO as \(+ FROM \(* N INC)) or use a variable whose value was
212computed with this exact expression. Alternatively, you can,
213of course, also replace TO with a slightly larger value
214\(or a slightly more negative value if INC is negative)."
215 (if (or (not to) (= from to))
202 (list from) 216 (list from)
203 (or inc (setq inc 1)) 217 (or inc (setq inc 1))
204 (let (seq) 218 (when (zerop inc) (error "The increment can not be zero"))
205 (while (<= from to) 219 (let (seq (n 0) (next from))
206 (setq seq (cons from seq) 220 (if (> inc 0)
207 from (+ from inc))) 221 (while (<= next to)
222 (setq seq (cons next seq)
223 n (1+ n)
224 next (+ from (* n inc))))
225 (while (>= next to)
226 (setq seq (cons next seq)
227 n (1+ n)
228 next (+ from (* n inc)))))
208 (nreverse seq)))) 229 (nreverse seq))))
209 230
210(defun remove (elt seq) 231(defun remove (elt seq)