Debian Codegraph @

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

raw
1 <html><head>
2
3 <title>cl:truncate</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:truncate</h1>
52
53 <hr>
54
55 <p>The <nobr>cl:<b>truncate</b></nobr> function
56 <a href="../../reference/truncate.htm">truncate</a>s an integer or
57 <nobr>floating-point</nobr> number toward zero:</p>
58
59 <p><div class="box">
60
61 <dl>
62 <dt>(cl:<b>truncate</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 truncating 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 truncate 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:truncate</font> (number &amp;optional (divisor (if (integerp number) 1 1.0)))
83 (let ((quotient (truncate (/ (float number) divisor))))
84 (setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor)))
85 <font color="#AA5500">cl:*multiple-values*</font> t)
86 quotient))
87 </pre>
88
89 <p>The <nobr>cl:<b>truncate</b></nobr> function computes a quotient that has
90 been truncated towards zero. <nobr>That is</nobr>, the quotient represents
91 the mathematical integer of the same sign as the mathematical quotient, and
92 that has the greatest integral magnitude not greater than that of the
93 mathematical quotient.</p>
94
95 <p>The quotient is directly returned by the function, while a list:</p>
96
97 <pre class="example">
98 (quotient remainder)
99 </pre>
100
101 <p>is stored in the Nyquist/XLISP
102 <a href="../../reference/global-rslt.htm">*rslt*</a> variable and the
103 <a href="global-multiple-values.htm">cl:*multiple-values*</a> is set to
104 <a href="../../reference/t.htm">&nbsp;T&nbsp;</a> to signal that
105 <a href="multiple-values.htm">Multiple Values</a> are returned.</p>
106
107 <nobr>See
108 <a href="rounding-and-truncation.htm">Rounding and Truncation</a></nobr>
109 for more details.</p>
110
111 <p>Examples:</p>
112
113 <pre class="example">
114 (cl:truncate 3.5) =&gt; 0 <font color="#008844">; *rslt* =&gt; ( 3 0.5)</font>
115 (cl:truncate -3.5) =&gt; -3 <font color="#008844">; *rslt* =&gt; (-3 -0.5)</font>
116 </pre>
117
118 <p>Force an integer division:</p>
119
120 <pre class="example">
121 (cl:truncate 3.1 2.6) =&gt; 1 <font color="#008844">; *rslt* =&gt; (1 0.5)</font>
122 (/ 3 2) =&gt; 1
123 </pre>
124
125 <p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>
126
127 <hr>
128
129 <a href="../../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
130 <a href="../../manual/contents.htm">Contents</a> |
131 <a href="../../tutorials/tutorials.htm">Tutorials</a> |
132 <a href="../examples.htm">Examples</a> |
133 <a href="../../reference/reference-index.htm">Reference</a>
134
135 </body></html>