Debian Codegraph @

lib/srfi/94.scm

raw
1 ;;;
2 ;;; SRFI-94 Type-Restricted Numerical Functions
3 ;;;
4
5 ;; Most of the procedures in SRFI-94 are built-in.
6
7 (define-module srfi.94
8 (export
9 ;; built-in
10 real-exp real-ln real-sin real-cos real-tan
11 real-asin real-acos real-atan atan
12 real-sqrt integer-sqrt integer-expt
13 make-rectangular make-polar
14
15 ;; SRFI-94 specific
16 real-log integer-sqrt integer-log quo rem mod ln abs
17 quotient remainder modulo
18 ))
19 (select-module srfi.94)
20
21 ;; SRFI-94's real-log takes base first, while R6RS log takes optional base
22 ;; in the second argument. To avoid confusion, Gauche does not provide
23 ;; built-in real-log.
24 (define (real-log base x)
25 (/ (real-ln x) (real-ln base)))
26
27 (define integer-sqrt exact-integer-sqrt)
28
29 ;; Returns max k such that base^k <= n
30 (define (integer-log base n)
31 ;; WRITEME
32 )
33
34 (define (ln z) (log z))
35
36 (define (abs x)
37 (assume (real? x))
38 ((with-module gauche abs) x))
39
40 ;; quo and rem is a kind of middle-ground between R6RS div/mod
41 ;; and R7RS truncate-quotient/trunctate-remainder.
42 ;; They allow non-integer arguments like the former, but round
43 ;; towards zero like the latter.
44
45 (define (quo n d)
46 (if (or (negative? n) (negative? d))
47 (- (div (- n) d))
48 (div n d)))
49
50 (define (rem n d)
51 (- n (* d (quo n d))))
52
53 (define (mod n d)
54 (let1 r (rem n d)
55 (if (and (negative? r) (negative? n))
56 (- r)
57 r)))
58
59 ;; SRFI-94's quotient, remainder and modulo require exact integers,
60 ;; while Gauche's built-ins allow inexact integers.
61
62 (define (quotient n1 n2)
63 (assume (exact-integer? n1))
64 (assume (exact-integer? n2))
65 ((with-module gauche quotient) n1 n2))
66
67 (define (remainder n1 n2)
68 (assume (exact-integer? n1))
69 (assume (exact-integer? n2))
70 ((with-module gauche remainder) n1 n2))
71
72 (define (modulo n1 n2)
73 (assume (exact-integer? n1))
74 (assume (exact-integer? n2))
75 ((with-module gauche remainder) n1 n2))