1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
<html><head>
<title>Rounding and Truncation</title>
<style type="text/css">
.example {
color: #000000;
background-color: #F5F5F5;
padding: 8px;
border: #808080;
border-style: solid;
border-width: 1px;
width:auto;
}
.button {
color: #000000;
background-color: #F5F5F5;
padding-top: 1px;
padding-bottom: 1px;
padding-left: 4px;
padding-right: 8px;
border: #808080;
border-style: solid;
border-width: 1px;
white-space: pre;
}
.box {
color: #000000;
padding-top: 4px;
padding-bottom: 4px;
padding-left: 16px;
padding-right: 16px;
border: #808080;
border-style: solid;
border-width: 1px;
}
</style>
</head>
<body>
<a href="../../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../../manual/contents.htm">Contents</a> |
<a href="../../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples.htm">Examples</a> |
<a href="../../reference/reference-index.htm">Reference</a>
<hr>
<h1>Rounding and Truncation</h1>
<hr>
<p>The <a href="round.htm">cl:round</a>,
<a href="truncate.htm">cl:truncate</a>,
<a href="ceiling.htm">cl:ceiling</a> and
<a href="floor.htm">cl:floor</a> functions divide a number by a divisor,
returning a quotient and a remainder:</p>
<p><div class="box">
<p><table cellpadding="0" cellspacing="0"><tbody>
<tr valign="top">
<td align="right"><nobr>(<a href="round.htm">cl:round</a> </nobr></td>
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
<td><nobr> ⇒ </nobr></td>
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
<tr valign="top">
<td align="right"><nobr>(<a href="truncate.htm">cl:truncate</a> </nobr></td>
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
<td><nobr> ⇒ </nobr></td>
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
<tr valign="top">
<td align="right"><nobr>(<a href="ceiling.htm">cl:ceiling</a> </nobr></td>
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
<td><nobr> ⇒ </nobr></td>
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
<tr valign="top">
<td align="right"><nobr>(<a href="floor.htm">cl:floor</a> </nobr></td>
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
<td><nobr> ⇒ </nobr></td>
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
</tbody></table></p>
<p><nobr>
<i>quotient</i> * <i>divisor</i> + <i>remainder</i> = <i>number</i></nobr></p>
</div></p>
<p>The 'quotient' always represents a mathematical integer. <nobr>The
'remainder'</nobr> is an integer if both 'number' and 'divisor' arguments
are integers, and a <nobr>floating-point</nobr> number if either the
'number' or the 'divisor' or both are <nobr>floating-point</nobr>
numbers.</p>
<p>With Nyquist/XLISP, the 'quotient' is always directly returned by the
function, while a list:</p>
<pre class="example">
(<font color="#0000CC">quotient remainder</font>)
</pre>
<p>is stored in the Nyquist/XLISP
<a href="../../reference/global-rslt.htm">*rslt*</a> variable and the
<a href="global-multiple-values.htm">cl:*multiple-values*</a> is set to
<a href="../../reference/t.htm"> T </a> to signal that
<a href="multiple-values.htm">Multiple Values</a> are returned.</p>
Examples:
<pre class="example">
(cl:round 3.5) => 4 <font color="#008844">; *rslt* = ( 4 -0.5)</font>
(cl:truncate 3.5) => 3 <font color="#008844">; *rslt* = ( 3 0.5)</font>
(cl:ceiling 3.5) => 4 <font color="#008844">; *rslt* = ( 4 -0.5)</font>
(cl:floor 3.5) => 3 <font color="#008844">; *rslt* = ( 3 0.5)</font>
(cl:round -3.5) => -4 <font color="#008844">; *rslt* = (-4 0.5)</font>
(cl:truncate -3.5) => -3 <font color="#008844">; *rslt* = (-3 -0.5)</font>
(cl:ceiling -3.5) => -3 <font color="#008844">; *rslt* = (-3 -0.5)</font>
(cl:floor -3.5) => -4 <font color="#008844">; *rslt* = (-4 0.5)</font>
</pre>
<p>Force an integer division:</p>
<pre class="example">
(cl:truncate 3.0 2.0) => 1 <font color="#008844">; Common Lisp</font>
(/ (truncate 3.0) (truncate 2.0)) => 1 <font color="#008844">; Nyquist/XLISP</font>
(/ 3 2) => 1 <font color="#008844">; integer division</font>
</pre>
<p><div class="box">
<p><b>Implementation Notes</b></p>
<pre class="example">
(defun <font color="#0000CC">name</font> (number &optional (divisor (if (<font color="#AA0000">integerp</font> number) 1 1.0)))
... )
</pre>
<p>The <a href="../../reference/integerp.htm">integerp</a> test in the
parameter list signals an error if the 'number' argument is not a number,
also the <nobr><a href="../../reference/division.htm"> / </a>
[division]</nobr> function signals errors if the 'divisor' argument is zero
or not a number, so we do not explicitely need to test the arguments.</p>
<p>The <nobr><a href="ceiling.htm">cl:ceiling</a></nobr> and
<nobr><a href="floor.htm">cl:floor</a></nobr> functions test if 'number' is
an integer multiple of 'divisor' by comparing the results of an integer
division and a <nobr>floating-point</nobr> division:</p>
<pre class="example">
(let ((<font color="#AA0000">i-quotient</font> (/ (truncate number) (truncate divisor)))
(<font color="#AA0000">f-quotient</font> (/ (float number) divisor)))
(if (= <font color="#AA0000">i-quotient f-quotient</font>)
...
</pre>
<p>I'm not sure if this really catches all cases <nobr>[e.g.
regarding</nobr> floating point precision], but have found no problems so
far.</p>
</div></p>
<p><nobr> <a href="#top">Back to top</a></nobr></p>
<hr>
<a href="../../start.htm">Nyquist / XLISP 2.0</a> -
<a href="../../manual/contents.htm">Contents</a> |
<a href="../../tutorials/tutorials.htm">Tutorials</a> |
<a href="../examples.htm">Examples</a> |
<a href="../../reference/reference-index.htm">Reference</a>
</body></html>
|