Discussion:
[gambit-list] How do you do Log N, Log 2, Log 10 in Gambit?
Adam
2018-08-04 09:14:01 UTC
Permalink
Hi,

How do you do Log N, Log 2 and Log 10 in Gambit?

Thanks!
Adam
Marc Feeley
2018-08-04 11:35:57 UTC
Permalink
Post by Adam
Hi,
How do you do Log N, Log 2 and Log 10 in Gambit?
Thanks!
Adam
(exp 1)
2.718281828459045
Post by Adam
(log (exp 1))
1.
Post by Adam
(/ (log 1000) (log 10))
2.9999999999999996
Post by Adam
(/ (log 1024) (log 2))
10.
Post by Adam
(- (integer-length 1024) 1)
10

The procedure integer-length can be useful for exact integers if you want an exact integer result.

Marc
Adam
2018-08-04 12:08:57 UTC
Permalink
Post by Marc Feeley
Post by Adam
Hi,
How do you do Log N, Log 2 and Log 10 in Gambit?
Thanks!
Adam
(exp 1)
2.718281828459045
Post by Adam
(log (exp 1))
1.
Post by Adam
(/ (log 1000) (log 10))
2.9999999999999996
Post by Adam
(/ (log 1024) (log 2))
10.
While this may understandably appear trivial for you, would you mind to
give one minute to spell out the code for

(define (log2 n) ...)
(define (log10 n) ...)
(define (logn n base) ...)

?
Post by Marc Feeley
Post by Adam
(- (integer-length 1024) 1)
10
The procedure integer-length can be useful for exact integers if you want
an exact integer result.
Ah interesting, |integer-length| counts an exact integer's significant bits
Post by Marc Feeley
(integer-length 1)
1
Post by Marc Feeley
(integer-length 2)
2
Post by Marc Feeley
(integer-length 4)
3
Post by Marc Feeley
(integer-length 8)
4
Post by Marc Feeley
(integer-length 16)
5

Right, so this gives us something like, for |n| > 0,

(define (integer-log2 n) (- (integer-length n) 1))
Marc Feeley
2018-08-04 12:27:04 UTC
Permalink
While this may understandably appear trivial for you, would you mind to give one minute to spell out the code for
(define (log2 n) (logn n 2))
(define (log10 n) (logn n 10))
(define (logn n base) (/ (log n) (log base)))

Marc

Loading...