File: math.htm

package info (click to toggle)
nyquist 3.05-2.1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 31,172 kB
  • ctags: 22,924
  • sloc: ansic: 149,216; sh: 21,301; lisp: 17,746; cpp: 12,778; java: 8,006; makefile: 4,574; python: 39
file content (824 lines) | stat: -rw-r--r-- 27,196 bytes parent folder | download | duplicates (7)
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
<html><head>

<title>Math</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>&nbsp; -&nbsp;
<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>Math</h1>

<hr>

<ol>
<li><nobr><a href="#number-types">Number Types</a></nobr></li>
<li><nobr><a href="#integer-limits">Integer Limits</a></nobr></li>
<li><nobr><a href="#rounding-and-truncation">Rounding and Truncation</a></nobr></li>
<ul>
<li><nobr><a href="#cl-round">cl:round</a> - round towards the next integer</nobr></li>
<li><nobr><a href="#cl-truncate">cl:truncate</a> - truncate towards zero</nobr></li>
<li><nobr><a href="#cl-ceiling">cl:ceiling</a> - truncate towards positive infinity</nobr></li>
<li><nobr><a href="#cl-floor">cl:floor</a> - truncate towards negative infinity</nobr></li>
</ul>
<li><nobr><li><nobr><a href="#remainder-and-modulus">Remainder and Modulus</a></nobr></li></nobr></li>
<ul>
<li><nobr><a href="#cl-rem">cl:rem</a></nobr></li>
<li><nobr><a href="#cl-mod">cl:mod</a></nobr></li>
</ul>
<li><nobr>Power and Roots</nobr></li>
<ul>
<li><nobr><a href="#cl-exp">cl:exp</a> - compute 'e' to the power of 'x'</nobr></li>
<li><nobr><a href="#cl-expt">cl:expt</a> - compute 'x' to the power of 'y'</nobr></li>
<li><nobr><a href="#cl-log">cl:log</a></nobr></li>
<li><nobr><a href="#cl-sqrt">cl:sqrt</a></nobr></li>
</ul>
</ol>

<hr>

<h2>Number Types</h2>

<hr>

<p>Nyquist/XLISP only knows two types of numers:</p>

<ul>
<li><nobr><b>fixnum</b>  - integer numbers</nobr></li>
<li><nobr><b>flonum</b>  - floating-point numbers</nobr></li>
</ul>

<p>In Nyquist/XLISP, there are no ratios or complex numbers. Even if the
math functions on this page are modelled after <nobr>Common Lisp</nobr>, no
attempt is made to emulate these numbers.</p>

<a name="integer-limits"></a>

<hr>

<h2>Integer Limits</h2>

<hr>

<pre class="example">
(setq <font color="#AA5500">*most-positive-fixnum*</font>  2147483647)
(setq <font color="#AA5500">*most-negative-fixnum*</font> -2147483648)
</pre>

<p><b>Note:</b> these are the limits for <nobr>32-bit</nobr> machines.</p>

<pre class="example">
(defun <font color="#0000CC">fixnum-bits</font> ()
  (dolist (bits '(15 31 63) nil)
    (let ((fixnum (round (expt 2.0 bits))))
      (and (plusp (1- fixnum))
           (minusp fixnum)
           (return (1+ bits))))))
</pre>

<pre class="example">
(defun <font color="#0000CC">fixnum-limits</font> ()
  (if (dolist (bits '(15 31 63) nil)
        (let* ((negative (round (expt 2.0 bits)))
               (positive (1- negative)))
           (when (and (plusp  positive)
                      (minusp negative))
             (setq most-positive-fixnum positive
                   most-negative-fixnum negative)
             (return t))))
      most-positive-fixnum
      (error <font color="#880000">"fixnum limit not found"</font>)))
</pre>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="print-float"></a>

<hr>

<h2>print-float</h2>

<hr>

<p>The '<nobr>print-float</nobr>' function prints
<nobr>floating-point</nobr> numbers ending in '.0' as
<nobr>floating-point</nobr> numbers and not as integers:</p>

<pre class="example">
(defun <font color="#0000CC">print-float</font> (item)
  (if (not (floatp item))
      item
      (let ((string (format nil <font color="#880000">"~a"</font> item)))
        (if (not (string-search <font color="#880000">"."</font> string))
            (strcat string <font color="#880000">".0"</font>)
            string))))
</pre>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="divide-float"></a>

<hr>

<h2>divide-float</h2>

<hr>

<p>An easy way to force a sequence of integers to be divided as floating
point numbers is to insert the number 1.0 after the first argument in the
list of arguments to the divider function or to explicitely convert the
first argument into a floating point number by using the XLISP <a
href="float.htm">float</a> function:</p>

<pre class="example">
(defun <font color="#0000CC">divide-float</font> (&rest args)
  (if (null args)
      (error <font color="#880000">"too few arguments"</font>)
      (apply #'/ (cons (float (first args)) (rest args)))))
</pre>

<p>See <a href="apply.htm">apply</a>, <a href="cons.htm">cons</a>,
<a href="defun.htm">defun</a>, <a href="error.htm">error</a>,
<a href="first.htm">first</a>, <a href="float.htm">float</a>,
<a href="if.htm">if</a>, <a href="null.htm">null</a>,
<a href="rest.htm">rest</a>,
<a href="lambda-keyword-rest.htm">&amp;rest</a>.</p>

<p>Examples:</p>

<pre class="example">
(divide-float 1)    =&gt; 1.0
(divide-float 1 2)  =&gt; 0.5
</pre>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="rounding-and-truncation"></a>

<hr>

<h2>Rounding and Truncation</h2>

<hr>

<p>The <a href="#cl-round">cl:round</a>,
<a href="#cl-truncate">cl:truncate</a>,
<a href="#cl-ceiling">cl:ceiling</a> and
<a href="#cl-floor">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="#cl-round">cl:round</a>&nbsp;</nobr></td>
  <td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
  <td><nobr>&nbsp; &rArr; &nbsp;</nobr></td>
  <td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
<tr valign="top">
  <td align="right"><nobr>(<a href="#cl-truncate">cl:truncate</a>&nbsp;</nobr></td>
  <td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
  <td><nobr>&nbsp; &rArr; &nbsp;</nobr></td>
  <td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
<tr valign="top">
  <td align="right"><nobr>(<a href="#cl-ceiling">cl:ceiling</a>&nbsp;</nobr></td>
  <td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
  <td><nobr>&nbsp; &rArr; &nbsp;</nobr></td>
  <td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
<tr valign="top">
  <td align="right"><nobr>(<a href="#cl-floor">cl:floor</a>&nbsp;</nobr></td>
  <td><nobr><i>number</i> [<i>divisor</i>])</nobr></td>
  <td><nobr>&nbsp; &rArr; &nbsp;</nobr></td>
  <td><nobr><i>quotient</i>, <i>remainder</i></nobr></td>
</tr>
</tbody></table></p>

<p><nobr>&nbsp;
<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="values.htm#cl-global-multiple-values">cl:*multiple-values*</a> is
set to <a href="../reference/t.htm">&nbsp;T&nbsp;</a> to signal that
<a href="values.htm">Multiple Values</a> are returned.</p>

Examples:

<pre class="example">
(cl:round     3.5)  =&gt;  4  <font color="#008844">; *rslt* = ( 4 -0.5)</font>
(cl:truncate  3.5)  =&gt;  3  <font color="#008844">; *rslt* = ( 3  0.5)</font>
(cl:ceiling   3.5)  =&gt;  4  <font color="#008844">; *rslt* = ( 4 -0.5)</font>
(cl:floor     3.5)  =&gt;  3  <font color="#008844">; *rslt* = ( 3  0.5)</font>

(cl:round    -3.5)  =&gt; -4  <font color="#008844">; *rslt* = (-4  0.5)</font>
(cl:truncate -3.5)  =&gt; -3  <font color="#008844">; *rslt* = (-3 -0.5)</font>
(cl:ceiling  -3.5)  =&gt; -3  <font color="#008844">; *rslt* = (-3 -0.5)</font>
(cl:floor    -3.5)  =&gt; -4  <font color="#008844">; *rslt* = (-4  0.5)</font>
</pre>

Force integer division:

<pre class="example">
(cl:truncate 3.0 2.0)              =&gt; 1
(/ (truncate 3.0) (truncate 2.0))  =&gt; 1
(/ 3 4)                            =&gt; 1
</pre>

<p><div class="box">

<p><b>Implementation Notes</b></p>

<pre class="example">
(defun <font color="#0000CC">name</font> (number &amp;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">&nbsp;/&nbsp;</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="#cl-ceiling">cl:ceiling</a></nobr> and <nobr><a
href="#cl-floor">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>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-round"></a>

<hr>

<h2>cl:round</h2>

<hr>

<p>The '<nobr>cl:round</nobr>' function truncates towards the next
integer:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>round</b> <i>number </i> [<i>divisor</i>])</dt>
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br>
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br>
<table cellpadding="0" cellspacing="0"><tbody>
<tr>
  <td valign="top"><nobr>returns</nobr></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the result of runding the result of <i>number</i> divided by <i>divisor</i></td>
</tr>
<tr>
  <td></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the remainder of the round operation</td>
</tr>
</tbody></table></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:round</font> (number &amp;optional (divisor
                                  (if (integerp number) 1 1.0)
                                  divisor-p))
  (let* ((x (/ (float number) divisor))
         (quotient (cond ((and (not divisor-p) (integerp number)) number)
                         ((= number divisor) 1)
                         ((plusp x) (truncate (+ x 0.5)))
                         ((= (- x 0.5) (truncate (- x 0.5)))
                          (if (minusp x)
                              (1- (truncate x))
                              (truncate x)))
                         (t (truncate (- x 0.5))))))
    (setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor)))
          <font color="#AA5500">cl:*multiple-values*</font> t)
    quotient))
</pre>

<p>The '<nobr>cl:round</nobr>' function computes a quotient that has been rounded to the
nearest mathematical integer. <nobr>If the</nobr> mathematical quotient is
exactly halfway between two integers, [that is, it has the form
<nobr>'integer+1/2']</nobr>, then the quotient has been rounded to the even
[divisible <nobr>by two]</nobr> integer. <nobr>See
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr>
above for more details.</p>

<pre class="example">
(round  3.5)     =&gt;  4
(round -3.5)     =&gt; -3

(cl:round  3.5)  =&gt;  4  <font color="#008844">; *rslt* = ( 4 -0.5)</font>
(cl:round -3.5)  =&gt; -4  <font color="#008844">; *rslt* = (-4  0.5)</font>
</pre>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-truncate"></a>

<hr>

<h2>cl:truncate</h2>

<hr>

<p>The '<nobr>cl:truncate</nobr>' function truncates towards zero:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>truncate</b> <i>number </i> [<i>divisor</i>])</dt>
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br>
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br>
<table cellpadding="0" cellspacing="0"><tbody>
<tr>
  <td valign="top"><nobr>returns</nobr></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the result of truncating the result of <i>number</i> divided by <i>divisor</i></td>
</tr>
<tr>
  <td></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the remainder of the truncate operation</td>
</tr>
</tbody></table></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:truncate</font> (number &amp;optional (divisor (if (integerp number) 1 1.0)))
  (let ((quotient (truncate (/ (float number) divisor))))
    (setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor)))
          <font color="#AA5500">cl:*multiple-values*</font> t)
    quotient))
</pre>

<p>The '<nobr>cl:truncate</nobr>' function computes a quotient that has been
truncated towards zero. That is, the quotient represents the mathematical
integer of the same sign as the mathematical quotient, and that has the
greatest integral magnitude not greater than that of the mathematical
quotient. <nobr>See
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr>
above for more details.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-ceiling"></a>

<hr>

<h2>cl:ceiling</h2>

<hr>

<p>The '<nobr>cl:ceiling</nobr>' function truncates towards positive
infinity:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>ceiling</b> <i>number </i> [<i>divisor</i>])</dt>
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br>
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br>
<table cellpadding="0" cellspacing="0"><tbody>
<tr>
  <td valign="top"><nobr>returns</nobr></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the result of truncating the result of <i>number</i> divided by <i>divisor</i></td>
</tr>
<tr>
  <td></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the remainder of the truncate operation</td>
</tr>
</tbody></table></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:ceiling</font> (number &amp;optional (divisor
                                    (if (integerp number) 1 1.0)
                                    divisor-p))
  (let ((quotient
          (cond ((and (not divisor-p) (integerp number)) number)
                ((= number divisor) 1)
                (t (let ((i-quotient (/ (truncate number) (truncate divisor)))
                         (f-quotient (/ (float number) divisor)))
                     (if (or (= i-quotient f-quotient)  <font color="#008844">; integer result</font>
                             (not (plusp f-quotient)))
                          (truncate f-quotient)
                          (1+ (truncate f-quotient))))))))
    (setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor)))
          <font color="#AA5500">cl:*multiple-values*</font> t)
    quotient))
</pre>

<p>The '<nobr>cl:ceiling</nobr>' function computes a quotient that has been
truncated toward positive infinity. That is, the quotient represents the
smallest mathematical integer that is not smaller than the mathematical
result. <nobr>See
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr>
above for more details.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-floor"></a>

<hr>

<h2>cl:floor</h2>

<hr>

<p>The '<nobr>cl:floor</nobr>' function truncates towards negative infinity:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>floor</b> <i>number </i> [<i>divisor</i>])</dt>
<dd><i>number</i> - an integer or <nobr>floating-point</nobr> number<br>
<i>divisor</i> - an integer or <nobr>floating-point</nobr> number, except zero<br>
<table cellpadding="0" cellspacing="0"><tbody>
<tr>
  <td valign="top"><nobr>returns</nobr></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the result of truncating the result of <i>number</i> divided by <i>divisor</i></td>
</tr>
<tr>
  <td></td>
  <td valign="top"><nobr>&nbsp;-&nbsp;</nobr></td>
  <td width="100%">the remainder of the truncate operation</td>
</tr>
</tbody></table></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:floor</font> (number &amp;optional (divisor
                                  (if (integerp number) 1 1.0)
                                  divisor-p))
  (let ((quotient
          (cond ((and (not divisor-p) (integerp number)) number)
                ((= number divisor) 1)
                (t (let ((i-quotient (/ (truncate number) (truncate divisor)))
                         (f-quotient (/ (float number) divisor)))
                     (if (or (= i-quotient f-quotient)  <font color="#008844">; integer result</font>
                             (not (minusp f-quotient)))
                          (truncate f-quotient)
                          (1- (truncate f-quotient))))))))
    (setq <font color="#AA5500">*rslt*</font> (list quotient (- number (* quotient divisor)))
          <font color="#AA5500">cl:*multiple-values*</font> t)
    quotient))
</pre>

<p>The <nobr>'cl:floor</nobr>' function computes a quotient that has been
truncated toward negative infinity. That is, the quotient represents the
largest mathematical integer that is not larger than the mathematical
quotient. <nobr>See
<a href="#rounding-and-truncation">Rounding and Truncation</a></nobr>
above for more details.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="remainder-and-modulus"></a>

<hr>

<h2>Remainder and Modulus</h2>

<hr>

<p>The <a href="#cl-mod">cl:mod</a> and <a href="#cl-rem">cl:rem</a>
function are generalizations of the modulus and remainder functions.
<nobr>The <a href="#cl-mod">cl:mod</a></nobr> function performs the
<a href="#cl-floor">cl:floor</a> operation on its arguments and returns the
remainder of the <a href="#cl-floor">cl:floor</a> operation.
<nobr>The <a href="#cl-rem">cl:rem</a></nobr> function performs the
<a href="cl-truncate">cl:truncate</a> operation on its arguments and returns
the remainder of the <a href="cl-truncate">cl:truncate</a> operation.
<nobr>The <a href="#cl-mod">cl:mod</a></nobr> and
<a href="#cl-rem">cl:rem</a> functions are the modulus and remainder
functions when the 'number' and 'divisor' arguments both are integers.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-rem"></a>

<hr>

<h2>cl:rem</h2>

<hr>

<p><div class="box">

<dl>
<dt>(cl:<b>rem</b> <i>number divisor</i>)</dt>
<dd><i>number</i> - an integer or floating-point number<br>
<i>divisor</i> - an integer or floating-point number<br>
returns - the remainder of a <a href="cl-truncate">cl:truncate</a> operation</dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:rem</font> (number divisor)
  (if (= (abs number) (abs divisor))
      (if (and (integerp number) (integerp divisor)) 0 0.0)
      (let ((quotient (truncate (/ (float number) divisor))))
        (- number (* quotient divisor)))))
</pre>

<p>The '<nobr>cl:rem</nobr>' function performs the
<a href="cl-truncate">cl:truncate</a> operation on its arguments and returns
the remainder of the <a href="cl-truncate">cl:truncate</a> operation.
<nobr>The result</nobr> is either zero or an integer or
<nobr>floating-point</nobr> number with the same sign as the 'number'
argument. <nobr>If both</nobr> arguments are integer numbers, the
'<nobr>cl:rem</nobr>' function is equal to the mathematical remainder
function.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-mod"></a>

<hr>

<h2>cl:mod</h2>

<hr>

<p><div class="box">

<dl>
<dt>(cl:<b>mod</b> <i>number divisor</i>)</dt>
<dd><i>number</i> - an integer or floating-point number<br>
<i>divisor</i> - an integer or floating-point number<br>
returns - the remainder of a <a href="#cl-floor">cl:floor</a> operation</dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:mod</font> (number divisor)
  (if (= (abs number) (abs divisor))
      (if (and (integerp number) (integerp divisor)) 0 0.0)
      (let* ((i-quotient (/ (truncate number) (truncate divisor)))
             (f-quotient (/ (float number) divisor))
             (quotient (if (or (= i-quotient f-quotient)  <font color="#008844">; integer result</font>
                               (not (minusp f-quotient)))
                           (truncate f-quotient)
                           (1- (truncate f-quotient)))))
        (- number (* quotient divisor)))))
</pre>

<p>The '<nobr>cl:mod</nobr>' function performs the
<a href="#cl-floor">cl:floor</a> operation on its arguments and returns the
remainder of the <a href="#cl-floor">cl:floor</a> operation. <nobr>The
result</nobr> is either zero or an integer or <nobr>floating-point</nobr>
number with the same sign as the 'divisor' argument. <nobr>If both</nobr>
arguments are integer numbers, the '<nobr>cl:rem</nobr>' function is equal
to the mathematical modulus function.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-exp"></a>

<hr>

<h2>cl:exp</h2>

<hr>

<p>The '<nobr>cl:exp</nobr>' function does the same as the Nyquist/XLISP
<a href="../reference/exp.htm">exp</a> function, but it also accepts
integer numbers as argument:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>exp</b> <i>power</i>)</dt>
<dd><i>power</i> - an integer or floating-point number<br>
returns - the result of <nobr>'e' [2.7128]</nobr> to the power of <i>power</i></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:exp</font> (x)
  (exp (float x)))
</pre>

<p>The '<nobr>cl:exp</nobr>' function computes <nobr>'e' [2.7128]</nobr>
raised to the specified 'power'. <nobr>The result</nobr> is always a
<nobr>floating-point</nobr> number.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-expt"></a>

<hr>

<h2>cl:expt</h2>

<hr>

<p>The '<nobr>cl:expt</nobr>' function computes the result of 'x' to the
power <nobr>of 'y'</nobr>:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>expt</b> <i>base power</i>)</dt>
<dd><i>base</i> - the base<br>
<i>power</i> - the exponent<br>
returns - the result of <i>base</i> to the power of <i>power</i></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:expt</font> (x y)
  (let ((power (expt (float x) y)))
    (if (and (integerp x) (integerp y))
        (round power)
        power)))
</pre>

<p>See <a href="../reference/and.htm">and</a>,
<a href="../reference/defun.htm">defun</a>,
<a href="../reference/expt.htm">expt</a>,
<a href="../reference/float.htm">float</a>,
<nobr><a href="../reference/if.htm">&nbsp;if&nbsp;</a></nobr>,
<a href="../reference/integerp.htm">integerp</a>,
<a href="../reference/let.htm">let</a>,
<a href="../reference/power.htm">power</a>,
<a href="../reference/round.htm">round</a>.</p>

<p>The '<nobr>cl:expt</nobr>' function accepts integer and floating point
numbers as arguments. <nobr>If both</nobr> arguments are integer numbers,
the result will be an integer number, <nobr>if one</nobr> or both arguments
are <nobr>floating-point</nobr> numbers, the result will be a
<nobr>floating-point</nobr> number. <nobr>In contrast</nobr> to the
Nyquist/XLISP <a href="../reference/expt.htm">expt</a> function, the
'<nobr>cl:expt</nobr>' function specifies exactly two arguments.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-log"></a>

<hr>

<h2>cl:log</h2>

<hr>

<p>The '<nobr>cl:log</nobr>' function does the same as the Nyquist/XLISP
<a href="../reference/log.htm">log</a> function, but also accepts
integer numbers and has an optional 'base' argument:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>log</b> <i>number</i> [<i>base</i>])</dt>
<dd><i>number</i> - an integer or floating-point number<br>
<i>base</i> - an integer or floating-point number<br>
returns - the the logarithm of <i>number</i> in base <i>base</i></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:log</font> (number &amp;optional base)
  (if base
      (if (zerop base)
          0.0
          (/ (log (float number)) (log (float base))))
      (log (float number))))
</pre>

<p>The '<nobr>cl:log</nobr>' function returns the logarithm of 'number' in
base 'base'. <nobr>If 'base'</nobr> is not supplied its value <nobr>is
'e'</nobr>, the base of the natural logarithms. <nobr>If the</nobr> 'base'
argument is zero, then 'cl:log' returns zero. <nobr>The result</nobr> is
always a <nobr>floating-point</nobr> number.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<a name="cl-sqrt"></a>

<hr>

<h2>cl:sqrt</h2>

<hr>

<p>The '<nobr>cl:sqrt</nobr>' function does the same as the Nyquist/XLISP
<a href="../reference/sqrt.htm">sqrt</a> function, but it also accepts
integer numbers as argument:</p>

<p><div class="box">

<dl>
<dt>(cl:<b>sqrt</b> <i>number</i>)</dt>
<dd><i>number</i> - an integer or floating-point number<br>
returns - the square root of <i>number</i></dd>
</dl>

</div></p>

<pre class="example">
(defun <font color="#0000CC">cl:sqrt</font> (x)
  (sqrt (float x)))
</pre>

<p><nobr>The result</nobr> is always a <nobr>floating-point</nobr>
number.</p>

<p><nobr>&nbsp;&nbsp;<a href="#top">Back to top</a></nobr></p>

<hr>

<a href="../start.htm">Nyquist / XLISP 2.0</a>&nbsp; -&nbsp;
<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>