* exact->inexact is now implemnted.
* +, -, and * now handles flonums by converting exact arguments to inexact if the other argument is a flonum.
This commit is contained in:
Binary file not shown.
+73
-2
@@ -26,6 +26,17 @@
|
||||
(lambda (x)
|
||||
(foreign-call "ikrt_isbignum" x)))
|
||||
|
||||
(define (fixnum->flonum x)
|
||||
(foreign-call "ikrt_fixnum_to_flonum" x))
|
||||
(define (bignum->flonum x)
|
||||
(foreign-call "ikrt_bignum_to_flonum" x))
|
||||
(define ($fl+ x y)
|
||||
(foreign-call "ikrt_fl_plus" x y))
|
||||
(define ($fl- x y)
|
||||
(foreign-call "ikrt_fl_minus" x y))
|
||||
(define ($fl* x y)
|
||||
(foreign-call "ikrt_fl_times" x y))
|
||||
|
||||
(define binary+
|
||||
(lambda (x y)
|
||||
(cond
|
||||
@@ -35,6 +46,8 @@
|
||||
(foreign-call "ikrt_fxfxplus" x y)]
|
||||
[(bignum? y)
|
||||
(foreign-call "ikrt_fxbnplus" x y)]
|
||||
[(flonum? y)
|
||||
($fl+ (fixnum->flonum x) y)]
|
||||
[else
|
||||
(error '+ "~s is not a number" y)])]
|
||||
[(bignum? x)
|
||||
@@ -43,6 +56,18 @@
|
||||
(foreign-call "ikrt_fxbnplus" y x)]
|
||||
[(bignum? y)
|
||||
(foreign-call "ikrt_bnbnplus" x y)]
|
||||
[(flonum? y)
|
||||
($fl+ (bignum->flonum x) y)]
|
||||
[else
|
||||
(error '+ "~s is not a number" y)])]
|
||||
[(flonum? x)
|
||||
(cond
|
||||
[(fixnum? y)
|
||||
($fl+ x (fixnum->flonum y))]
|
||||
[(bignum? y)
|
||||
($fl+ x (bignum->flonum y))]
|
||||
[(flonum? y)
|
||||
($fl+ x y)]
|
||||
[else
|
||||
(error '+ "~s is not a number" y)])]
|
||||
[else (error '+ "~s is not a number" x)])))
|
||||
@@ -77,6 +102,8 @@
|
||||
(foreign-call "ikrt_fxfxminus" x y)]
|
||||
[(bignum? y)
|
||||
(foreign-call "ikrt_fxbnminus" x y)]
|
||||
[(flonum? y)
|
||||
($fl- (fixnum->flonum x) y)]
|
||||
[else
|
||||
(error '- "~s is not a number" y)])]
|
||||
[(bignum? x)
|
||||
@@ -85,8 +112,20 @@
|
||||
(foreign-call "ikrt_bnfxminus" x y)]
|
||||
[(bignum? y)
|
||||
(foreign-call "ikrt_bnbnminus" x y)]
|
||||
[(flonum? y)
|
||||
($fl- (bignum->flonum x) y)]
|
||||
[else
|
||||
(error '- "~s is not a number" y)])]
|
||||
[(flonum? x)
|
||||
(cond
|
||||
[(fixnum? y)
|
||||
($fl- x (fixnum->flonum y))]
|
||||
[(bignum? y)
|
||||
($fl- x (bignum->flonum y))]
|
||||
[(flonum? y)
|
||||
($fl- x y)]
|
||||
[else
|
||||
(error '- "~s is not a number" y)])]
|
||||
[else (error '- "~s is not a number" x)])))
|
||||
|
||||
(define binary*
|
||||
@@ -98,6 +137,8 @@
|
||||
(foreign-call "ikrt_fxfxmult" x y)]
|
||||
[(bignum? y)
|
||||
(foreign-call "ikrt_fxbnmult" x y)]
|
||||
[(flonum? y)
|
||||
($fl* (fixnum->flonum x) y)]
|
||||
[else
|
||||
(error '* "~s is not a number" y)])]
|
||||
[(bignum? x)
|
||||
@@ -106,8 +147,20 @@
|
||||
(foreign-call "ikrt_fxbnmult" y x)]
|
||||
[(bignum? y)
|
||||
(foreign-call "ikrt_bnbnmult" x y)]
|
||||
[(flonum? y)
|
||||
($fl* (bignum->flonum x) y)]
|
||||
[else
|
||||
(error '* "~s is not a number" y)])]
|
||||
[(flonum? x)
|
||||
(cond
|
||||
[(fixnum? y)
|
||||
($fl* x (fixnum->flonum y))]
|
||||
[(bignum? y)
|
||||
($fl* x (bignum->flonum y))]
|
||||
[(flonum? y)
|
||||
($fl* x y)]
|
||||
[else
|
||||
(error '* "~s is not a number" y)])]
|
||||
[else (error '* "~s is not a number" x)])))
|
||||
|
||||
(define +
|
||||
@@ -212,7 +265,7 @@
|
||||
x
|
||||
(error 'max "~s is not a number" x))]))
|
||||
|
||||
(define min
|
||||
(define min
|
||||
(case-lambda
|
||||
[(x y)
|
||||
(cond
|
||||
@@ -250,10 +303,18 @@
|
||||
|
||||
(define complex?
|
||||
(lambda (x) (number? x)))
|
||||
|
||||
(define real?
|
||||
(lambda (x) (number? x)))
|
||||
|
||||
(define rational?
|
||||
(lambda (x) (number? x)))
|
||||
(lambda (x)
|
||||
(cond
|
||||
[(fixnum? x) #t]
|
||||
[(bignum? x) #t]
|
||||
[(flonum? x) #f]
|
||||
[else (error 'rational? "~s is not a number" x)])))
|
||||
|
||||
(define integer?
|
||||
(lambda (x) (number? x)))
|
||||
|
||||
@@ -266,6 +327,15 @@
|
||||
[else
|
||||
(error 'exact? "~s is not a number" x)])))
|
||||
|
||||
(define exact->inexact
|
||||
(lambda (x)
|
||||
(cond
|
||||
[(fixnum? x) (fixnum->flonum x)]
|
||||
[(bignum? x) (bignum->flonum x)]
|
||||
[else
|
||||
(error 'exact->inexact
|
||||
"~s is not an exact number" x)])))
|
||||
|
||||
(define inexact?
|
||||
(lambda (x)
|
||||
(cond
|
||||
@@ -545,4 +615,5 @@
|
||||
(primitive-set! 'exact? exact?)
|
||||
(primitive-set! 'inexact? inexact?)
|
||||
(primitive-set! 'integer? integer?)
|
||||
(primitive-set! 'exact->inexact exact->inexact)
|
||||
)
|
||||
|
||||
+57
-6
@@ -45,6 +45,8 @@
|
||||
[(eof-object? c) n]
|
||||
[(digit? c)
|
||||
(tokenize-number (+ (* n 10) (char->num c)) p)]
|
||||
[($char= c #\.)
|
||||
(tokenize-flonum/with-digits n p)]
|
||||
[(delimiter? c)
|
||||
(unread-char c p)
|
||||
n]
|
||||
@@ -123,6 +125,46 @@
|
||||
(let ([i ($char->fixnum c)])
|
||||
(unless (or (fx= i 10) (fx= i 13))
|
||||
(skip-comment p)))))))
|
||||
(define (ls->flonum ls pos?)
|
||||
(let ([str (if pos?
|
||||
(list->string
|
||||
(cons #\. (reverse ls)))
|
||||
(list->string
|
||||
(list* #\- #\. (reverse ls))))])
|
||||
(string->flonum str)))
|
||||
(define (tokenize-flonum ls pos? p)
|
||||
(let ([c (read-char p)])
|
||||
(cond
|
||||
[(eof-object? c) (ls->flonum ls pos?)]
|
||||
[(digit? c) (tokenize-flonum (cons c ls) pos? p)]
|
||||
[(delimiter? c)
|
||||
(unread-char c p)
|
||||
(ls->flonum ls pos?)]
|
||||
[else
|
||||
(unread-char c p)
|
||||
(error 'tokenize "invalid char ~a after flonum" c)])))
|
||||
(define (tokenize-flonum/no-digits pos? p)
|
||||
(let ([c (read-char p)])
|
||||
(cond
|
||||
[(eof-object? c)
|
||||
(error 'tokenize "invalid eof")]
|
||||
[(digit? c)
|
||||
(tokenize-flonum (list c) pos? p)]
|
||||
[else
|
||||
(unread-char c p)
|
||||
(error 'tokenize "invalid char ~a after decimal point" c)])))
|
||||
(define (tokenize-flonum/with-digits n p)
|
||||
(let ([c (read-char p)])
|
||||
(cond
|
||||
[(eof-object? c) (+ n (string->flonum "0.0"))]
|
||||
[(digit? c)
|
||||
(+ n (tokenize-flonum (list c) #t p))]
|
||||
[(delimiter? c)
|
||||
(unread-char c p)
|
||||
(+ n (string->flonum "0.0"))]
|
||||
[else
|
||||
(unread-char c p)
|
||||
(error 'tokenize "invalid char ~a after decimal point" c)])))
|
||||
(define tokenize-plus
|
||||
(lambda (p)
|
||||
(let ([c (peek-char p)])
|
||||
@@ -132,6 +174,9 @@
|
||||
[(digit? c)
|
||||
(read-char p)
|
||||
(cons 'datum (tokenize-number (char->num c) p))]
|
||||
[($char= c #\.)
|
||||
(read-char p)
|
||||
(cons 'datum (tokenize-flonum/no-digits #t p))]
|
||||
[else (error 'tokenize "invalid sequence +~a" c)]))))
|
||||
(define tokenize-minus
|
||||
(lambda (p)
|
||||
@@ -142,6 +187,9 @@
|
||||
[(digit? c)
|
||||
(read-char p)
|
||||
(cons 'datum (* -1 (tokenize-number (char->num c) p)))]
|
||||
[($char= c #\.)
|
||||
(read-char p)
|
||||
(cons 'datum (tokenize-flonum/no-digits #f p))]
|
||||
[else (error 'tokenize "invalid sequence -~a" c)]))))
|
||||
(define tokenize-dot
|
||||
(lambda (p)
|
||||
@@ -158,13 +206,16 @@
|
||||
[($char= c #\.) ; this is the third
|
||||
(let ([c (peek-char p)])
|
||||
(cond
|
||||
[(eof-object? c) '(datum . ...)]
|
||||
[(delimiter? c) '(datum . ...)]
|
||||
[else
|
||||
(error 'tokenize "invalid syntax ...~a" c)]))]
|
||||
[(eof-object? c) '(datum . ...)]
|
||||
[(delimiter? c) '(datum . ...)]
|
||||
[else
|
||||
(error 'tokenize "invalid syntax ...~a" c)]))]
|
||||
[else
|
||||
(unread-char c)
|
||||
(unread-char c p)
|
||||
(error 'tokenize "invalid syntax ..~a" c)]))]
|
||||
[(digit? c)
|
||||
(read-char p)
|
||||
(cons 'datum (tokenize-flonum (list c) #t p))]
|
||||
[else
|
||||
(error 'tokenize "invalid syntax .~a" c)]))))
|
||||
(define tokenize-char*
|
||||
@@ -265,7 +316,7 @@
|
||||
[($char= #\1 c) (read-binary (+ (* ac 2) 1) (cons c chars) p)]
|
||||
[(delimiter? c) (unread-char c p) ac]
|
||||
[else
|
||||
(unread-char c)
|
||||
(unread-char c p)
|
||||
(error 'tokenize "invalid syntax #b~a"
|
||||
(list->string (reverse (cons c chars))))]))))
|
||||
(define tokenize-hash
|
||||
|
||||
+1
-1
@@ -91,7 +91,7 @@
|
||||
even? odd? member char-whitespace? char-alphabetic?
|
||||
char-downcase max min complex? real? rational?
|
||||
exact? inexact? integer?
|
||||
string->number
|
||||
string->number exact->inexact
|
||||
|
||||
flonum? flonum->string string->flonum
|
||||
))
|
||||
|
||||
Reference in New Issue
Block a user