mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-06-04 11:10:27 +02:00
* libguile/numbers.c (scm_finite_p): Add new predicate `finite?' from R6RS to guile core, which returns #t if and only if its argument is neither infinite nor a NaN. Note that this is not the same as (not (inf? x)) or (not (infinite? x)), since NaNs are neither finite nor infinite. * test-suite/tests/numbers.test: Add test cases for `finite?'. * module/rnrs/base.scm: Import `inf?' as `infinite?' instead of reimplementing it. Previously, the R6RS implementation of `infinite?' did not detect non-real complex infinities, nor did it throw exceptions for non-numbers. (Note that NaNs _are_ considered numbers by scheme, despite their name). Import `finite?' instead of reimplementing it. Previously, the R6RS implementation of `finite?' returned #t for both NaNs and non-real complex infinities, in violation of R6RS. * NEWS: Add NEWS entries, and reorganize existing numerics-related entries together under one subheading. * doc/ref/api-data.texi (Real and Rational Numbers): Add docs for `finite?' and scm_finite_p.
172 lines
6 KiB
Scheme
172 lines
6 KiB
Scheme
;;; base.scm --- The R6RS base library
|
||
|
||
;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
|
||
;;
|
||
;; This library is free software; you can redistribute it and/or
|
||
;; modify it under the terms of the GNU Lesser General Public
|
||
;; License as published by the Free Software Foundation; either
|
||
;; version 3 of the License, or (at your option) any later version.
|
||
;;
|
||
;; This library is distributed in the hope that it will be useful,
|
||
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||
;; Lesser General Public License for more details.
|
||
;;
|
||
;; You should have received a copy of the GNU Lesser General Public
|
||
;; License along with this library; if not, write to the Free Software
|
||
;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||
|
||
|
||
(library (rnrs base (6))
|
||
(export boolean? symbol? char? vector? null? pair? number? string? procedure?
|
||
|
||
define define-syntax syntax-rules lambda let let* let-values
|
||
let*-values letrec letrec* begin
|
||
|
||
quote lambda if set! cond case
|
||
|
||
or and not
|
||
|
||
eqv? equal? eq?
|
||
|
||
+ - * / max min abs numerator denominator gcd lcm floor ceiling
|
||
truncate round rationalize real-part imag-part make-rectangular angle
|
||
div mod div-and-mod div0 mod0 div0-and-mod0
|
||
|
||
expt exact-integer-sqrt sqrt exp log sin cos tan asin acos atan
|
||
make-polar magnitude angle
|
||
|
||
complex? real? rational? integer? exact? inexact? real-valued?
|
||
rational-valued? integer-valued? zero? positive? negative? odd? even?
|
||
nan? finite? infinite?
|
||
|
||
exact inexact = < > <= >=
|
||
|
||
number->string string->number
|
||
|
||
boolean=?
|
||
|
||
cons car cdr caar cadr cdar cddr caaar caadr cadar cdaar caddr cdadr
|
||
cddar cdddr caaaar caaadr caadar cadaar cdaaar cddaar cdadar cdaadr
|
||
cadadr caaddr caddar cadddr cdaddr cddadr cdddar cddddr
|
||
|
||
list? list length append reverse list-tail list-ref map for-each
|
||
|
||
symbol->string string->symbol symbol=?
|
||
|
||
char->integer integer->char char=? char<? char>? char<=? char>=?
|
||
|
||
make-string string string-length string-ref string=? string<? string>?
|
||
string<=? string>=? substring string-append string->list list->string
|
||
string-for-each string-copy
|
||
|
||
vector? make-vector vector vector-length vector-ref vector-set!
|
||
vector->list list->vector vector-fill! vector-map vector-for-each
|
||
|
||
error assertion-violation assert
|
||
|
||
call-with-current-continuation call/cc call-with-values dynamic-wind
|
||
values apply
|
||
|
||
quasiquote unquote unquote-splicing
|
||
|
||
let-syntax letrec-syntax
|
||
|
||
syntax-rules identifier-syntax)
|
||
(import (rename (except (guile) error raise)
|
||
(quotient div)
|
||
(modulo mod)
|
||
(inf? infinite?)
|
||
(exact->inexact inexact)
|
||
(inexact->exact exact))
|
||
(srfi srfi-11))
|
||
|
||
(define (boolean=? . bools)
|
||
(define (boolean=?-internal lst last)
|
||
(or (null? lst)
|
||
(let ((bool (car lst)))
|
||
(and (eqv? bool last) (boolean=?-internal (cdr lst) bool)))))
|
||
(or (null? bools)
|
||
(let ((bool (car bools)))
|
||
(and (boolean? bool) (boolean=?-internal (cdr bools) bool)))))
|
||
|
||
(define (symbol=? . syms)
|
||
(define (symbol=?-internal lst last)
|
||
(or (null? lst)
|
||
(let ((sym (car lst)))
|
||
(and (eq? sym last) (symbol=?-internal (cdr lst) sym)))))
|
||
(or (null? syms)
|
||
(let ((sym (car syms)))
|
||
(and (symbol? sym) (symbol=?-internal (cdr syms) sym)))))
|
||
|
||
(define (exact-integer-sqrt x)
|
||
(let* ((s (exact (floor (sqrt x)))) (e (- x (* s s)))) (values s e)))
|
||
|
||
;; These definitions should be revisited, since the behavior of Guile's
|
||
;; implementations of `integer?', `rational?', and `real?' (exported from this
|
||
;; library) is not entirely consistent with R6RS's requirements for those
|
||
;; functions.
|
||
|
||
(define integer-valued? integer?)
|
||
(define rational-valued? rational?)
|
||
(define real-valued? real?)
|
||
|
||
(define (vector-for-each proc . vecs)
|
||
(apply for-each (cons proc (map vector->list vecs))))
|
||
(define (vector-map proc . vecs)
|
||
(list->vector (apply map (cons proc (map vector->list vecs)))))
|
||
|
||
(define (div-and-mod x y) (let ((q (div x y)) (r (mod x y))) (values q r)))
|
||
|
||
(define (div0 x y)
|
||
(call-with-values (lambda () (div0-and-mod0 x y)) (lambda (q r) q)))
|
||
|
||
(define (mod0 x y)
|
||
(call-with-values (lambda () (div0-and-mod0 x y)) (lambda (q r) r)))
|
||
|
||
(define (div0-and-mod0 x y)
|
||
(call-with-values (lambda () (div-and-mod x y))
|
||
(lambda (q r)
|
||
(cond ((< r (abs (/ y 2))) (values q r))
|
||
((negative? y) (values (- q 1) (+ r y)))
|
||
(else (values (+ q 1) (+ r y)))))))
|
||
|
||
(define raise
|
||
(@ (rnrs exceptions) raise))
|
||
(define condition
|
||
(@ (rnrs conditions) condition))
|
||
(define make-error
|
||
(@ (rnrs conditions) make-error))
|
||
(define make-assertion-violation
|
||
(@ (rnrs conditions) make-assertion-violation))
|
||
(define make-who-condition
|
||
(@ (rnrs conditions) make-who-condition))
|
||
(define make-message-condition
|
||
(@ (rnrs conditions) make-message-condition))
|
||
(define make-irritants-condition
|
||
(@ (rnrs conditions) make-irritants-condition))
|
||
|
||
(define (error who message . irritants)
|
||
(raise (apply condition
|
||
(append (list (make-error))
|
||
(if who (list (make-who-condition who)) '())
|
||
(list (make-message-condition message)
|
||
(make-irritants-condition irritants))))))
|
||
|
||
(define (assertion-violation who message . irritants)
|
||
(raise (apply condition
|
||
(append (list (make-assertion-violation))
|
||
(if who (list (make-who-condition who)) '())
|
||
(list (make-message-condition message)
|
||
(make-irritants-condition irritants))))))
|
||
|
||
(define-syntax assert
|
||
(syntax-rules ()
|
||
((_ expression)
|
||
(if (not expression)
|
||
(raise (condition
|
||
(make-assertion-violation)
|
||
(make-message-condition
|
||
(format #f "assertion failed: ~s" 'expression))))))))
|
||
|
||
)
|