Re: Possible additions to SRFI 278
Jeronimo Pellegrini 13 Sep 2026 14:16 UTC
On 2026-09-12 21:18, Peter McGoron wrote:
> I like the idea of exact-integer-log, but doing that in general sounds
> like I have to transform the radix of the number. (I don't have the
> energy or time to write that implementation right now.)
See the pseudocode below, it's just a few lines of code. The comments
are
from the STklos implementation.
;; This is easier and faster than R7RS exact-integer-sqrt, since:
;; * log(n b) = log_2(n) / log_2(b).
;; * integer-length(n) = floor(log_2(n)) + 1
;; We just put it all together.
log2n <- int_length(n) - 1 ; or use floor and inexact log
log2b <- int_length(b) - 1 ; or use floor and inexact log
L <- quotient(log2n log2b)
step <- 1
;; Ok, so FLOOR(a/b) can be smaller than FLOOR(a)/FLOOR(b).
;; That means we may have overestimated L -- but we should be
;; close. If we overestimated, then (expt b L) will be larger
;; than n, and we can subtract from it until we find the correct
;; number.
;; We do increase the step at each iteration in order to speed
;; up the process. If we go too far, we correct it later. This is
;; fast.
while n < b^L:
L <- L - step
step <- step + 2
;; If we overestimated, then we had to walk down from L.
;; In this case, since we added 2 to the step at each
;; iteration, we may now have UNDERestimated. But then,
;; we undo the last iteration (the "go back" line below),
;; and start walking again, this time with a fixed step
;; equal to 1.
if step > 1: ; did we need to walk?
L <- L + step - 2 ; go back...
while n < b^L:
L <- L - 1 ; don't grow the step this time!
return values L, n - b^L
>> trigonometric functions that work with bignums
>
> This might be too difficult to mandate.
Yes, I agree.
J.