XLISP round

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

# round

Type: | - | Lisp function (closure)
Source: | - | fileio.lsp

## Syntax

(round number)
  number - an integer or floating point numbers
  returns - the number, rounded to the next integer value

In Nyquist, 'round' is implemented as a Lisp function:

    (defun round (x)
      (cond ((> x 0) (truncate (+ x 0.5)))
            ((= (- x 0.5) (truncate (- x 0.5))) (truncate x))
            (t (truncate (- x 0.5)))))

## Description

The 'round' function rounds a number to the next integer value. This is tricky because truncate rounds toward zero as does C in other words, rounding is down for positive numbers and up for negative numbers. You can convert rounding up to rounding down by subtracting one, but this fails on the integers, so we need a special test if (- x 0.5) is an integer.

## Examples

    (round .5)   => 1
    (round -.5)  => 0

Back to Top

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