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

cl:truncate


The cl:truncate function truncates an integer or floating-point number toward zero:

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

(defun cl:truncate (number &optional (divisor (if (integerp number) 1 1.0)))
  (let ((quotient (truncate (/ (float number) divisor))))
    (setq *rslt* (list quotient (- number (* quotient divisor)))
          cl:*multiple-values* t)
    quotient))

The cl:truncate function computes a quotient that has been truncated towards zero. That is, the quotient represents the mathematical integer of the same sign as the mathematical quotient, and that has the greatest integral magnitude not greater than that of the mathematical quotient.

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:

(cl:truncate  3.5)  =>  0  ; *rslt* => ( 3  0.5)
(cl:truncate -3.5)  => -3  ; *rslt* => (-3 -0.5)

Force an integer division:

(cl:truncate 3.1 2.6)  => 1  ; *rslt* => (1 0.5)
(/ 3 2)                => 1

  Back to top


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