flonum? returns false for complex number objects.
[bpt/guile.git] / module / rnrs / arithmetic / flonums.scm
1 ;;; flonums.scm --- The R6RS flonums arithmetic library
2
3 ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
4 ;;
5 ;; This library is free software; you can redistribute it and/or
6 ;; modify it under the terms of the GNU Lesser General Public
7 ;; License as published by the Free Software Foundation; either
8 ;; version 3 of the License, or (at your option) any later version.
9 ;;
10 ;; This library is distributed in the hope that it will be useful,
11 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ;; Lesser General Public License for more details.
14 ;;
15 ;; You should have received a copy of the GNU Lesser General Public
16 ;; License along with this library; if not, write to the Free Software
17 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 \f
19
20 (library (rnrs arithmetic flonums (6))
21 (export flonum?
22 real->flonum
23
24 fl=? fl<? fl<=? fl>? fl>=?
25
26 flinteger? flzero? flpositive? flnegative? flodd? fleven? flfinite?
27 flinfinite? flnan?
28
29 flmax flmin
30
31 fl+ fl* fl- fl/
32
33 flabs
34
35 fldiv-and-mod
36 fldiv
37 flmod
38 fldiv0-and-mod0
39 fldiv0
40 flmod0
41
42 flnumerator
43 fldenominator
44
45 flfloor flceiling fltruncate flround
46
47 flexp fllog flsin flcos fltan flacos flasin flatan
48
49 flsqrt flexpt
50
51 &no-infinities
52 make-no-infinities-violation
53 no-infinities-violation?
54
55 &no-nans
56 make-no-nans-violation
57 no-nans-violation?
58
59 fixnum->flonum)
60 (import (ice-9 optargs)
61 (only (guile) inf?)
62 (rnrs arithmetic fixnums (6))
63 (rnrs base (6))
64 (rnrs conditions (6))
65 (rnrs exceptions (6))
66 (rnrs lists (6))
67 (rnrs r5rs (6)))
68
69 (define (flonum? obj) (and (real? obj) (inexact? obj)))
70 (define (assert-flonum . args)
71 (or (for-all flonum? args) (raise (make-assertion-violation))))
72 (define (assert-iflonum . args)
73 (or (for-all (lambda (i) (and (flonum? i) (integer? i))) args)
74 (raise (make-assertion-violation))))
75
76 (define (real->flonum x)
77 (or (real? x) (raise (make-assertion-violation)))
78 (exact->inexact x))
79
80 (define (fl=? . args) (apply assert-flonum args) (apply = args))
81 (define (fl<? . args) (apply assert-flonum args) (apply < args))
82 (define (fl<=? . args) (apply assert-flonum args) (apply <= args))
83 (define (fl>? . args) (apply assert-flonum args) (apply > args))
84 (define (fl>=? . args) (apply assert-flonum args) (apply >= args))
85
86 (define (flinteger? fl) (assert-flonum fl) (integer? fl))
87 (define (flzero? fl) (assert-flonum fl) (zero? fl))
88 (define (flpositive? fl) (assert-flonum fl) (positive? fl))
89 (define (flnegative? fl) (assert-flonum fl) (negative? fl))
90 (define (flodd? ifl) (assert-iflonum ifl) (odd? ifl))
91 (define (fleven? ifl) (assert-iflonum ifl) (even? ifl))
92 (define (flfinite? fl) (assert-flonum fl) (not (inf? fl)))
93 (define (flinfinite? fl) (assert-flonum fl) (inf? fl))
94 (define (flnan? fl) (assert-flonum fl) (nan? fl))
95
96 (define (flmax fl1 . args)
97 (let ((flargs (cons fl1 args)))
98 (apply assert-flonum flargs)
99 (apply max flargs)))
100
101 (define (flmin fl1 . args)
102 (let ((flargs (cons fl1 args)))
103 (apply assert-flonum flargs)
104 (apply min flargs)))
105
106 (define (fl+ . args)
107 (apply assert-flonum args)
108 (if (null? args) 0.0 (apply + args)))
109
110 (define (fl* . args)
111 (apply assert-flonum args)
112 (if (null? args) 1.0 (apply * args)))
113
114 (define (fl- fl1 . args)
115 (let ((flargs (cons fl1 args)))
116 (apply assert-flonum flargs)
117 (apply - flargs)))
118
119 (define (fl/ fl1 . args)
120 (let ((flargs (cons fl1 args)))
121 (apply assert-flonum flargs)
122 (apply / flargs)))
123
124 (define (flabs fl) (assert-flonum fl) (abs fl))
125
126 (define (fldiv-and-mod fl1 fl2)
127 (assert-iflonum fl1 fl2)
128 (div-and-mod fl1 fl2))
129
130 (define (fldiv fl1 fl2)
131 (assert-iflonum fl1 fl2)
132 (div fl1 fl2))
133
134 (define (flmod fl1 fl2)
135 (assert-iflonum fl1 fl2)
136 (mod fl1 fl2))
137
138 (define (fldiv0-and-mod0 fl1 fl2)
139 (assert-iflonum fl1 fl2)
140 (div0-and-mod0 fl1 fl2))
141
142 (define (fldiv0 fl1 fl2)
143 (assert-iflonum fl1 fl2)
144 (div0 fl1 fl2))
145
146 (define (flmod0 fl1 fl2)
147 (assert-iflonum fl1 fl2)
148 (mod0 fl1 fl2))
149
150 (define (flnumerator fl)
151 (assert-flonum fl)
152 (case fl
153 ((+inf.0) +inf.0)
154 ((-inf.0) -inf.0)
155 (else (numerator fl))))
156
157 (define (fldenominator fl)
158 (assert-flonum fl)
159 (case fl
160 ((+inf.0) 1.0)
161 ((-inf.0) 1.0)
162 (else (denominator fl))))
163
164 (define (flfloor fl) (assert-flonum fl) (floor fl))
165 (define (flceiling fl) (assert-flonum fl) (ceiling fl))
166 (define (fltruncate fl) (assert-flonum fl) (truncate fl))
167 (define (flround fl) (assert-flonum fl) (round fl))
168
169 (define (flexp fl) (assert-flonum fl) (exp fl))
170 (define* (fllog fl #:optional fl2)
171 (assert-flonum fl)
172 (cond ((fl=? fl -inf.0) +nan.0)
173 (fl2 (begin (assert-flonum fl2) (/ (log fl) (log fl2))))
174 (else (log fl))))
175
176 (define (flsin fl) (assert-flonum fl) (sin fl))
177 (define (flcos fl) (assert-flonum fl) (cos fl))
178 (define (fltan fl) (assert-flonum fl) (tan fl))
179 (define (flasin fl) (assert-flonum fl) (asin fl))
180 (define (flacos fl) (assert-flonum fl) (acos fl))
181 (define* (flatan fl #:optional fl2)
182 (assert-flonum fl)
183 (if fl2 (begin (assert-flonum fl2) (atan fl fl2)) (atan fl)))
184
185 (define (flsqrt fl) (assert-flonum fl) (sqrt fl))
186 (define (flexpt fl1 fl2) (assert-flonum fl1 fl2) (expt fl1 fl2))
187
188 (define-condition-type &no-infinities
189 &implementation-restriction
190 make-no-infinities-violation
191 no-infinities-violation?)
192
193 (define-condition-type &no-nans
194 &implementation-restriction
195 make-no-nans-violation
196 no-nans-violation?)
197
198 (define (fixnum->flonum fx)
199 (or (fixnum? fx) (raise (make-assertion-violation)))
200 (exact->inexact fx))
201 )