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))
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))))
76 (define (real->flonum x)
77 (or (real? x) (raise (make-assertion-violation)))
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))
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))
96 (define (flmax fl1 . args)
97 (let ((flargs (cons fl1 args)))
98 (apply assert-flonum flargs)
101 (define (flmin fl1 . args)
102 (let ((flargs (cons fl1 args)))
103 (apply assert-flonum flargs)
107 (apply assert-flonum args)
108 (if (null? args) 0.0 (apply + args)))
111 (apply assert-flonum args)
112 (if (null? args) 1.0 (apply * args)))
114 (define (fl- fl1 . args)
115 (let ((flargs (cons fl1 args)))
116 (apply assert-flonum flargs)
119 (define (fl/ fl1 . args)
120 (let ((flargs (cons fl1 args)))
121 (apply assert-flonum flargs)
124 (define (flabs fl) (assert-flonum fl) (abs fl))
126 (define (fldiv-and-mod fl1 fl2)
127 (assert-iflonum fl1 fl2)
128 (div-and-mod fl1 fl2))
130 (define (fldiv fl1 fl2)
131 (assert-iflonum fl1 fl2)
134 (define (flmod fl1 fl2)
135 (assert-iflonum fl1 fl2)
138 (define (fldiv0-and-mod0 fl1 fl2)
139 (assert-iflonum fl1 fl2)
140 (div0-and-mod0 fl1 fl2))
142 (define (fldiv0 fl1 fl2)
143 (assert-iflonum fl1 fl2)
146 (define (flmod0 fl1 fl2)
147 (assert-iflonum fl1 fl2)
150 (define (flnumerator fl)
155 (else (numerator fl))))
157 (define (fldenominator fl)
162 (else (denominator fl))))
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))
169 (define (flexp fl) (assert-flonum fl) (exp fl))
170 (define* (fllog fl #:optional fl2)
172 (cond ((fl=? fl -inf.0) +nan.0)
173 (fl2 (begin (assert-flonum fl2) (/ (log fl) (log fl2))))
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)
183 (if fl2 (begin (assert-flonum fl2) (atan fl fl2)) (atan fl)))
185 (define (flsqrt fl) (assert-flonum fl) (sqrt fl))
186 (define (flexpt fl1 fl2) (assert-flonum fl1 fl2) (expt fl1 fl2))
188 (define-condition-type &no-infinities
189 &implementation-restriction
190 make-no-infinities-violation
191 no-infinities-violation?)
193 (define-condition-type &no-nans
194 &implementation-restriction
195 make-no-nans-violation
198 (define (fixnum->flonum fx)
199 (or (fixnum? fx) (raise (make-assertion-violation)))