1 ;;; flonums.scm --- The R6RS flonums arithmetic library
3 ;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
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.
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.
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
20 (library (rnrs arithmetic flonums (6))
24 fl=? fl<? fl<=? fl>? fl>=?
26 flinteger? flzero? flpositive? flnegative? flodd? fleven? flfinite?
45 flfloor flceiling fltruncate flround
47 flexp fllog flsin flcos fltan flacos flasin flatan
52 make-no-infinities-violation
53 no-infinities-violation?
56 make-no-nans-violation
60 (import (ice-9 optargs)
62 (rnrs arithmetic fixnums (6))
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))))
77 (define (ensure-flonum z)
79 ((zero? (imag-part z)) (real-part z))
82 (define (real->flonum x)
83 (or (real? x) (raise (make-assertion-violation)))
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))
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))
102 (define (flmax fl1 . args)
103 (let ((flargs (cons fl1 args)))
104 (apply assert-flonum flargs)
107 (define (flmin fl1 . args)
108 (let ((flargs (cons fl1 args)))
109 (apply assert-flonum flargs)
113 (apply assert-flonum args)
114 (if (null? args) 0.0 (apply + args)))
117 (apply assert-flonum args)
118 (if (null? args) 1.0 (apply * args)))
120 (define (fl- fl1 . args)
121 (let ((flargs (cons fl1 args)))
122 (apply assert-flonum flargs)
125 (define (fl/ fl1 . args)
126 (let ((flargs (cons fl1 args)))
127 (apply assert-flonum flargs)
130 (define (flabs fl) (assert-flonum fl) (abs fl))
132 (define (fldiv-and-mod fl1 fl2)
133 (assert-iflonum fl1 fl2)
134 (div-and-mod fl1 fl2))
136 (define (fldiv fl1 fl2)
137 (assert-iflonum fl1 fl2)
140 (define (flmod fl1 fl2)
141 (assert-iflonum fl1 fl2)
144 (define (fldiv0-and-mod0 fl1 fl2)
145 (assert-iflonum fl1 fl2)
146 (div0-and-mod0 fl1 fl2))
148 (define (fldiv0 fl1 fl2)
149 (assert-iflonum fl1 fl2)
152 (define (flmod0 fl1 fl2)
153 (assert-iflonum fl1 fl2)
156 (define (flnumerator fl)
161 (else (numerator fl))))
163 (define (fldenominator fl)
168 (else (denominator fl))))
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))
175 (define (flexp fl) (assert-flonum fl) (exp 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))))
184 (assert-flonum fl fl2)
185 (ensure-flonum (/ (log (+ fl 0.0))
186 (log (+ fl2 0.0)))))))
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)))
195 ((fl) (assert-flonum fl) (atan fl))
196 ((fl fl2) (assert-flonum fl fl2) (atan fl fl2))))
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)))
201 (define-condition-type &no-infinities
202 &implementation-restriction
203 make-no-infinities-violation
204 no-infinities-violation?)
206 (define-condition-type &no-nans
207 &implementation-restriction
208 make-no-nans-violation
211 (define (fixnum->flonum fx)
212 (or (fixnum? fx) (raise (make-assertion-violation)))