diff options
| author | Luc Teirlinck | 2003-11-26 19:23:58 +0000 |
|---|---|---|
| committer | Luc Teirlinck | 2003-11-26 19:23:58 +0000 |
| commit | 2c1385edb656815975976a72081e4b134ed716e9 (patch) | |
| tree | fd07119ee77464ef46e75076724cf1b765dce791 | |
| parent | 44d23d8d3467c1985de441afe3cbd8abd4874ba0 (diff) | |
| download | emacs-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.el | 45 |
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. |
| 194 | INC is the increment used between numbers in the sequence. | 194 | INC is the increment used between numbers in the sequence and defaults to 1. |
| 195 | So, the Nth element of the list is (+ FROM (* N INC)) where N counts from | 195 | So, the Nth element of the list is \(+ FROM \(* N INC)) where N counts from |
| 196 | zero. | 196 | zero. TO is only included if there is an N for which TO = FROM + N * INC. |
| 197 | If INC is nil, it defaults to 1 (one). | 197 | If TO is nil or numerically equal to FROM, return \(FROM). |
| 198 | If TO is nil, it defaults to FROM. | 198 | If INC is positive and TO is less than FROM, or INC is negative |
| 199 | If TO is less than FROM, the value is nil. | 199 | and TO is larger than FROM, return nil. |
| 200 | Note that FROM, TO and INC can be integer or float." | 200 | If INC is zero and TO is neither nil nor numerically equal to |
| 201 | (if (not to) | 201 | FROM, signal an error. |
| 202 | |||
| 203 | This function is primarily designed for integer arguments. | ||
| 204 | Nevertheless, FROM, TO and INC can be integer or float. However, | ||
| 205 | floating point arithmetic is inexact. For instance, depending on | ||
| 206 | the machine, it may quite well happen that | ||
| 207 | \(number-sequence 0.4 0.6 0.2) returns the one element list \(0.4), | ||
| 208 | whereas \(number-sequence 0.4 0.8 0.2) returns a list with three | ||
| 209 | elements. Thus, if some of the arguments are floats and one wants | ||
| 210 | to make sure that TO is included, one may have to explicitly write | ||
| 211 | TO as \(+ FROM \(* N INC)) or use a variable whose value was | ||
| 212 | computed with this exact expression. Alternatively, you can, | ||
| 213 | of 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) |