mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-07-08 13:37:35 +02:00
Making rationalize on inexact numbers agree with the standard.
I still think this is pointless though.
This commit is contained in:
parent
9ac7caff59
commit
33326888f0
3 changed files with 7 additions and 9 deletions
|
@ -42,7 +42,7 @@
|
||||||
;; Adapted from Bawden's algorithm.
|
;; Adapted from Bawden's algorithm.
|
||||||
(define (rationalize x e)
|
(define (rationalize x e)
|
||||||
(define (sr x y return)
|
(define (sr x y return)
|
||||||
(let ((fx (inexact->exact (floor x))) (fy (inexact->exact (floor y))))
|
(let ((fx (floor x)) (fy (floor y)))
|
||||||
(cond
|
(cond
|
||||||
((>= fx x)
|
((>= fx x)
|
||||||
(return fx 1))
|
(return fx 1))
|
||||||
|
@ -50,12 +50,10 @@
|
||||||
(sr (/ (- y fy)) (/ (- x fx)) (lambda (n d) (return (+ d (* fx n)) n))))
|
(sr (/ (- y fy)) (/ (- x fx)) (lambda (n d) (return (+ d (* fx n)) n))))
|
||||||
(else
|
(else
|
||||||
(return (+ fx 1) 1)))))
|
(return (+ fx 1) 1)))))
|
||||||
(if (exact? x)
|
|
||||||
(let ((return (if (negative? x) (lambda (num den) (/ (- num) den)) /))
|
(let ((return (if (negative? x) (lambda (num den) (/ (- num) den)) /))
|
||||||
(x (abs x))
|
(x (abs x))
|
||||||
(e (abs e)))
|
(e (abs e)))
|
||||||
(sr (- x e) (+ x e) return))
|
(sr (- x e) (+ x e) return)))
|
||||||
x))
|
|
||||||
|
|
||||||
(define (square x) (* x x))
|
(define (square x) (* x x))
|
||||||
|
|
||||||
|
|
|
@ -561,7 +561,7 @@
|
||||||
(test 7 (round 7))
|
(test 7 (round 7))
|
||||||
|
|
||||||
(test 1/3 (rationalize (exact .3) 1/10))
|
(test 1/3 (rationalize (exact .3) 1/10))
|
||||||
;; (test #i1/3 (rationalize .3 1/10)) ; inexact
|
(test #i1/3 (rationalize .3 1/10))
|
||||||
|
|
||||||
(test 1.0 (exp 0))
|
(test 1.0 (exp 0))
|
||||||
(test 20.0855369231877 (exp 3))
|
(test 20.0855369231877 (exp 3))
|
||||||
|
|
2
vm.c
2
vm.c
|
@ -1861,7 +1861,7 @@ sexp sexp_apply (sexp ctx, sexp proc, sexp args) {
|
||||||
else if (sexp_ratiop(_ARG1))
|
else if (sexp_ratiop(_ARG1))
|
||||||
_ARG1 = sexp_make_flonum(ctx, sexp_ratio_to_double(_ARG1));
|
_ARG1 = sexp_make_flonum(ctx, sexp_ratio_to_double(_ARG1));
|
||||||
#endif
|
#endif
|
||||||
else if (! sexp_flonump(_ARG1))
|
} else if (! sexp_flonump(_ARG1))
|
||||||
sexp_raise("exact->inexact: not a number", sexp_list1(ctx, _ARG1));
|
sexp_raise("exact->inexact: not a number", sexp_list1(ctx, _ARG1));
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
|
|
Loading…
Add table
Reference in a new issue