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

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

Added combinators. Updating arith.

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