mirror of
https://github.com/ashinn/chibi-scheme.git
synced 2025-07-06 20:56:38 +02:00
Ensuring exact-integer-sqrt always returns positive remainders.
Moving out of (chibi) into only (scheme base).
This commit is contained in:
parent
c559ae11be
commit
0daa2f270a
2 changed files with 10 additions and 7 deletions
|
@ -1039,13 +1039,6 @@
|
|||
|
||||
(define (eqv? a b) (if (eq? a b) #t (and (number? a) (equal? a b))))
|
||||
|
||||
(define (exact-integer-sqrt x)
|
||||
(let ((res (sqrt x)))
|
||||
(if (exact? res)
|
||||
(values res 0)
|
||||
(let ((res (inexact->exact (truncate res))))
|
||||
(values res (- x (* res res)))))))
|
||||
|
||||
(define (zero? x) (= x 0))
|
||||
(define (positive? x) (> x 0))
|
||||
(define (negative? x) (< x 0))
|
||||
|
|
|
@ -54,6 +54,16 @@
|
|||
(define (floor/ n m)
|
||||
(values (floor-quotient n m) (floor-remainder n m)))
|
||||
|
||||
(define (exact-integer-sqrt x)
|
||||
(let ((res (sqrt x)))
|
||||
(if (exact? res)
|
||||
(values res 0)
|
||||
(let lp ((res (inexact->exact (truncate res))))
|
||||
(let ((rem (- x (* res res))))
|
||||
(if (negative? rem)
|
||||
(lp (- res 1))
|
||||
(values res rem)))))))
|
||||
|
||||
;; Adapted from Bawden's algorithm.
|
||||
(define (rationalize x e)
|
||||
(define (sr x y return)
|
||||
|
|
Loading…
Add table
Reference in a new issue