mirror of
https://github.com/justinethier/cyclone.git
synced 2025-07-16 17:27:33 +02:00
Extend exact-integer-sqrt to support bignums
This commit is contained in:
parent
53ed84b765
commit
a45621a8de
1 changed files with 10 additions and 1 deletions
|
@ -1070,13 +1070,22 @@
|
|||
(define-c bignum?
|
||||
"(void *data, int argc, closure _, object k, object obj)"
|
||||
" return_closcall1(data, k, Cyc_is_bignum(obj)); ")
|
||||
(define-c bignum-sqrt
|
||||
"(void *data, int argc, closure _, object k, object obj)"
|
||||
" alloc_bignum(data, bn);
|
||||
if (MP_OKAY != mp_sqrt(&(bignum_value(obj)), &bignum_value(bn))) {
|
||||
Cyc_rt_raise2(data, \"Error computing sqrt\", obj);
|
||||
}
|
||||
return_closcall1(data, k, bn); ")
|
||||
;; from mosh
|
||||
(define (exact-integer-sqrt k)
|
||||
(unless (and (exact? k)
|
||||
(integer? k)
|
||||
(not (negative? k)))
|
||||
(error "exact non-negative integer required" k))
|
||||
(let* ((s (exact (truncate (sqrt k))))
|
||||
(let* ((s (if (bignum? k)
|
||||
(bignum-sqrt k)
|
||||
(exact (truncate (sqrt k)))))
|
||||
(r (- k (* s s))))
|
||||
(values s r)))
|
||||
(define-c sqrt
|
||||
|
|
Loading…
Add table
Reference in a new issue