(let ((n (flo:round->exact guess)))
(if (int:= x (int:* n n))
n
- guess))
+ (receive (s e) (exact-integer-sqrt x)
+ (if (int:zero? e)
+ s
+ guess))))
(let ((q (flo:->rational guess)))
(if (rat:= x (rat:square q))
q
- guess)))))
+ (receive (ns ne) (exact-integer-sqrt (rat:numerator x))
+ (if (int:zero? ne)
+ (receive (ds de) (exact-integer-sqrt (rat:denominator x))
+ (if (int:zero? de)
+ (rat:/ ns ds)
+ guess))
+ guess)))))))
(define (real:sqrt x)
(if (flonum? x) (flo:sqrt x) (rat:sqrt x)))
(4. 2.)
;; Square root of perfect square x times 2i should be exactly x+xi.
(,(make-rectangular 0 (* 2 (expt 2 -4000)))
- ,(make-rectangular (expt 2 -2000) (expt 2 -2000))
- ,expect-failure)
+ ,(make-rectangular (expt 2 -2000) (expt 2 -2000)))
(,(make-rectangular 0. (* 2 flo:smallest-positive-subnormal))
,(make-rectangular (expt 2. (/ flo:subnormal-exponent-min 2))
(expt 2. (/ flo:subnormal-exponent-min 2))))
,expect-error)
;; Likewise, sqrt of perfect square x times -2i should be x-xi.
(,(make-rectangular 0 (* -2 (expt 2 -4000)))
- ,(make-rectangular (expt 2 -2000) (- (expt 2 -2000)))
- ,expect-failure)
+ ,(make-rectangular (expt 2 -2000) (- (expt 2 -2000))))
(,(make-rectangular 0. (- (* 2 flo:smallest-positive-subnormal)))
,(make-rectangular
(expt 2. (/ flo:subnormal-exponent-min 2))