mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-05-21 14:49:18 +02:00
fixing exact?, inexact? and numeric predicates when complex numbers are present
This commit is contained in:
parent
f5e07050ef
commit
366c88bce1
1 changed files with 28 additions and 5 deletions
33
lib/init.scm
33
lib/init.scm
|
@ -891,9 +891,21 @@
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; math utils
|
;; math utils
|
||||||
|
|
||||||
|
(cond-expand
|
||||||
|
(complex
|
||||||
|
(define (exact-complex? x)
|
||||||
|
(and (complex? x) (exact? (complex-real x)) (exact? (complex-imag x))))))
|
||||||
|
|
||||||
(cond-expand
|
(cond-expand
|
||||||
(ratios
|
(ratios
|
||||||
(define (exact? x) (if (fixnum? x) #t (if (bignum? x) #t (ratio? x))))
|
(cond-expand
|
||||||
|
(complex
|
||||||
|
(define (exact? x)
|
||||||
|
(if (fixnum? x)
|
||||||
|
#t
|
||||||
|
(if (bignum? x) #t (if (ratio? x) #t (exact-complex? x))))))
|
||||||
|
(else
|
||||||
|
(define (exact? x) (if (fixnum? x) #t (if (bignum? x) #t (ratio? x))))))
|
||||||
(define (numerator x)
|
(define (numerator x)
|
||||||
(if (ratio? x)
|
(if (ratio? x)
|
||||||
(ratio-numerator x)
|
(ratio-numerator x)
|
||||||
|
@ -903,7 +915,12 @@
|
||||||
(if (ratio? x) (ratio-denominator x) 1)
|
(if (ratio? x) (ratio-denominator x) 1)
|
||||||
(let lp ((x x) (r 1.0)) (if (integer? x) r (lp (* x 10) (* r 10)))))))
|
(let lp ((x x) (r 1.0)) (if (integer? x) r (lp (* x 10) (* r 10)))))))
|
||||||
(else
|
(else
|
||||||
(define (exact? x) (if (fixnum? x) #t (bignum? x)))
|
(cond-expand
|
||||||
|
(complex
|
||||||
|
(define (exact? x)
|
||||||
|
(if (fixnum? x) #t (if (bignum? x) #t (exact-complex? x)))))
|
||||||
|
(else
|
||||||
|
(define (exact? x) (if (fixnum? x) #t (bignum? x)))))
|
||||||
(define (numerator x)
|
(define (numerator x)
|
||||||
(if (integer? x) x (numerator (* x 10))))
|
(if (integer? x) x (numerator (* x 10))))
|
||||||
(define (denominator x)
|
(define (denominator x)
|
||||||
|
@ -911,14 +928,20 @@
|
||||||
1
|
1
|
||||||
(let lp ((x x) (r 1.0)) (if (integer? x) r (lp (* x 10) (* r 10))))))))
|
(let lp ((x x) (r 1.0)) (if (integer? x) r (lp (* x 10) (* r 10))))))))
|
||||||
|
|
||||||
(define inexact? flonum?)
|
(cond-expand
|
||||||
|
(complex
|
||||||
|
(define (inexact? x)
|
||||||
|
(if (flonum? x) #t (and (complex? x) (not (exact-complex? x))))))
|
||||||
|
(else (define inexact? flonum?)))
|
||||||
(define (exact-integer? x) (if (fixnum? x) #t (bignum? x)))
|
(define (exact-integer? x) (if (fixnum? x) #t (bignum? x)))
|
||||||
(define (integer? x)
|
(define (integer? x)
|
||||||
(if (exact-integer? x) #t (and (flonum? x) (= x (truncate x)))))
|
(if (exact-integer? x) #t (and (flonum? x) (= x (truncate x)))))
|
||||||
(define (number? x) (if (inexact? x) #t (exact? x)))
|
(define (number? x) (if (inexact? x) #t (exact? x)))
|
||||||
(define complex? number?)
|
(define complex? number?)
|
||||||
(define rational? number?)
|
(cond-expand
|
||||||
(define real? number?)
|
(complex (define (rational? x) (and (number? x) (not (complex? x)))))
|
||||||
|
(else (define rational? number?)))
|
||||||
|
(define real? rational?)
|
||||||
|
|
||||||
(define (exact-integer-sqrt x)
|
(define (exact-integer-sqrt x)
|
||||||
(let ((res (sqrt x)))
|
(let ((res (sqrt x)))
|
||||||
|
|
Loading…
Add table
Reference in a new issue