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

Last change on this file since 14119 was 14119, checked in by sjamaan, 11 years ago

Change all user links so they point to /users/NAME instead of just NAME

File size: 15.0 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 FXN FXD) => FIXNUM</procedure>
103
104==== fxdiv
105
106<procedure>(fxdiv FXN FXD) => FIXNUM</procedure>
107
108==== fxdiv-and-mod
109
110<procedure>(fxdiv-and-mod FXN FXD) => FIXNUM</procedure>
111
112==== fxdiv0
113
114<procedure>(fxdiv0 FXN FXD) => FIXNUM</procedure>
115
116==== fxmod0
117
118<procedure>(fxmod0 FXN FXD) => FIXNUM</procedure>
119
120==== fxdiv0-and-mod0
121
122<procedure>(fxdiv0-and-mod0 FXN FXD) => 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 FXN FXD) => FIXNUM</procedure>
247
248==== fxremainder
249
250<procedure>(fxremainder FXN FXD) => 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{{NUMBER}} may only be a {{fixnum}} or {{flonum}}.
350
351==== fixnum->flonum
352
353<procedure>(fixnum->flonum FX) => FLONUM</procedure>
354
355==== fl=?
356
357<procedure>(fl=? FP...) => BOOLEAN</procedure>
358
359==== fl<?
360
361<procedure>(fl<? FP...) => BOOLEAN</procedure>
362
363==== fl>?
364
365<procedure>(fl>? FP...) => BOOLEAN</procedure>
366
367==== fl<=?
368
369<procedure>(fl<=? FP...) => BOOLEAN</procedure>
370
371==== fl>=?
372
373<procedure>(fl>=? FP...) => BOOLEAN</procedure>
374
375==== flinteger?
376
377<procedure>(flinteger? FP) => BOOLEAN</procedure>
378
379==== flzero?
380
381<procedure>(flzero? FP) => BOOLEAN</procedure>
382
383==== flpositive?
384
385<procedure>(flpositive? FP) => BOOLEAN</procedure>
386
387==== flnegative?
388
389<procedure>(flnegative? FP) => BOOLEAN</procedure>
390
391==== flodd?
392
393<procedure>(flodd? FP) => BOOLEAN</procedure>
394
395==== fleven?
396
397<procedure>(fleven? FP) => BOOLEAN</procedure>
398
399==== flfinite?
400
401<procedure>(flfinite? FP) => BOOLEAN</procedure>
402
403==== flinfinite?
404
405<procedure>(flinfinite? FP) => BOOLEAN</procedure>
406
407==== flnan?
408
409<procedure>(flnan? FP) => BOOLEAN</procedure>
410
411==== fl+
412
413<procedure>(fl+ FP...) => FLONUM</procedure>
414
415==== fl*
416
417<procedure>(fl* FP...) => FLONUM</procedure>
418
419==== fl-
420
421<procedure>(fl- FP...) => FLONUM</procedure>
422
423==== fl/
424
425<procedure>(fl/ FP...) => FLONUM</procedure>
426
427==== flmax
428
429<procedure>(flmax FP...) => FLONUM</procedure>
430
431==== flmin
432
433<procedure>(flmin FP...) => FLONUM</procedure>
434
435==== flmax-and-min
436
437<procedure>(flmax-and-min FP...) => FLONUM</procedure>
438
439==== flabs
440
441<procedure>(flabs FP) => FLONUM</procedure>
442
443==== fltruncate
444
445<procedure>(fltruncate FP) => FLONUM</procedure>
446
447==== flfloor
448
449<procedure>(flfloor FP) => FLONUM</procedure>
450
451==== flceiling
452
453<procedure>(flceiling FP) => FLONUM</procedure>
454
455==== flround
456
457<procedure>(flround FP) => FLONUM</procedure>
458
459==== fldiv
460
461<procedure>(fldiv FLN FLD) => FLONUM</procedure>
462
463==== flmod
464
465<procedure>(flmod FLN FLD) => FLONUM</procedure>
466
467==== fldiv-and-mod
468
469<procedure>(fldiv-and-mod FLN FLD) => FLONUM</procedure>
470
471==== fldiv0
472
473<procedure>(fldiv0 FLN FLD) => FLONUM</procedure>
474
475==== flmod0
476
477<procedure>(flmod0 FLN FLD) => FLONUM</procedure>
478
479==== fldiv0-and-mod0
480
481<procedure>(fldiv0-and-mod0 FLN FLD) => FLONUM</procedure>
482
483==== flexp
484
485<procedure>(flexp FP) => FLONUM</procedure>
486
487==== fllog
488
489<procedure>(fllog FP [BASE]) => FLONUM</procedure>
490
491==== flsin
492
493<procedure>(flsin FP) => FLONUM</procedure>
494
495==== flcos
496
497<procedure>(flcos FP) => FLONUM</procedure>
498
499==== fltan
500
501<procedure>(fltan FP) => FLONUM</procedure>
502
503==== flasin
504
505<procedure>(flasin FP) => FLONUM</procedure>
506
507==== flacos
508
509<procedure>(flacos FP) => FLONUM</procedure>
510
511==== flatan
512
513<procedure>(flatan FP [FP2]) => FLONUM</procedure>
514
515==== flsqrt
516
517<procedure>(flsqrt FP) => FLONUM</procedure>
518
519==== flexpt
520
521<procedure>(flexpt FP EXP) => FLONUM</procedure>
522
523==== flnumerator
524
525<procedure>(flnumerator FP) => FLONUM</procedure>
526
527==== fldenominator
528
529<procedure>(fldenominator FP) => FLONUM</procedure>
530
531==== Extras
532
533(All `-fl...' identifiers are reserved.)
534
535==== fl<>?
536
537<procedure>(fl<>? FP1 FP2) => BOOLEAN</procedure>
538
539==== flcompare
540
541<procedure>(flcompare FP1 FP2) => BOOLEAN</procedure>
542
543==== flfraction
544
545<procedure>(flfraction FP) => FLONUM</procedure>
546
547===== flnegate
548
549<procedure>(flnegate FP) => FLONUM</procedure>
550
551===== $fl=?
552
553<macro>($fl=? FP [FP1...]) => BOOLEAN</macro>
554
555===== $fl<?
556
557<macro>($fl<? FP [FP1...]) => BOOLEAN</macro>
558
559===== $fl>?
560
561<macro>($fl>? FP [FP1...]) => BOOLEAN</macro>
562
563===== $fl<=?
564
565<macro>($fl<=? FP [FP1...]) => BOOLEAN</macro>
566
567===== $fl>=?
568
569<macro>($fl>=? FP [FP1...]) => BOOLEAN</macro>
570
571===== $flmax
572
573<macro>($flmax FP [FP1...]) => FIXNUM</macro>
574
575===== $flmin
576
577<macro>($flmin FP [FP1...]) => FIXNUM</macro>
578
579===== $fland
580
581<macro>($fland FP [FP1...]) => FIXNUM</macro>
582
583===== $flior
584
585<macro>($flior FP [FP1...]) => FIXNUM</macro>
586
587===== $flxor
588
589<macro>($flxor FP [FP1...]) => FIXNUM</macro>
590
591===== $fl+
592
593<macro>($fl+ FP [FP1...]) => FIXNUM</macro>
594
595===== $fl-
596
597<macro>($fl- FP [FP1...]) => FIXNUM</macro>
598
599===== $fl*
600
601<macro>($fl* FP [FP1...]) => FIXNUM</macro>
602
603===== $fl/
604
605<macro>($fl/ FP [FP1...]) => FIXNUM</macro>
606
607
608=== Bitwise Arithmetic
609
610ERR5RS bitwise functions.
611
612See [[http://www.r6rs.org/final/html/r6rs-lib/r6rs-lib-Z-H-12.html#node_sec_11.1|Exact bitwise arithmetic]]
613
614==== bitwise-if
615
616<procedure>(bitwise-if MASK TRUE FALSE) => INTEGER</procedure>
617
618==== bitwise-test?
619
620<procedure>(bitwise-test? VALUE1 VALUE2) => BOOLEAN</procedure>
621
622==== bitwise-bit-count
623
624<procedure>(bitwise-bit-count VALUE) => INTEGER</procedure>
625
626==== bitwise-length
627
628<procedure>(bitwise-length VALUE) => INTEGER</procedure>
629
630==== bitwise-first-bit-set
631
632<procedure>(bitwise-first-bit-set VALUE) => INTEGER</procedure>
633
634==== bitwise-bit-set?
635
636<procedure>(bitwise-bit-set? VALUE INDEX) => BOOLEAN</procedure>
637
638==== bitwise-copy-bit
639
640<procedure>(bitwise-copy-bit TO START BIT) => INTEGER</procedure>
641
642==== bitwise-bit-field
643
644<procedure>(bitwise-bit-field VALUE START END) => INTEGER</procedure>
645
646==== bitwise-copy-bit-field
647
648<procedure>(bitwise-copy-bit-field TO START END FROM) => INTEGER</procedure>
649
650==== bitwise-rotate-bit-field
651
652<procedure>(bitwise-rotate-bit-field VALUE START END COUNT) => INTEGER</procedure>
653
654==== bitwise-reverse
655
656<procedure>(bitwise-reverse VALUE COUNT) => INTEGER</procedure>
657
658==== bitwise-reverse-bit-field
659
660<procedure>(bitwise-reverse-bit-field VALUE START END) => INTEGER</procedure>
661
662==== bitwise-list->integer
663
664<procedure>(bitwise-list->integer LIST) => INTEGER</procedure>
665
666==== bitwise-integer->list
667
668<procedure>(bitwise-integer->list VALUE [LENGTH]) => LIST</procedure>
669
670==== bitwise-arithmetic-shift
671
672<procedure>(bitwise-arithmetic-shift VALUE DIRECTIONAL-COUNT) => INTEGER</procedure>
673
674==== bitwise-arithmetic-shift-left
675
676<procedure>(bitwise-arithmetic-shift-left VALUE COUNT) => INTEGER</procedure>
677
678==== bitwise-arithmetic-shift-right
679
680<procedure>(bitwise-arithmetic-shift-right VALUE COUNT) => INTEGER</procedure>
681
682==== Extras
683
684===== pow2log2
685
686<procedure>(pow2log2 VALUE) => INTEGER</procedure>
687
688Returns {{(* 2 (log2 VALUE))}}, but more efficient.
689
690==== bitwise-last-bit-set
691
692<procedure>(bitwise-last-bit-set VALUE) => INTEGER</procedure>
693
694===== bitwise-if-not
695
696<procedure>(bitwise-if-not MASK TRUE FALSE) => INTEGER</procedure>
697
698Merge under not mask.
699
700===== boolean->bit
701
702<procedure>(boolean->bit OBJECT) => INTEGER</procedure>
703
704Returns {{0}} for {{#f}} or {{0}} and {{1}} otherwise.
705
706===== Unchecked variants
707
708These procedures have the same signature as the similarly named ones but
709perform no argument checking.
710
711====== *bitwise-if
712====== *bitwise-if-not
713====== *bitwise-test?
714====== *bitwise-bit-count
715====== *bitwise-length
716====== *bitwise-first-bit-set
717====== *bitwise-last-bit-set
718====== *bitwise-bit-set?
719====== *bitwise-copy-bit
720====== *bitwise-bit-field
721====== *bitwise-copy-bit-field
722====== *bitwise-rotate-bit-field
723====== *bitwise-reverse
724====== *bitwise-reverse-bit-field
725====== *bitwise-reverse-bit-field
726====== *bitwise-list->integer
727====== *bitwise-integer->list
728====== *bitwise-arithmetic-shift
729====== *bitwise-arithmetic-shift-left
730====== *bitwise-arithmetic-shift-right
731====== *pow2log2
732
733
734== Usage
735
736
737=== Fixnum Arithmetic
738
739<enscript language=scheme>
740(require-library err5rs-arithmetic-fixnums)
741...
742(import err5rs-arithmetic)
743</enscript>
744
745or
746
747<enscript language=scheme>
748(require-extension err5rs-arithmetic-fixnums)
749</enscript>
750
751
752=== Flonum Arithmetic
753
754<enscript language=scheme>
755(require-library err5rs-arithmetic-flonums)
756...
757(import err5rs-arithmetic)
758</enscript>
759
760or
761
762<enscript language=scheme>
763(require-extension err5rs-arithmetic-flonums)
764</enscript>
765
766
767=== Bitwise Arithmetic
768
769<enscript language=scheme>
770(require-library err5rs-arithmetic-bitwise)
771...
772(import err5rs-arithmetic)
773</enscript>
774
775or
776
777<enscript language=scheme>
778(require-extension err5rs-arithmetic-bitwise)
779</enscript>
780
781
782== Examples
783
784
785== Notes
786
787
788== Requirements
789
790
791== Bugs and Limitations
792
793* No full-numeric-tower arithmetic routines.
794
795* No full-numeric-tower suuport.
796
797
798== Author
799
800[[/users/kon lovett|kon lovett]]
801
802
803== Version history
804
805; 1.0.0 : Chicken 4 release.
806
807
808== License
809
810Copyright (C) 2009 Kon Lovett.  All rights reserved.
811
812Permission is hereby granted, free of charge, to any person obtaining a
813copy of this software and associated documentation files (the Software),
814to deal in the Software without restriction, including without limitation
815the rights to use, copy, modify, merge, publish, distribute, sublicense,
816and/or sell copies of the Software, and to permit persons to whom the
817Software is furnished to do so, subject to the following conditions:
818
819The above copyright notice and this permission notice shall be included
820in all copies or substantial portions of the Software.
821
822THE SOFTWARE IS PROVIDED ASIS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
823IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
824FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
825THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
826OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
827ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
828OTHER DEALINGS IN THE SOFTWARE.
Note: See TracBrowser for help on using the repository browser.