[gnu.emacs.bug] bug in floor

peck@SUN.COM (Jeff Peck) (08/03/89)

The value returned by floor has the wrong sign
for calls of the form:  (floor -q*d d),
 I.e.: (floor -10 5) gives [2 0] instead of [-2 0]
Fix is in the last COND clause.

(defun floor (number &optional divisor)
  "Divide DIVIDEND by DIVISOR, rounding toward minus infinity.
DIVISOR defaults to 1.  The remainder is produced as a second value."
  (cond
   ((and (null divisor)                 ; trivial case
         (numberp number))
    (values number 0))
   (t                                   ; do the division
    (multiple-value-bind
        (q r s)
        (safe-idiv number divisor)
      (cond
       ((zerop s)
        (values 0 0))
       ((plusp s)
        (values q r))
       (t (if (zerop r)
	      (values (- 0 q) 0)
	      (let ((q (- 0 (+ q 1))))
	        (values q (- number (* q divisor))))
 	   )))))))