Debian Codegraph @

docsrc/xlisp/xlisp-doc/examples/common-lisp/round.htm

raw
1 <html><head>
2
3 <title>cl:round</title>
4
5 <style type="text/css">
6 .example {
7 color: #000000;
8 background-color: #F5F5F5;
9 padding: 8px;
10 border: #808080;
11 border-style: solid;
12 border-width: 1px;
13 width:auto;
14 }
15 .button {
16 color: #000000;
17 background-color: #F5F5F5;
18 padding-top: 1px;
19 padding-bottom: 1px;
20 padding-left: 4px;
21 padding-right: 8px;
22 border: #808080;
23 border-style: solid;
24 border-width: 1px;
25 white-space: pre;
26 }
27 .box {
28 color: #000000;
29 padding-top: 4px;
30 padding-bottom: 4px;
31 padding-left: 16px;
32 padding-right: 16px;
33 border: #808080;
34 border-style: solid;
35 border-width: 1px;
36 }
37 </style>
38
39 </head>
40
41 <body>
42
43 <a href="../../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
44 <a href="../../manual/contents.htm">Contents</a> |
45 <a href="../../tutorials/tutorials.htm">Tutorials</a> |
46 <a href="../examples.htm">Examples</a> |
47 <a href="../../reference/reference-index.htm">Reference</a>
48
49 <hr>
50
51 <h1>cl:round</h1>
52
53 <hr>
54
55 <p>The <nobr>cl:<b>round</b></nobr> function
56 <a href="../../reference/truncate.htm">truncate</a>s an integer or
57 <nobr>floating-point</nobr> number toward the next integer:</p>
58
59 <p><div class="box">
60
61 <dl>
62 <dt>(cl:<b>round</b> <i>number </i> [<i>divisor</i>])</dt>
63 <dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br>
64 <i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br>
65 <table cellpadding="0" cellspacing="0"><tbody>
66 <tr>
67 <td valign="top"><nobr>returns</nobr></td>
68 <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
69 <td width="100%">the result of runding the result of <i>number</i> divided by <i>divisor</i></td>
70 </tr>
71 <tr>
72 <td></td>
73 <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
74 <td width="100%">the remainder of the round operation</td>
75 </tr>
76 </tbody></table></dd>
77 </dl>
78
79 </div></p>
80
81 <pre class="example">
82 (defun <font color="#0000CC">cl:round</font> (number &amp;optional (divisor
83 (if (integerp number) 1 1.0)
84 divisor-p))
85 (let* ((x (/ (float number) divisor))
86 (quotient (cond ((and (not divisor-p) (integerp number)) number)
87 ((= number divisor) 1)
88 ((plusp x) (truncate (+ x 0.5)))
89 ((= (- x 0.5) (truncate (- x 0.5)))
90 (if (minusp x)
91 (1- (truncate x))
92 (truncate x)))
93 (t (truncate (- x 0.5))))))
94 (setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor)))
95 <font color="#AA5500">cl:*multiple-values*</font> t)
96 quotient))
97 </pre>
98
99 <p>The <nobr>cl:<b>round</b></nobr> function computes a quotient that has
100 been rounded to the nearest mathematical integer. <nobr>If the</nobr>
101 mathematical quotient is exactly halfway between two integers, [that is, it
102 has the form <nobr>'integer+1/2']</nobr>, then the quotient has been rounded
103 to the even [divisible <nobr>by two]</nobr> integer.</p>
104
105 <p>The quotient is directly returned by the function, while a list:</p>
106
107 <pre class="example">
108 (quotient remainder)
109 </pre>
110
111 <p>is stored in the Nyquist/XLISP
112 <a href="../../reference/global-rslt.htm">*rslt*</a> variable and the
113 <a href="global-multiple-values.htm">cl:*multiple-values*</a> is set to
114 <a href="../../reference/t.htm">&nbsp;T&nbsp;</a> to signal that
115 <a href="multiple-values.htm">Multiple Values</a> are returned.</p>
116
117 <nobr>See
118 <a href="rounding-and-truncation.htm">Rounding and Truncation</a></nobr>
119 for more details.</p>
120
121 <p>Examples:</p>
122
123 <pre class="example">
124 (round 3.5) =&gt; 4
125 (round -3.5) =&gt; -3
126
127 (cl:round 3.5) =&gt; 4 <font color="#008844">; *rslt* = ( 4 -0.5)</font>
128 (cl:round -3.5) =&gt; -4 <font color="#008844">; *rslt* = (-4 0.5)</font>
129 </pre>
130
131 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
132
133 <hr>
134
135 <a href="../../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
136 <a href="../../manual/contents.htm">Contents</a> |
137 <a href="../../tutorials/tutorials.htm">Tutorials</a> |
138 <a href="../examples.htm">Examples</a> |
139 <a href="../../reference/reference-index.htm">Reference</a>
140
141 </body></html>