Merge remote-tracking branch 'origin/stable-2.0'
[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 control (6))
65 (rnrs conditions (6))
66 (rnrs exceptions (6))
67 (rnrs lists (6))
68 (rnrs r5rs (6)))
69
70 (define (flonum? obj) (and (real? obj) (inexact? obj)))
71 (define (assert-flonum . args)
72 (or (for-all flonum? args) (raise (make-assertion-violation))))
73 (define (assert-iflonum . args)
74 (or (for-all (lambda (i) (and (flonum? i) (integer? i))) args)
75 (raise (make-assertion-violation))))
76
77 (define (ensure-flonum z)
78 (cond ((real? z) z)
79 ((zero? (imag-part z)) (real-part z))
80 (else +nan.0)))
81
82 (define (real->flonum x)
83 (or (real? x) (raise (make-assertion-violation)))
84 (exact->inexact x))
85
86 (define (fl=? . args) (apply assert-flonum args) (apply = args))
87 (define (fl<? . args) (apply assert-flonum args) (apply < args))
88 (define (fl<=? . args) (apply assert-flonum args) (apply <= args))
89 (define (fl>? . args) (apply assert-flonum args) (apply > args))
90 (define (fl>=? . args) (apply assert-flonum args) (apply >= args))
91
92 (define (flinteger? fl) (assert-flonum fl) (integer? fl))
93 (define (flzero? fl) (assert-flonum fl) (zero? fl))
94 (define (flpositive? fl) (assert-flonum fl) (positive? fl))
95 (define (flnegative? fl) (assert-flonum fl) (negative? fl))
96 (define (flodd? ifl) (assert-iflonum ifl) (odd? ifl))
97 (define (fleven? ifl) (assert-iflonum ifl) (even? ifl))
98 (define (flfinite? fl) (assert-flonum fl) (not (or (inf? fl) (nan? fl))))
99 (define (flinfinite? fl) (assert-flonum fl) (inf? fl))
100 (define (flnan? fl) (assert-flonum fl) (nan? fl))
101
102 (define (flmax fl1 . args)
103 (let ((flargs (cons fl1 args)))
104 (apply assert-flonum flargs)
105 (apply max flargs)))
106
107 (define (flmin fl1 . args)
108 (let ((flargs (cons fl1 args)))
109 (apply assert-flonum flargs)
110 (apply min flargs)))
111
112 (define (fl+ . args)
113 (apply assert-flonum args)
114 (if (null? args) 0.0 (apply + args)))
115
116 (define (fl* . args)
117 (apply assert-flonum args)
118 (if (null? args) 1.0 (apply * args)))
119
120 (define (fl- fl1 . args)
121 (let ((flargs (cons fl1 args)))
122 (apply assert-flonum flargs)
123 (apply - flargs)))
124
125 (define (fl/ fl1 . args)
126 (let ((flargs (cons fl1 args)))
127 (apply assert-flonum flargs)
128 (apply / flargs)))
129
130 (define (flabs fl) (assert-flonum fl) (abs fl))
131
132 (define (fldiv-and-mod fl1 fl2)
133 (assert-iflonum fl1 fl2)
134 (div-and-mod fl1 fl2))
135
136 (define (fldiv fl1 fl2)
137 (assert-iflonum fl1 fl2)
138 (div fl1 fl2))
139
140 (define (flmod fl1 fl2)
141 (assert-iflonum fl1 fl2)
142 (mod fl1 fl2))
143
144 (define (fldiv0-and-mod0 fl1 fl2)
145 (assert-iflonum fl1 fl2)
146 (div0-and-mod0 fl1 fl2))
147
148 (define (fldiv0 fl1 fl2)
149 (assert-iflonum fl1 fl2)
150 (div0 fl1 fl2))
151
152 (define (flmod0 fl1 fl2)
153 (assert-iflonum fl1 fl2)
154 (mod0 fl1 fl2))
155
156 (define (flnumerator fl)
157 (assert-flonum fl)
158 (case fl
159 ((+inf.0) +inf.0)
160 ((-inf.0) -inf.0)
161 (else (numerator fl))))
162
163 (define (fldenominator fl)
164 (assert-flonum fl)
165 (case fl
166 ((+inf.0) 1.0)
167 ((-inf.0) 1.0)
168 (else (denominator fl))))
169
170 (define (flfloor fl) (assert-flonum fl) (floor fl))
171 (define (flceiling fl) (assert-flonum fl) (ceiling fl))
172 (define (fltruncate fl) (assert-flonum fl) (truncate fl))
173 (define (flround fl) (assert-flonum fl) (round fl))
174
175 (define (flexp fl) (assert-flonum fl) (exp fl))
176 (define fllog
177 (case-lambda
178 ((fl)
179 (assert-flonum fl)
180 ;; add 0.0 to fl, to change -0.0 to 0.0,
181 ;; so that (fllog -0.0) will be -inf.0, not -inf.0+pi*i.
182 (ensure-flonum (log (+ fl 0.0))))
183 ((fl fl2)
184 (assert-flonum fl fl2)
185 (ensure-flonum (/ (log (+ fl 0.0))
186 (log (+ fl2 0.0)))))))
187
188 (define (flsin fl) (assert-flonum fl) (sin fl))
189 (define (flcos fl) (assert-flonum fl) (cos fl))
190 (define (fltan fl) (assert-flonum fl) (tan fl))
191 (define (flasin fl) (assert-flonum fl) (ensure-flonum (asin fl)))
192 (define (flacos fl) (assert-flonum fl) (ensure-flonum (acos fl)))
193 (define flatan
194 (case-lambda
195 ((fl) (assert-flonum fl) (atan fl))
196 ((fl fl2) (assert-flonum fl fl2) (atan fl fl2))))
197
198 (define (flsqrt fl) (assert-flonum fl) (ensure-flonum (sqrt fl)))
199 (define (flexpt fl1 fl2) (assert-flonum fl1 fl2) (ensure-flonum (expt fl1 fl2)))
200
201 (define-condition-type &no-infinities
202 &implementation-restriction
203 make-no-infinities-violation
204 no-infinities-violation?)
205
206 (define-condition-type &no-nans
207 &implementation-restriction
208 make-no-nans-violation
209 no-nans-violation?)
210
211 (define (fixnum->flonum fx)
212 (or (fixnum? fx) (raise (make-assertion-violation)))
213 (exact->inexact fx))
214 )