source: project/wiki/eggref/4/err5rs-arithmetic @ 13793

Last change on this file since 13793 was 13793, checked in by Kon Lovett, 11 years ago

Added & renamed routines.

File size: 14.4 KB
Line 
1[[tags: egg]]
2
3== err5rs-arithmetic
4
5[[toc:]]
6
7
8== Documentation
9
10ERR5RS arithmetic routines.
11
12See [[http://scheme-punks.cyber-rush.org/wiki/index.php?title=ERR5RS:SRFIs_and_Standard_Libraries|ERR5RS:SRFIs_and_Standard_Libraries]]
13
14Note that while this egg is named ''err5rs-arithmetic'' the actual extension is
15''err5rs-arithmetic-fixnums'', ''err5rs-arithmetic-bitwise'' and ''err5rs-arithmetic-flonums''.
16
17
18=== Fixnum Arithmetic
19
20ERR5RS fixnum functions.
21
22See [[http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-12.html#node_sec_11.2|Fixnums]]
23
24==== fixnum-width
25
26<procedure>(fixnum-width) => FIXNUM</procedure>
27
28==== least-fixnum
29
30<procedure>(least-fixnum) => FIXNUM</procedure>
31
32==== greatest-fixnum
33
34<procedure>(greatest-fixnum) => FIXNUM</procedure>
35
36==== fx=?
37
38<procedure>(fx=? FX...) => BOOLEAN</procedure>
39
40==== fx<?
41
42<procedure>(fx<? FX...) => BOOLEAN</procedure>
43
44==== fx>?
45
46<procedure>(fx>? FX...) => BOOLEAN</procedure>
47
48==== fx<=?
49
50<procedure>(fx<=? FX...) => BOOLEAN</procedure>
51
52==== fx>=?
53
54<procedure>(fx>=? FX...) => BOOLEAN</procedure>
55
56==== fxzero?
57
58<procedure>(fxzero? FX) => BOOLEAN</procedure>
59
60==== fxpositive?
61
62<procedure>(fxpositive? FX) => BOOLEAN</procedure>
63
64==== fxnegative?
65
66<procedure>(fxnegative? FX) => BOOLEAN</procedure>
67
68==== fxodd?
69
70<procedure>(fxodd? FX) => BOOLEAN</procedure>
71
72==== fxeven?
73
74<procedure>(fxeven? FX) => BOOLEAN</procedure>
75
76==== fxmax
77
78<procedure>(fxmax FX...) => FIXNUM</procedure>
79
80==== fxmin
81
82<procedure>(fxmin FX...) => FIXNUM</procedure>
83
84==== fxmax-and-min
85
86<procedure>(fxmax-and-min FX...) => FIXNUM</procedure>
87
88==== fxmodulo
89
90<procedure>(fxmodulo FX-N FX-D) => FIXNUM</procedure>
91
92==== fxdiv
93
94<procedure>(fxdiv FX-N FX-D) => FIXNUM</procedure>
95
96==== fxdiv-and-mod
97
98<procedure>(fxdiv-and-mod FX-N FX-D) => FIXNUM</procedure>
99
100==== fxdiv0
101
102<procedure>(fxdiv0 FX-N FX-D) => FIXNUM</procedure>
103
104==== fxmod0
105
106<procedure>(fxmod0 FX-N FX-D) => FIXNUM</procedure>
107
108==== fxdiv0-and-mod0
109
110<procedure>(fxdiv0-and-mod0 FX-N FX-D) => FIXNUM</procedure>
111
112==== fx*/carry
113
114<procedure>(fx*/carry FX1 FX2 FX3) => FIXNUM</procedure>
115
116==== fx+/carry
117
118<procedure>(fx+/carry FX1 FX2 FX3) => FIXNUM</procedure>
119
120==== fx-/carry
121
122<procedure>(fx-/carry FX1 FX2 FX3) => FIXNUM</procedure>
123
124==== fxarithmetic-shift
125
126<procedure>(fxarithmetic-shift FX DIRECTIONAL-AMOUNT) => FIXNUM</procedure>
127
128==== fxarithmetic-shift-left
129
130<procedure>(fxarithmetic-shift-left FX AMOUNT) => FIXNUM</procedure>
131
132==== fxarithmetic-shift-right
133
134<procedure>(fxarithmetic-shift-right FX AMOUNT) => FIXNUM</procedure>
135
136==== fx+
137
138<procedure>(fx+ FX FX2) => FIXNUM</procedure>
139
140==== fx-
141
142<procedure>(fx- FX [FX2]) => FIXNUM</procedure>
143
144==== fx*
145
146<procedure>(fx* FX FX2) => FIXNUM</procedure>
147
148==== fxand
149
150<procedure>(fxand FX...) => FIXNUM</procedure>
151
152==== fxior
153
154<procedure>(fxior FX...) => FIXNUM</procedure>
155
156==== fxxor
157
158<procedure>(fxxor FX...) => FIXNUM</procedure>
159
160==== fxif
161
162<procedure>(fxif MASK TRUE FALSE) => FIXNUM</procedure>
163
164==== fxbit-count
165
166<procedure>(fxbit-count FX) => FIXNUM</procedure>
167
168==== fxlength
169
170<procedure>(fxlength FX) => FIXNUM</procedure>
171
172==== fxfirst-bit-set
173
174<procedure>(fxfirst-bit-set FX) => FIXNUM</procedure>
175
176==== fxbit-set?
177
178<procedure>(fxbit-set? FX INDEX) => BOOLEAN</procedure>
179
180==== fxcopy-bit
181
182<procedure>(fxcopy-bit FX INDEX BIT) => FIXNUM</procedure>
183
184==== fxbit-field
185
186<procedure>(fxbit-field FX START END) => FIXNUM</procedure>
187
188==== fxcopy-bit-field
189
190<procedure>(fxcopy-bit-field FX-TO START END FX-FROM) => FIXNUM</procedure>
191
192==== fxrotate-bit-field
193
194<procedure>(fxrotate-bit-field FX START END COUNT) => FIXNUM</procedure>
195
196==== fxreverse-bit-field
197
198<procedure>(fxreverse-bit-field FX START END) => FIXNUM</procedure>
199
200==== Extras
201
202(All `$fx...' identifiers are reserved.)
203
204==== fx<>?
205
206<procedure>(fx<>? FX1 FX2) => BOOLEAN</procedure>
207
208==== fxcompare
209
210<procedure>(fxcompare FX1 FX2) => BOOLEAN</procedure>
211
212==== fxabs
213
214<procedure>(fxabs FX) => FIXNUM</procedure>
215
216===== fxnegate
217
218<procedure>(fxnegate FX) => FIXNUM</procedure>
219
220==== fxadd1
221
222<procedure>(fxadd1 FX) => FIXNUM</procedure>
223
224==== fxsub1
225
226<procedure>(fxsub1 FX) => FIXNUM</procedure>
227
228===== fx/
229
230<procedure>(fx/ FX1 FX2) => FIXNUM</procedure>
231
232==== fxquotient
233
234<procedure>(fxquotient FX-N FX-D) => FIXNUM</procedure>
235
236==== fxremainder
237
238<procedure>(fxremainder FX-N FX-D) => FIXNUM</procedure>
239
240===== fxpow2log2
241
242<procedure>(fxpow2log2 FX) => FIXNUM</procedure>
243
244===== fxif-not
245
246<procedure>(fxif-not MASK TRUE FALSE) => FIXNUM</procedure>
247
248==== fxlast-bit-set
249
250<procedure>(fxlast-bit-set FX) => FIXNUM</procedure>
251
252===== fixnum->string
253
254<procedure>(fixnum->string FX [RADIX]) => STRING</procedure>
255
256===== *fx=?
257
258<macro>(*fx=? FX [FX1...]) => BOOLEAN</macro>
259
260===== *fx<?
261
262<macro>(*fx<? FX [FX1...]) => BOOLEAN</macro>
263
264===== *fx>?
265
266<macro>(*fx>? FX [FX1...]) => BOOLEAN</macro>
267
268===== *fx<=?
269
270<macro>(*fx<=? FX [FX1...]) => BOOLEAN</macro>
271
272===== *fx>=?
273
274<macro>(*fx>=? FX [FX1...]) => BOOLEAN</macro>
275
276===== *fxmax
277
278<macro>(*fxmax FX [FX1...]) => FIXNUM</macro>
279
280===== *fxmin
281
282<macro>(*fxmin FX [FX1...]) => FIXNUM</macro>
283
284===== *fxand
285
286<macro>(*fxand FX [FX1...]) => FIXNUM</macro>
287
288===== *fxior
289
290<macro>(*fxior FX [FX1...]) => FIXNUM</macro>
291
292===== *fxxor
293
294<macro>(*fxxor FX [FX1...]) => FIXNUM</macro>
295
296===== *fx+
297
298<macro>(*fx+ FX [FX1...]) => FIXNUM</macro>
299
300===== *fx-
301
302<macro>(*fx- FX [FX1...]) => FIXNUM</macro>
303
304===== *fx*
305
306<macro>(*fx* FX [FX1...]) => FIXNUM</macro>
307
308===== *fx/
309
310<macro>(*fx/ FX [FX1...]) => FIXNUM</macro>
311
312
313=== Flonum Arithmetic
314
315ERR5RS flonum functions.
316
317See [[http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-12.html#node_sec_11.3|Flonums]]
318
319==== real->flonum
320
321<procedure>(real->flonum NUMBER) => FLONUM</procedure>
322
323==== fixnum->flonum
324
325<procedure>(fixnum->flonum FX) => FLONUM</procedure>
326
327==== fl=?
328
329<procedure>(fl=? FP...) => BOOLEAN</procedure>
330
331==== fl<?
332
333<procedure>(fl<? FP...) => BOOLEAN</procedure>
334
335==== fl>?
336
337<procedure>(fl>? FP...) => BOOLEAN</procedure>
338
339==== fl<=?
340
341<procedure>(fl<=? FP...) => BOOLEAN</procedure>
342
343==== fl>=?
344
345<procedure>(fl>=? FP...) => BOOLEAN</procedure>
346
347==== flinteger?
348
349<procedure>(flinteger? FP) => BOOLEAN</procedure>
350
351==== flzero?
352
353<procedure>(flzero? FP) => BOOLEAN</procedure>
354
355==== flpositive?
356
357<procedure>(flpositive? FP) => BOOLEAN</procedure>
358
359==== flnegative?
360
361<procedure>(flnegative? FP) => BOOLEAN</procedure>
362
363==== flodd?
364
365<procedure>(flodd? FP) => BOOLEAN</procedure>
366
367==== fleven?
368
369<procedure>(fleven? FP) => BOOLEAN</procedure>
370
371==== flfinite?
372
373<procedure>(flfinite? FP) => BOOLEAN</procedure>
374
375==== flinfinite?
376
377<procedure>(flinfinite? FP) => BOOLEAN</procedure>
378
379==== flnan?
380
381<procedure>(flnan? FP) => BOOLEAN</procedure>
382
383==== fl+
384
385<procedure>(fl+ FP...) => FLONUM</procedure>
386
387==== fl*
388
389<procedure>(fl* FP...) => FLONUM</procedure>
390
391==== fl-
392
393<procedure>(fl- FP...) => FLONUM</procedure>
394
395==== fl/
396
397<procedure>(fl/ FP...) => FLONUM</procedure>
398
399==== flmax
400
401<procedure>(flmax FP...) => FLONUM</procedure>
402
403==== flmin
404
405<procedure>(flmin FP...) => FLONUM</procedure>
406
407==== flmax-and-min
408
409<procedure>(flmax-and-min FP...) => FLONUM</procedure>
410
411==== flabs
412
413<procedure>(flabs FP) => FLONUM</procedure>
414
415==== fltruncate
416
417<procedure>(fltruncate FP) => FLONUM</procedure>
418
419==== flfloor
420
421<procedure>(flfloor FP) => FLONUM</procedure>
422
423==== flceiling
424
425<procedure>(flceiling FP) => FLONUM</procedure>
426
427==== flround
428
429<procedure>(flround FP) => FLONUM</procedure>
430
431==== fldiv
432
433<procedure>(fldiv FL-N FL-D) => FLONUM</procedure>
434
435==== flmod
436
437<procedure>(flmod FL-N FL-D) => FLONUM</procedure>
438
439==== fldiv-and-mod
440
441<procedure>(fldiv-and-mod FL-N FL-D) => FLONUM</procedure>
442
443==== fldiv0
444
445<procedure>(fldiv0 FL-N FL-D) => FLONUM</procedure>
446
447==== flmod0
448
449<procedure>(flmod0 FL-N FL-D) => FLONUM</procedure>
450
451==== fldiv0-and-mod0
452
453<procedure>(fldiv0-and-mod0 FL-N FL-D) => FLONUM</procedure>
454
455==== flexp
456
457<procedure>(flexp FP) => FLONUM</procedure>
458
459==== fllog
460
461<procedure>(fllog FP [BASE]) => FLONUM</procedure>
462
463==== flsin
464
465<procedure>(flsin FP) => FLONUM</procedure>
466
467==== flcos
468
469<procedure>(flcos FP) => FLONUM</procedure>
470
471==== fltan
472
473<procedure>(fltan FP) => FLONUM</procedure>
474
475==== flasin
476
477<procedure>(flasin FP) => FLONUM</procedure>
478
479==== flacos
480
481<procedure>(flacos FP) => FLONUM</procedure>
482
483==== flatan
484
485<procedure>(flatan FP [FP2]) => FLONUM</procedure>
486
487==== flsqrt
488
489<procedure>(flsqrt FP) => FLONUM</procedure>
490
491==== flexpt
492
493<procedure>(flexpt FP EXP) => FLONUM</procedure>
494
495==== flnumerator
496
497<procedure>(flnumerator FP) => FLONUM</procedure>
498
499==== fldenominator
500
501<procedure>(fldenominator FP) => FLONUM</procedure>
502
503==== Extras
504
505(All `$fl...' identifiers are reserved.)
506
507==== fl<>?
508
509<procedure>(fl<>? FP1 FP2) => BOOLEAN</procedure>
510
511==== flcompare
512
513<procedure>(flcompare FP1 FP2) => BOOLEAN</procedure>
514
515==== flfraction
516
517<procedure>(flfraction FP) => FLONUM</procedure>
518
519===== flnegate
520
521<procedure>(flnegate FP) => FLONUM</procedure>
522
523===== *fl=?
524
525<macro>(*fl=? FP [FP1...]) => BOOLEAN</macro>
526
527===== *fl<?
528
529<macro>(*fl<? FP [FP1...]) => BOOLEAN</macro>
530
531===== *fl>?
532
533<macro>(*fl>? FP [FP1...]) => BOOLEAN</macro>
534
535===== *fl<=?
536
537<macro>(*fl<=? FP [FP1...]) => BOOLEAN</macro>
538
539===== *fl>=?
540
541<macro>(*fl>=? FP [FP1...]) => BOOLEAN</macro>
542
543===== *flmax
544
545<macro>(*flmax FP [FP1...]) => FIXNUM</macro>
546
547===== *flmin
548
549<macro>(*flmin FP [FP1...]) => FIXNUM</macro>
550
551===== *fland
552
553<macro>(*fland FP [FP1...]) => FIXNUM</macro>
554
555===== *flior
556
557<macro>(*flior FP [FP1...]) => FIXNUM</macro>
558
559===== *flxor
560
561<macro>(*flxor FP [FP1...]) => FIXNUM</macro>
562
563===== *fl+
564
565<macro>(*fl+ FP [FP1...]) => FIXNUM</macro>
566
567===== *fl-
568
569<macro>(*fl- FP [FP1...]) => FIXNUM</macro>
570
571===== *fl*
572
573<macro>(*fl* FP [FP1...]) => FIXNUM</macro>
574
575===== *fl/
576
577<macro>(*fl/ FP [FP1...]) => FIXNUM</macro>
578
579
580=== Bitwise Arithmetic
581
582ERR5RS bitwise functions.
583
584See [[http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-12.html#node_sec_11.1|Exact bitwise arithmetic]]
585
586==== bitwise-if
587
588<procedure>(bitwise-if MASK TRUE FALSE) => INTEGER</procedure>
589
590==== bitwise-test?
591
592<procedure>(bitwise-test? VALUE1 VALUE2) => BOOLEAN</procedure>
593
594==== bitwise-bit-count
595
596<procedure>(bitwise-bit-count VALUE) => INTEGER</procedure>
597
598==== bitwise-length
599
600<procedure>(bitwise-length VALUE) => INTEGER</procedure>
601
602==== bitwise-first-bit-set
603
604<procedure>(bitwise-first-bit-set VALUE) => INTEGER</procedure>
605
606==== bitwise-bit-set?
607
608<procedure>(bitwise-bit-set? VALUE INDEX) => BOOLEAN</procedure>
609
610==== bitwise-copy-bit
611
612<procedure>(bitwise-copy-bit TO START BIT) => INTEGER</procedure>
613
614==== bitwise-bit-field
615
616<procedure>(bitwise-bit-field VALUE START END) => INTEGER</procedure>
617
618==== bitwise-copy-bit-field
619
620<procedure>(bitwise-copy-bit-field TO START END FROM) => INTEGER</procedure>
621
622==== bitwise-rotate-bit-field
623
624<procedure>(bitwise-rotate-bit-field VALUE START END COUNT) => INTEGER</procedure>
625
626==== bitwise-reverse
627
628<procedure>(bitwise-reverse VALUE COUNT) => INTEGER</procedure>
629
630==== bitwise-reverse-bit-field
631
632<procedure>(bitwise-reverse-bit-field VALUE START END) => INTEGER</procedure>
633
634==== bitwise-list->integer
635
636<procedure>(bitwise-list->integer LIST) => INTEGER</procedure>
637
638==== bitwise-integer->list
639
640<procedure>(bitwise-integer->list VALUE [LENGTH]) => LIST</procedure>
641
642==== bitwise-arithmetic-shift
643
644<procedure>(bitwise-arithmetic-shift VALUE DIRECTIONAL-COUNT) => INTEGER</procedure>
645
646==== bitwise-arithmetic-shift-left
647
648<procedure>(bitwise-arithmetic-shift-left VALUE COUNT) => INTEGER</procedure>
649
650==== bitwise-arithmetic-shift-right
651
652<procedure>(bitwise-arithmetic-shift-right VALUE COUNT) => INTEGER</procedure>
653
654==== Extras
655
656===== pow2log2
657
658<procedure>(pow2log2 VALUE) => INTEGER</procedure>
659
660Returns {{(* 2 (log2 VALUE))}}, but more efficient.
661
662==== bitwise-last-bit-set
663
664<procedure>(bitwise-last-bit-set VALUE) => INTEGER</procedure>
665
666===== bitwise-if-not
667
668<procedure>(bitwise-if-not MASK TRUE FALSE) => INTEGER</procedure>
669
670Merge under not mask.
671
672===== boolean->bit
673
674<procedure>(boolean->bit OBJECT) => INTEGER</procedure>
675
676Returns {{0}} for {{#f}} or {{0}} and {{1}} otherwise.
677
678===== Unchecked variants
679
680These procedures have the same signature as the similarly named ones but
681perform no argument checking.
682
683====== *bitwise-if
684====== *bitwise-if-not
685====== *bitwise-test?
686====== *bitwise-bit-count
687====== *bitwise-length
688====== *bitwise-first-bit-set
689====== *bitwise-last-bit-set
690====== *bitwise-bit-set?
691====== *bitwise-copy-bit
692====== *bitwise-bit-field
693====== *bitwise-copy-bit-field
694====== *bitwise-rotate-bit-field
695====== *bitwise-reverse
696====== *bitwise-reverse-bit-field
697====== *bitwise-reverse-bit-field
698====== *bitwise-list->integer
699====== *bitwise-integer->list
700====== *bitwise-arithmetic-shift
701====== *bitwise-arithmetic-shift-left
702====== *bitwise-arithmetic-shift-right
703====== *pow2log2
704
705
706== Usage
707
708
709=== Fixnum Arithmetic
710
711<enscript language=scheme>
712(require-library err5rs-arithmetic-fixnums)
713...
714(import err5rs-arithmetic)
715</enscript>
716
717or
718
719<enscript language=scheme>
720(require-extension err5rs-arithmetic-fixnums)
721</enscript>
722
723
724=== Flonum Arithmetic
725
726<enscript language=scheme>
727(require-library err5rs-arithmetic-flonums)
728...
729(import err5rs-arithmetic)
730</enscript>
731
732or
733
734<enscript language=scheme>
735(require-extension err5rs-arithmetic-flonums)
736</enscript>
737
738
739=== Bitwise Arithmetic
740
741<enscript language=scheme>
742(require-library err5rs-arithmetic-bitwise)
743...
744(import err5rs-arithmetic)
745</enscript>
746
747or
748
749<enscript language=scheme>
750(require-extension err5rs-arithmetic-bitwise)
751</enscript>
752
753
754== Examples
755
756
757== Notes
758
759
760== Requirements
761
762
763== Bugs and Limitations
764
765* No ERR5RS full-numeric-tower arithmetic routines.
766
767* No full-numeric-tower suuport for {{real->flonum}}.
768
769
770== Author
771
772[[kon lovett]]
773
774
775== Version history
776
777; 1.0.0 : Chicken 4 release.
778
779
780== License
781
782Copyright (C) 2009 Kon Lovett.  All rights reserved.
783
784Permission is hereby granted, free of charge, to any person obtaining a
785copy of this software and associated documentation files (the Software),
786to deal in the Software without restriction, including without limitation
787the rights to use, copy, modify, merge, publish, distribute, sublicense,
788and/or sell copies of the Software, and to permit persons to whom the
789Software is furnished to do so, subject to the following conditions:
790
791The above copyright notice and this permission notice shall be included
792in all copies or substantial portions of the Software.
793
794THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
795IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
796FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
797THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
798OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
799ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
800OTHER DEALINGS IN THE SOFTWARE.
Note: See TracBrowser for help on using the repository browser.