This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

square root (Re: guile & r5rs)


> > The old favourite, Newton's iterative square root looks like:
> 
> Right; but you've totally skipped the interesting part.  It has to
> work in bignum arithmetic only.

OK, I was hoping that you would do the fiddly work,
don't say I'm not good to you:


(define (square-root-recur C X)
  (let* ((x2 (quotient (+ (quotient (+ C X -1) X) X) 2)))
    (cond ((>= x2 X) X)
	  (else (square-root-recur C x2)))))

(define (square-root C)
  (cond ((< C 0) (please-install-complex-support))
	((< C 1) 0)
	(else (square-root-recur C C))))


That seems to get the same answer as bc for the numbers that
I felt like checking. Probably raiding a different bignum
library is better than figuring it all out again, unless there
are some very special requirements for guile that I'm not aware
of. Isn't the source for bc a GPL item anyhow?

	- Tel