Nyquist / XLISP 2.0  -  Contents | Tutorials | Examples | Reference

cl:round


The cl:round function truncates an integer or floating-point number toward the next integer:

(cl:round number [divisor])
number - an integer or floating-point number
divisor - an integer or floating-point number, except zero
returns  -  the result of runding the result of number divided by divisor
 -  the remainder of the round operation

(defun cl:round (number &optional (divisor
                                  (if (integerp number) 1 1.0)
                                  divisor-p))
  (let* ((x (/ (float number) divisor))
         (quotient (cond ((and (not divisor-p) (integerp number)) number)
                         ((= number divisor) 1)
                         ((plusp x) (truncate (+ x 0.5)))
                         ((= (- x 0.5) (truncate (- x 0.5)))
                          (if (minusp x)
                              (1- (truncate x))
                              (truncate x)))
                         (t (truncate (- x 0.5))))))
    (setq *rslt* (list quotient (- number (* quotient divisor)))
          cl:*multiple-values* t)
    quotient))

The cl:round function computes a quotient that has been rounded to the nearest mathematical integer. If the mathematical quotient is exactly halfway between two integers, [that is, it has the form 'integer+1/2'], then the quotient has been rounded to the even [divisible by two] integer.

The quotient is directly returned by the function, while a list:

(quotient remainder)

is stored in the Nyquist/XLISP *rslt* variable and the cl:*multiple-values* is set to  T  to signal that Multiple Values are returned.

See Rounding and Truncation for more details.

Examples:

(round  3.5)     =>  4
(round -3.5)     => -3

(cl:round  3.5)  =>  4  ; *rslt* = ( 4 -0.5)
(cl:round -3.5)  => -4  ; *rslt* = (-4  0.5)

  Back to top


Nyquist / XLISP 2.0  -  Contents | Tutorials | Examples | Reference