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