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

Last change on this file since 13603 was 13603, checked in by Kon Lovett, 12 years ago

Save.

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