| 1 |
<html><head> |
| 2 |
|
| 3 |
<title>Math</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>Math</h1> |
| 52 |
|
| 53 |
<hr> |
| 54 |
|
| 55 |
<ol> |
| 56 |
<li><nobr><a href="#number-types">Number Types</a></nobr></li> |
| 57 |
<li><nobr><a href="#integer-limits">Integer Limits</a></nobr></li> |
| 58 |
<li><nobr><a href="#rounding-and-truncation">Rounding and Truncation</a></nobr></li> |
| 59 |
<ul> |
| 60 |
<li><nobr><a href="#cl-round">cl:round</a> - round towards the next integer</nobr></li> |
| 61 |
<li><nobr><a href="#cl-truncate">cl:truncate</a> - truncate towards zero</nobr></li> |
| 62 |
<li><nobr><a href="#cl-ceiling">cl:ceiling</a> - truncate towards positive infinity</nobr></li> |
| 63 |
<li><nobr><a href="#cl-floor">cl:floor</a> - truncate towards negative infinity</nobr></li> |
| 64 |
</ul> |
| 65 |
<li><nobr><li><nobr><a href="#remainder-and-modulus">Remainder and Modulus</a></nobr></li></nobr></li> |
| 66 |
<ul> |
| 67 |
<li><nobr><a href="#cl-rem">cl:rem</a></nobr></li> |
| 68 |
<li><nobr><a href="#cl-mod">cl:mod</a></nobr></li> |
| 69 |
</ul> |
| 70 |
<li><nobr>Power and Roots</nobr></li> |
| 71 |
<ul> |
| 72 |
<li><nobr><a href="#cl-exp">cl:exp</a> - compute 'e' to the power of 'x'</nobr></li> |
| 73 |
<li><nobr><a href="#cl-expt">cl:expt</a> - compute 'x' to the power of 'y'</nobr></li> |
| 74 |
<li><nobr><a href="#cl-log">cl:log</a></nobr></li> |
| 75 |
<li><nobr><a href="#cl-sqrt">cl:sqrt</a></nobr></li> |
| 76 |
</ul> |
| 77 |
</ol> |
| 78 |
|
| 79 |
<hr> |
| 80 |
|
| 81 |
<h2>Number Types</h2> |
| 82 |
|
| 83 |
<hr> |
| 84 |
|
| 85 |
<p>Nyquist/XLISP only knows two types of numers:</p> |
| 86 |
|
| 87 |
<ul> |
| 88 |
<li><nobr><b>fixnum</b> - integer numbers</nobr></li> |
| 89 |
<li><nobr><b>flonum</b> - floating-point numbers</nobr></li> |
| 90 |
</ul> |
| 91 |
|
| 92 |
<p>In Nyquist/XLISP, there are no ratios or complex numbers. Even if the |
| 93 |
math functions on this page are modelled after <nobr>Common Lisp</nobr>, no |
| 94 |
attempt is made to emulate these numbers.</p> |
| 95 |
|
| 96 |
<a name="integer-limits"></a> |
| 97 |
|
| 98 |
<hr> |
| 99 |
|
| 100 |
<h2>Integer Limits</h2> |
| 101 |
|
| 102 |
<hr> |
| 103 |
|
| 104 |
<pre class="example"> |
| 105 |
(setq <font color="#AA5500">*most-positive-fixnum*</font> 2147483647) |
| 106 |
(setq <font color="#AA5500">*most-negative-fixnum*</font> -2147483648) |
| 107 |
</pre> |
| 108 |
|
| 109 |
<p><b>Note:</b> these are the limits for <nobr>32-bit</nobr> machines.</p> |
| 110 |
|
| 111 |
<pre class="example"> |
| 112 |
(defun <font color="#0000CC">fixnum-bits</font> () |
| 113 |
(dolist (bits '(15 31 63) nil) |
| 114 |
(let ((fixnum (round (expt 2.0 bits)))) |
| 115 |
(and (plusp (1- fixnum)) |
| 116 |
(minusp fixnum) |
| 117 |
(return (1+ bits)))))) |
| 118 |
</pre> |
| 119 |
|
| 120 |
<pre class="example"> |
| 121 |
(defun <font color="#0000CC">fixnum-limits</font> () |
| 122 |
(if (dolist (bits '(15 31 63) nil) |
| 123 |
(let* ((negative (round (expt 2.0 bits))) |
| 124 |
(positive (1- negative))) |
| 125 |
(when (and (plusp positive) |
| 126 |
(minusp negative)) |
| 127 |
(setq most-positive-fixnum positive |
| 128 |
most-negative-fixnum negative) |
| 129 |
(return t)))) |
| 130 |
most-positive-fixnum |
| 131 |
(error <font color="#880000">"fixnum limit not found"</font>))) |
| 132 |
</pre> |
| 133 |
|
| 134 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 135 |
|
| 136 |
<a name="print-float"></a> |
| 137 |
|
| 138 |
<hr> |
| 139 |
|
| 140 |
<h2>print-float</h2> |
| 141 |
|
| 142 |
<hr> |
| 143 |
|
| 144 |
<p>The '<nobr>print-float</nobr>' function prints |
| 145 |
<nobr>floating-point</nobr> numbers ending in '.0' as |
| 146 |
<nobr>floating-point</nobr> numbers and not as integers:</p> |
| 147 |
|
| 148 |
<pre class="example"> |
| 149 |
(defun <font color="#0000CC">print-float</font> (item) |
| 150 |
(if (not (floatp item)) |
| 151 |
item |
| 152 |
(let ((string (format nil <font color="#880000">"~a"</font> item))) |
| 153 |
(if (not (string-search <font color="#880000">"."</font> string)) |
| 154 |
(strcat string <font color="#880000">".0"</font>) |
| 155 |
string)))) |
| 156 |
</pre> |
| 157 |
|
| 158 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 159 |
|
| 160 |
<a name="divide-float"></a> |
| 161 |
|
| 162 |
<hr> |
| 163 |
|
| 164 |
<h2>divide-float</h2> |
| 165 |
|
| 166 |
<hr> |
| 167 |
|
| 168 |
<p>An easy way to force a sequence of integers to be divided as floating |
| 169 |
point numbers is to insert the number 1.0 after the first argument in the |
| 170 |
list of arguments to the divider function or to explicitely convert the |
| 171 |
first argument into a floating point number by using the XLISP <a |
| 172 |
href="float.htm">float</a> function:</p> |
| 173 |
|
| 174 |
<pre class="example"> |
| 175 |
(defun <font color="#0000CC">divide-float</font> (&rest args) |
| 176 |
(if (null args) |
| 177 |
(error <font color="#880000">"too few arguments"</font>) |
| 178 |
(apply #'/ (cons (float (first args)) (rest args))))) |
| 179 |
</pre> |
| 180 |
|
| 181 |
<p>See <a href="apply.htm">apply</a>, <a href="cons.htm">cons</a>, |
| 182 |
<a href="defun.htm">defun</a>, <a href="error.htm">error</a>, |
| 183 |
<a href="first.htm">first</a>, <a href="float.htm">float</a>, |
| 184 |
<a href="if.htm">if</a>, <a href="null.htm">null</a>, |
| 185 |
<a href="rest.htm">rest</a>, |
| 186 |
<a href="lambda-keyword-rest.htm">&rest</a>.</p> |
| 187 |
|
| 188 |
<p>Examples:</p> |
| 189 |
|
| 190 |
<pre class="example"> |
| 191 |
(divide-float 1) => 1.0 |
| 192 |
(divide-float 1 2) => 0.5 |
| 193 |
</pre> |
| 194 |
|
| 195 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 196 |
|
| 197 |
<a name="rounding-and-truncation"></a> |
| 198 |
|
| 199 |
<hr> |
| 200 |
|
| 201 |
<h2>Rounding and Truncation</h2> |
| 202 |
|
| 203 |
<hr> |
| 204 |
|
| 205 |
<p>The <a href="#cl-round">cl:round</a>, |
| 206 |
<a href="#cl-truncate">cl:truncate</a>, |
| 207 |
<a href="#cl-ceiling">cl:ceiling</a> and |
| 208 |
<a href="#cl-floor">cl:floor</a> functions divide a number by a divisor, |
| 209 |
returning a quotient and a remainder:</p> |
| 210 |
|
| 211 |
<p><div class="box"> |
| 212 |
|
| 213 |
<p><table cellpadding="0" cellspacing="0"><tbody> |
| 214 |
<tr valign="top"> |
| 215 |
<td align="right"><nobr>(<a href="#cl-round">cl:round</a> </nobr></td> |
| 216 |
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td> |
| 217 |
<td><nobr> ⇒ </nobr></td> |
| 218 |
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td> |
| 219 |
</tr> |
| 220 |
<tr valign="top"> |
| 221 |
<td align="right"><nobr>(<a href="#cl-truncate">cl:truncate</a> </nobr></td> |
| 222 |
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td> |
| 223 |
<td><nobr> ⇒ </nobr></td> |
| 224 |
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td> |
| 225 |
</tr> |
| 226 |
<tr valign="top"> |
| 227 |
<td align="right"><nobr>(<a href="#cl-ceiling">cl:ceiling</a> </nobr></td> |
| 228 |
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td> |
| 229 |
<td><nobr> ⇒ </nobr></td> |
| 230 |
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td> |
| 231 |
</tr> |
| 232 |
<tr valign="top"> |
| 233 |
<td align="right"><nobr>(<a href="#cl-floor">cl:floor</a> </nobr></td> |
| 234 |
<td><nobr><i>number</i> [<i>divisor</i>])</nobr></td> |
| 235 |
<td><nobr> ⇒ </nobr></td> |
| 236 |
<td><nobr><i>quotient</i>, <i>remainder</i></nobr></td> |
| 237 |
</tr> |
| 238 |
</tbody></table></p> |
| 239 |
|
| 240 |
<p><nobr> |
| 241 |
<i>quotient</i> * <i>divisor</i> + <i>remainder</i> = <i>number</i></nobr></p> |
| 242 |
|
| 243 |
</div></p> |
| 244 |
|
| 245 |
<p>The 'quotient' always represents a mathematical integer. <nobr>The |
| 246 |
'remainder'</nobr> is an integer if both 'number' and 'divisor' arguments |
| 247 |
are integers, and a <nobr>floating-point</nobr> number if either the |
| 248 |
'number' or the 'divisor' or both are <nobr>floating-point</nobr> |
| 249 |
numbers.</p> |
| 250 |
|
| 251 |
<p>With Nyquist/XLISP, the 'quotient' is always directly returned by the |
| 252 |
function, while a list:</p> |
| 253 |
|
| 254 |
<pre class="example"> |
| 255 |
(<font color="#0000CC">quotient remainder</font>) |
| 256 |
</pre> |
| 257 |
|
| 258 |
<p>is stored in the Nyquist/XLISP |
| 259 |
<a href="../reference/global-rslt.htm">*rslt*</a> variable and the |
| 260 |
<a href="values.htm#cl-global-multiple-values">cl:*multiple-values*</a> is |
| 261 |
set to <a href="../reference/t.htm"> T </a> to signal that |
| 262 |
<a href="values.htm">Multiple Values</a> are returned.</p> |
| 263 |
|
| 264 |
Examples: |
| 265 |
|
| 266 |
<pre class="example"> |
| 267 |
(cl:round 3.5) => 4 <font color="#008844">; *rslt* = ( 4 -0.5)</font> |
| 268 |
(cl:truncate 3.5) => 3 <font color="#008844">; *rslt* = ( 3 0.5)</font> |
| 269 |
(cl:ceiling 3.5) => 4 <font color="#008844">; *rslt* = ( 4 -0.5)</font> |
| 270 |
(cl:floor 3.5) => 3 <font color="#008844">; *rslt* = ( 3 0.5)</font> |
| 271 |
|
| 272 |
(cl:round -3.5) => -4 <font color="#008844">; *rslt* = (-4 0.5)</font> |
| 273 |
(cl:truncate -3.5) => -3 <font color="#008844">; *rslt* = (-3 -0.5)</font> |
| 274 |
(cl:ceiling -3.5) => -3 <font color="#008844">; *rslt* = (-3 -0.5)</font> |
| 275 |
(cl:floor -3.5) => -4 <font color="#008844">; *rslt* = (-4 0.5)</font> |
| 276 |
</pre> |
| 277 |
|
| 278 |
Force integer division: |
| 279 |
|
| 280 |
<pre class="example"> |
| 281 |
(cl:truncate 3.0 2.0) => 1 |
| 282 |
(/ (truncate 3.0) (truncate 2.0)) => 1 |
| 283 |
(/ 3 4) => 1 |
| 284 |
</pre> |
| 285 |
|
| 286 |
<p><div class="box"> |
| 287 |
|
| 288 |
<p><b>Implementation Notes</b></p> |
| 289 |
|
| 290 |
<pre class="example"> |
| 291 |
(defun <font color="#0000CC">name</font> (number &optional (divisor (if (<font color="#AA0000">integerp</font> number) 1 1.0))) |
| 292 |
... ) |
| 293 |
</pre> |
| 294 |
|
| 295 |
<p>The <a href="../reference/integerp.htm">integerp</a> test in the |
| 296 |
parameter list signals an error if the 'number' argument is not a number, |
| 297 |
also the <nobr><a href="../reference/division.htm"> / </a> |
| 298 |
[division]</nobr> function signals errors if the 'divisor' argument is zero |
| 299 |
or not a number, so we do not explicitely need to test the arguments.</p> |
| 300 |
|
| 301 |
<p>The <nobr><a href="#cl-ceiling">cl:ceiling</a></nobr> and <nobr><a |
| 302 |
href="#cl-floor">cl:floor</a></nobr> functions test if 'number' is an |
| 303 |
integer multiple of 'divisor' by comparing the results of an integer |
| 304 |
division and a <nobr>floating-point</nobr> division:</p> |
| 305 |
|
| 306 |
<pre class="example"> |
| 307 |
(let ((<font color="#AA0000">i-quotient</font> (/ (truncate number) (truncate divisor))) |
| 308 |
(<font color="#AA0000">f-quotient</font> (/ (float number) divisor))) |
| 309 |
(if (= <font color="#AA0000">i-quotient f-quotient</font>) |
| 310 |
... |
| 311 |
</pre> |
| 312 |
|
| 313 |
<p>I'm not sure if this really catches all cases <nobr>[e.g. |
| 314 |
regarding</nobr> floating point precision], but have found no problems so |
| 315 |
far.</p> |
| 316 |
|
| 317 |
</div></p> |
| 318 |
|
| 319 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 320 |
|
| 321 |
<a name="cl-round"></a> |
| 322 |
|
| 323 |
<hr> |
| 324 |
|
| 325 |
<h2>cl:round</h2> |
| 326 |
|
| 327 |
<hr> |
| 328 |
|
| 329 |
<p>The '<nobr>cl:round</nobr>' function truncates towards the next |
| 330 |
integer:</p> |
| 331 |
|
| 332 |
<p><div class="box"> |
| 333 |
|
| 334 |
<dl> |
| 335 |
<dt>(cl:<b>round</b> <i>number </i> [<i>divisor</i>])</dt> |
| 336 |
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br> |
| 337 |
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br> |
| 338 |
<table cellpadding="0" cellspacing="0"><tbody> |
| 339 |
<tr> |
| 340 |
<td valign="top"><nobr>returns</nobr></td> |
| 341 |
<td valign="top"><nobr> - </nobr></td> |
| 342 |
<td width="100%">the result of runding the result of <i>number</i> divided by <i>divisor</i></td> |
| 343 |
</tr> |
| 344 |
<tr> |
| 345 |
<td></td> |
| 346 |
<td valign="top"><nobr> - </nobr></td> |
| 347 |
<td width="100%">the remainder of the round operation</td> |
| 348 |
</tr> |
| 349 |
</tbody></table></dd> |
| 350 |
</dl> |
| 351 |
|
| 352 |
</div></p> |
| 353 |
|
| 354 |
<pre class="example"> |
| 355 |
(defun <font color="#0000CC">cl:round</font> (number &optional (divisor |
| 356 |
(if (integerp number) 1 1.0) |
| 357 |
divisor-p)) |
| 358 |
(let* ((x (/ (float number) divisor)) |
| 359 |
(quotient (cond ((and (not divisor-p) (integerp number)) number) |
| 360 |
((= number divisor) 1) |
| 361 |
((plusp x) (truncate (+ x 0.5))) |
| 362 |
((= (- x 0.5) (truncate (- x 0.5))) |
| 363 |
(if (minusp x) |
| 364 |
(1- (truncate x)) |
| 365 |
(truncate x))) |
| 366 |
(t (truncate (- x 0.5)))))) |
| 367 |
(setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor))) |
| 368 |
<font color="#AA5500">cl:*multiple-values*</font> t) |
| 369 |
quotient)) |
| 370 |
</pre> |
| 371 |
|
| 372 |
<p>The '<nobr>cl:round</nobr>' function computes a quotient that has been rounded to the |
| 373 |
nearest mathematical integer. <nobr>If the</nobr> mathematical quotient is |
| 374 |
exactly halfway between two integers, [that is, it has the form |
| 375 |
<nobr>'integer+1/2']</nobr>, then the quotient has been rounded to the even |
| 376 |
[divisible <nobr>by two]</nobr> integer. <nobr>See |
| 377 |
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr> |
| 378 |
above for more details.</p> |
| 379 |
|
| 380 |
<pre class="example"> |
| 381 |
(round 3.5) => 4 |
| 382 |
(round -3.5) => -3 |
| 383 |
|
| 384 |
(cl:round 3.5) => 4 <font color="#008844">; *rslt* = ( 4 -0.5)</font> |
| 385 |
(cl:round -3.5) => -4 <font color="#008844">; *rslt* = (-4 0.5)</font> |
| 386 |
</pre> |
| 387 |
|
| 388 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 389 |
|
| 390 |
<a name="cl-truncate"></a> |
| 391 |
|
| 392 |
<hr> |
| 393 |
|
| 394 |
<h2>cl:truncate</h2> |
| 395 |
|
| 396 |
<hr> |
| 397 |
|
| 398 |
<p>The '<nobr>cl:truncate</nobr>' function truncates towards zero:</p> |
| 399 |
|
| 400 |
<p><div class="box"> |
| 401 |
|
| 402 |
<dl> |
| 403 |
<dt>(cl:<b>truncate</b> <i>number </i> [<i>divisor</i>])</dt> |
| 404 |
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br> |
| 405 |
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br> |
| 406 |
<table cellpadding="0" cellspacing="0"><tbody> |
| 407 |
<tr> |
| 408 |
<td valign="top"><nobr>returns</nobr></td> |
| 409 |
<td valign="top"><nobr> - </nobr></td> |
| 410 |
<td width="100%">the result of truncating the result of <i>number</i> divided by <i>divisor</i></td> |
| 411 |
</tr> |
| 412 |
<tr> |
| 413 |
<td></td> |
| 414 |
<td valign="top"><nobr> - </nobr></td> |
| 415 |
<td width="100%">the remainder of the truncate operation</td> |
| 416 |
</tr> |
| 417 |
</tbody></table></dd> |
| 418 |
</dl> |
| 419 |
|
| 420 |
</div></p> |
| 421 |
|
| 422 |
<pre class="example"> |
| 423 |
(defun <font color="#0000CC">cl:truncate</font> (number &optional (divisor (if (integerp number) 1 1.0))) |
| 424 |
(let ((quotient (truncate (/ (float number) divisor)))) |
| 425 |
(setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor))) |
| 426 |
<font color="#AA5500">cl:*multiple-values*</font> t) |
| 427 |
quotient)) |
| 428 |
</pre> |
| 429 |
|
| 430 |
<p>The '<nobr>cl:truncate</nobr>' function computes a quotient that has been |
| 431 |
truncated towards zero. That is, the quotient represents the mathematical |
| 432 |
integer of the same sign as the mathematical quotient, and that has the |
| 433 |
greatest integral magnitude not greater than that of the mathematical |
| 434 |
quotient. <nobr>See |
| 435 |
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr> |
| 436 |
above for more details.</p> |
| 437 |
|
| 438 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 439 |
|
| 440 |
<a name="cl-ceiling"></a> |
| 441 |
|
| 442 |
<hr> |
| 443 |
|
| 444 |
<h2>cl:ceiling</h2> |
| 445 |
|
| 446 |
<hr> |
| 447 |
|
| 448 |
<p>The '<nobr>cl:ceiling</nobr>' function truncates towards positive |
| 449 |
infinity:</p> |
| 450 |
|
| 451 |
<p><div class="box"> |
| 452 |
|
| 453 |
<dl> |
| 454 |
<dt>(cl:<b>ceiling</b> <i>number </i> [<i>divisor</i>])</dt> |
| 455 |
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br> |
| 456 |
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br> |
| 457 |
<table cellpadding="0" cellspacing="0"><tbody> |
| 458 |
<tr> |
| 459 |
<td valign="top"><nobr>returns</nobr></td> |
| 460 |
<td valign="top"><nobr> - </nobr></td> |
| 461 |
<td width="100%">the result of truncating the result of <i>number</i> divided by <i>divisor</i></td> |
| 462 |
</tr> |
| 463 |
<tr> |
| 464 |
<td></td> |
| 465 |
<td valign="top"><nobr> - </nobr></td> |
| 466 |
<td width="100%">the remainder of the truncate operation</td> |
| 467 |
</tr> |
| 468 |
</tbody></table></dd> |
| 469 |
</dl> |
| 470 |
|
| 471 |
</div></p> |
| 472 |
|
| 473 |
<pre class="example"> |
| 474 |
(defun <font color="#0000CC">cl:ceiling</font> (number &optional (divisor |
| 475 |
(if (integerp number) 1 1.0) |
| 476 |
divisor-p)) |
| 477 |
(let ((quotient |
| 478 |
(cond ((and (not divisor-p) (integerp number)) number) |
| 479 |
((= number divisor) 1) |
| 480 |
(t (let ((i-quotient (/ (truncate number) (truncate divisor))) |
| 481 |
(f-quotient (/ (float number) divisor))) |
| 482 |
(if (or (= i-quotient f-quotient) <font color="#008844">; integer result</font> |
| 483 |
(not (plusp f-quotient))) |
| 484 |
(truncate f-quotient) |
| 485 |
(1+ (truncate f-quotient)))))))) |
| 486 |
(setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor))) |
| 487 |
<font color="#AA5500">cl:*multiple-values*</font> t) |
| 488 |
quotient)) |
| 489 |
</pre> |
| 490 |
|
| 491 |
<p>The '<nobr>cl:ceiling</nobr>' function computes a quotient that has been |
| 492 |
truncated toward positive infinity. That is, the quotient represents the |
| 493 |
smallest mathematical integer that is not smaller than the mathematical |
| 494 |
result. <nobr>See |
| 495 |
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr> |
| 496 |
above for more details.</p> |
| 497 |
|
| 498 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 499 |
|
| 500 |
<a name="cl-floor"></a> |
| 501 |
|
| 502 |
<hr> |
| 503 |
|
| 504 |
<h2>cl:floor</h2> |
| 505 |
|
| 506 |
<hr> |
| 507 |
|
| 508 |
<p>The '<nobr>cl:floor</nobr>' function truncates towards negative infinity:</p> |
| 509 |
|
| 510 |
<p><div class="box"> |
| 511 |
|
| 512 |
<dl> |
| 513 |
<dt>(cl:<b>floor</b> <i>number </i> [<i>divisor</i>])</dt> |
| 514 |
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br> |
| 515 |
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br> |
| 516 |
<table cellpadding="0" cellspacing="0"><tbody> |
| 517 |
<tr> |
| 518 |
<td valign="top"><nobr>returns</nobr></td> |
| 519 |
<td valign="top"><nobr> - </nobr></td> |
| 520 |
<td width="100%">the result of truncating the result of <i>number</i> divided by <i>divisor</i></td> |
| 521 |
</tr> |
| 522 |
<tr> |
| 523 |
<td></td> |
| 524 |
<td valign="top"><nobr> - </nobr></td> |
| 525 |
<td width="100%">the remainder of the truncate operation</td> |
| 526 |
</tr> |
| 527 |
</tbody></table></dd> |
| 528 |
</dl> |
| 529 |
|
| 530 |
</div></p> |
| 531 |
|
| 532 |
<pre class="example"> |
| 533 |
(defun <font color="#0000CC">cl:floor</font> (number &optional (divisor |
| 534 |
(if (integerp number) 1 1.0) |
| 535 |
divisor-p)) |
| 536 |
(let ((quotient |
| 537 |
(cond ((and (not divisor-p) (integerp number)) number) |
| 538 |
((= number divisor) 1) |
| 539 |
(t (let ((i-quotient (/ (truncate number) (truncate divisor))) |
| 540 |
(f-quotient (/ (float number) divisor))) |
| 541 |
(if (or (= i-quotient f-quotient) <font color="#008844">; integer result</font> |
| 542 |
(not (minusp f-quotient))) |
| 543 |
(truncate f-quotient) |
| 544 |
(1- (truncate f-quotient)))))))) |
| 545 |
(setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor))) |
| 546 |
<font color="#AA5500">cl:*multiple-values*</font> t) |
| 547 |
quotient)) |
| 548 |
</pre> |
| 549 |
|
| 550 |
<p>The <nobr>'cl:floor</nobr>' function computes a quotient that has been |
| 551 |
truncated toward negative infinity. That is, the quotient represents the |
| 552 |
largest mathematical integer that is not larger than the mathematical |
| 553 |
quotient. <nobr>See |
| 554 |
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr> |
| 555 |
above for more details.</p> |
| 556 |
|
| 557 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 558 |
|
| 559 |
<a name="remainder-and-modulus"></a> |
| 560 |
|
| 561 |
<hr> |
| 562 |
|
| 563 |
<h2>Remainder and Modulus</h2> |
| 564 |
|
| 565 |
<hr> |
| 566 |
|
| 567 |
<p>The <a href="#cl-mod">cl:mod</a> and <a href="#cl-rem">cl:rem</a> |
| 568 |
function are generalizations of the modulus and remainder functions. |
| 569 |
<nobr>The <a href="#cl-mod">cl:mod</a></nobr> function performs the |
| 570 |
<a href="#cl-floor">cl:floor</a> operation on its arguments and returns the |
| 571 |
remainder of the <a href="#cl-floor">cl:floor</a> operation. |
| 572 |
<nobr>The <a href="#cl-rem">cl:rem</a></nobr> function performs the |
| 573 |
<a href="cl-truncate">cl:truncate</a> operation on its arguments and returns |
| 574 |
the remainder of the <a href="cl-truncate">cl:truncate</a> operation. |
| 575 |
<nobr>The <a href="#cl-mod">cl:mod</a></nobr> and |
| 576 |
<a href="#cl-rem">cl:rem</a> functions are the modulus and remainder |
| 577 |
functions when the 'number' and 'divisor' arguments both are integers.</p> |
| 578 |
|
| 579 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 580 |
|
| 581 |
<a name="cl-rem"></a> |
| 582 |
|
| 583 |
<hr> |
| 584 |
|
| 585 |
<h2>cl:rem</h2> |
| 586 |
|
| 587 |
<hr> |
| 588 |
|
| 589 |
<p><div class="box"> |
| 590 |
|
| 591 |
<dl> |
| 592 |
<dt>(cl:<b>rem</b> <i>number divisor</i>)</dt> |
| 593 |
<dd><i>number</i> - an integer or floating-point number<br> |
| 594 |
<i>divisor</i> - an integer or floating-point number<br> |
| 595 |
returns - the remainder of a <a href="cl-truncate">cl:truncate</a> operation</dd> |
| 596 |
</dl> |
| 597 |
|
| 598 |
</div></p> |
| 599 |
|
| 600 |
<pre class="example"> |
| 601 |
(defun <font color="#0000CC">cl:rem</font> (number divisor) |
| 602 |
(if (= (abs number) (abs divisor)) |
| 603 |
(if (and (integerp number) (integerp divisor)) 0 0.0) |
| 604 |
(let ((quotient (truncate (/ (float number) divisor)))) |
| 605 |
(- number (* quotient divisor))))) |
| 606 |
</pre> |
| 607 |
|
| 608 |
<p>The '<nobr>cl:rem</nobr>' function performs the |
| 609 |
<a href="cl-truncate">cl:truncate</a> operation on its arguments and returns |
| 610 |
the remainder of the <a href="cl-truncate">cl:truncate</a> operation. |
| 611 |
<nobr>The result</nobr> is either zero or an integer or |
| 612 |
<nobr>floating-point</nobr> number with the same sign as the 'number' |
| 613 |
argument. <nobr>If both</nobr> arguments are integer numbers, the |
| 614 |
'<nobr>cl:rem</nobr>' function is equal to the mathematical remainder |
| 615 |
function.</p> |
| 616 |
|
| 617 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 618 |
|
| 619 |
<a name="cl-mod"></a> |
| 620 |
|
| 621 |
<hr> |
| 622 |
|
| 623 |
<h2>cl:mod</h2> |
| 624 |
|
| 625 |
<hr> |
| 626 |
|
| 627 |
<p><div class="box"> |
| 628 |
|
| 629 |
<dl> |
| 630 |
<dt>(cl:<b>mod</b> <i>number divisor</i>)</dt> |
| 631 |
<dd><i>number</i> - an integer or floating-point number<br> |
| 632 |
<i>divisor</i> - an integer or floating-point number<br> |
| 633 |
returns - the remainder of a <a href="#cl-floor">cl:floor</a> operation</dd> |
| 634 |
</dl> |
| 635 |
|
| 636 |
</div></p> |
| 637 |
|
| 638 |
<pre class="example"> |
| 639 |
(defun <font color="#0000CC">cl:mod</font> (number divisor) |
| 640 |
(if (= (abs number) (abs divisor)) |
| 641 |
(if (and (integerp number) (integerp divisor)) 0 0.0) |
| 642 |
(let* ((i-quotient (/ (truncate number) (truncate divisor))) |
| 643 |
(f-quotient (/ (float number) divisor)) |
| 644 |
(quotient (if (or (= i-quotient f-quotient) <font color="#008844">; integer result</font> |
| 645 |
(not (minusp f-quotient))) |
| 646 |
(truncate f-quotient) |
| 647 |
(1- (truncate f-quotient))))) |
| 648 |
(- number (* quotient divisor))))) |
| 649 |
</pre> |
| 650 |
|
| 651 |
<p>The '<nobr>cl:mod</nobr>' function performs the |
| 652 |
<a href="#cl-floor">cl:floor</a> operation on its arguments and returns the |
| 653 |
remainder of the <a href="#cl-floor">cl:floor</a> operation. <nobr>The |
| 654 |
result</nobr> is either zero or an integer or <nobr>floating-point</nobr> |
| 655 |
number with the same sign as the 'divisor' argument. <nobr>If both</nobr> |
| 656 |
arguments are integer numbers, the '<nobr>cl:rem</nobr>' function is equal |
| 657 |
to the mathematical modulus function.</p> |
| 658 |
|
| 659 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 660 |
|
| 661 |
<a name="cl-exp"></a> |
| 662 |
|
| 663 |
<hr> |
| 664 |
|
| 665 |
<h2>cl:exp</h2> |
| 666 |
|
| 667 |
<hr> |
| 668 |
|
| 669 |
<p>The '<nobr>cl:exp</nobr>' function does the same as the Nyquist/XLISP |
| 670 |
<a href="../reference/exp.htm">exp</a> function, but it also accepts |
| 671 |
integer numbers as argument:</p> |
| 672 |
|
| 673 |
<p><div class="box"> |
| 674 |
|
| 675 |
<dl> |
| 676 |
<dt>(cl:<b>exp</b> <i>power</i>)</dt> |
| 677 |
<dd><i>power</i> - an integer or floating-point number<br> |
| 678 |
returns - the result of <nobr>'e' [2.7128]</nobr> to the power of <i>power</i></dd> |
| 679 |
</dl> |
| 680 |
|
| 681 |
</div></p> |
| 682 |
|
| 683 |
<pre class="example"> |
| 684 |
(defun <font color="#0000CC">cl:exp</font> (x) |
| 685 |
(exp (float x))) |
| 686 |
</pre> |
| 687 |
|
| 688 |
<p>The '<nobr>cl:exp</nobr>' function computes <nobr>'e' [2.7128]</nobr> |
| 689 |
raised to the specified 'power'. <nobr>The result</nobr> is always a |
| 690 |
<nobr>floating-point</nobr> number.</p> |
| 691 |
|
| 692 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 693 |
|
| 694 |
<a name="cl-expt"></a> |
| 695 |
|
| 696 |
<hr> |
| 697 |
|
| 698 |
<h2>cl:expt</h2> |
| 699 |
|
| 700 |
<hr> |
| 701 |
|
| 702 |
<p>The '<nobr>cl:expt</nobr>' function computes the result of 'x' to the |
| 703 |
power <nobr>of 'y'</nobr>:</p> |
| 704 |
|
| 705 |
<p><div class="box"> |
| 706 |
|
| 707 |
<dl> |
| 708 |
<dt>(cl:<b>expt</b> <i>base power</i>)</dt> |
| 709 |
<dd><i>base</i> - the base<br> |
| 710 |
<i>power</i> - the exponent<br> |
| 711 |
returns - the result of <i>base</i> to the power of <i>power</i></dd> |
| 712 |
</dl> |
| 713 |
|
| 714 |
</div></p> |
| 715 |
|
| 716 |
<pre class="example"> |
| 717 |
(defun <font color="#0000CC">cl:expt</font> (x y) |
| 718 |
(let ((power (expt (float x) y))) |
| 719 |
(if (and (integerp x) (integerp y)) |
| 720 |
(round power) |
| 721 |
power))) |
| 722 |
</pre> |
| 723 |
|
| 724 |
<p>See <a href="../reference/and.htm">and</a>, |
| 725 |
<a href="../reference/defun.htm">defun</a>, |
| 726 |
<a href="../reference/expt.htm">expt</a>, |
| 727 |
<a href="../reference/float.htm">float</a>, |
| 728 |
<nobr><a href="../reference/if.htm"> if </a></nobr>, |
| 729 |
<a href="../reference/integerp.htm">integerp</a>, |
| 730 |
<a href="../reference/let.htm">let</a>, |
| 731 |
<a href="../reference/power.htm">power</a>, |
| 732 |
<a href="../reference/round.htm">round</a>.</p> |
| 733 |
|
| 734 |
<p>The '<nobr>cl:expt</nobr>' function accepts integer and floating point |
| 735 |
numbers as arguments. <nobr>If both</nobr> arguments are integer numbers, |
| 736 |
the result will be an integer number, <nobr>if one</nobr> or both arguments |
| 737 |
are <nobr>floating-point</nobr> numbers, the result will be a |
| 738 |
<nobr>floating-point</nobr> number. <nobr>In contrast</nobr> to the |
| 739 |
Nyquist/XLISP <a href="../reference/expt.htm">expt</a> function, the |
| 740 |
'<nobr>cl:expt</nobr>' function specifies exactly two arguments.</p> |
| 741 |
|
| 742 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 743 |
|
| 744 |
<a name="cl-log"></a> |
| 745 |
|
| 746 |
<hr> |
| 747 |
|
| 748 |
<h2>cl:log</h2> |
| 749 |
|
| 750 |
<hr> |
| 751 |
|
| 752 |
<p>The '<nobr>cl:log</nobr>' function does the same as the Nyquist/XLISP |
| 753 |
<a href="../reference/log.htm">log</a> function, but also accepts |
| 754 |
integer numbers and has an optional 'base' argument:</p> |
| 755 |
|
| 756 |
<p><div class="box"> |
| 757 |
|
| 758 |
<dl> |
| 759 |
<dt>(cl:<b>log</b> <i>number</i> [<i>base</i>])</dt> |
| 760 |
<dd><i>number</i> - an integer or floating-point number<br> |
| 761 |
<i>base</i> - an integer or floating-point number<br> |
| 762 |
returns - the the logarithm of <i>number</i> in base <i>base</i></dd> |
| 763 |
</dl> |
| 764 |
|
| 765 |
</div></p> |
| 766 |
|
| 767 |
<pre class="example"> |
| 768 |
(defun <font color="#0000CC">cl:log</font> (number &optional base) |
| 769 |
(if base |
| 770 |
(if (zerop base) |
| 771 |
0.0 |
| 772 |
(/ (log (float number)) (log (float base)))) |
| 773 |
(log (float number)))) |
| 774 |
</pre> |
| 775 |
|
| 776 |
<p>The '<nobr>cl:log</nobr>' function returns the logarithm of 'number' in |
| 777 |
base 'base'. <nobr>If 'base'</nobr> is not supplied its value <nobr>is |
| 778 |
'e'</nobr>, the base of the natural logarithms. <nobr>If the</nobr> 'base' |
| 779 |
argument is zero, then 'cl:log' returns zero. <nobr>The result</nobr> is |
| 780 |
always a <nobr>floating-point</nobr> number.</p> |
| 781 |
|
| 782 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 783 |
|
| 784 |
<a name="cl-sqrt"></a> |
| 785 |
|
| 786 |
<hr> |
| 787 |
|
| 788 |
<h2>cl:sqrt</h2> |
| 789 |
|
| 790 |
<hr> |
| 791 |
|
| 792 |
<p>The '<nobr>cl:sqrt</nobr>' function does the same as the Nyquist/XLISP |
| 793 |
<a href="../reference/sqrt.htm">sqrt</a> function, but it also accepts |
| 794 |
integer numbers as argument:</p> |
| 795 |
|
| 796 |
<p><div class="box"> |
| 797 |
|
| 798 |
<dl> |
| 799 |
<dt>(cl:<b>sqrt</b> <i>number</i>)</dt> |
| 800 |
<dd><i>number</i> - an integer or floating-point number<br> |
| 801 |
returns - the square root of <i>number</i></dd> |
| 802 |
</dl> |
| 803 |
|
| 804 |
</div></p> |
| 805 |
|
| 806 |
<pre class="example"> |
| 807 |
(defun <font color="#0000CC">cl:sqrt</font> (x) |
| 808 |
(sqrt (float x))) |
| 809 |
</pre> |
| 810 |
|
| 811 |
<p><nobr>The result</nobr> is always a <nobr>floating-point</nobr> |
| 812 |
number.</p> |
| 813 |
|
| 814 |
<p><nobr> <a href="#top">Back to top</a></nobr></p> |
| 815 |
|
| 816 |
<hr> |
| 817 |
|
| 818 |
<a href="../start.htm">Nyquist / XLISP 2.0</a> - |
| 819 |
<a href="../manual/contents.htm">Contents</a> | |
| 820 |
<a href="../tutorials/tutorials.htm">Tutorials</a> | |
| 821 |
<a href="examples.htm">Examples</a> | |
| 822 |
<a href="../reference/reference-index.htm">Reference</a> |
| 823 |
|
| 824 |
</body></html> |