From f8a5f7e004d8db8baf5d6b1b0475b90eeb41bf8e Mon Sep 17 00:00:00 2001 From: Alex Shinn Date: Sun, 14 Apr 2013 04:59:49 +0000 Subject: [PATCH] exact-integer-sqrt should continue the babylonian method in scheme. The estimate given by the C code could be off by a large margin when the result is inexact, converging far too slowly if we use simple increments in Scheme. --- lib/scheme/extras.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/scheme/extras.scm b/lib/scheme/extras.scm index d7f70393..8dcb04df 100644 --- a/lib/scheme/extras.scm +++ b/lib/scheme/extras.scm @@ -61,7 +61,7 @@ (let lp ((res (inexact->exact (truncate res)))) (let ((rem (- x (* res res)))) (if (negative? rem) - (lp (- res 1)) + (lp (quotient (+ res (quotient x res)) 2)) (values res rem))))))) ;; Adapted from Bawden's algorithm.