-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
log10.e
37 lines (34 loc) · 953 Bytes
/
log10.e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
--
-- log10.e
--
--with debug
global function log10(atom n)
--Added 26/9/22:
if integer(n) and n>0 then
-- (sadly not quite as efficient as log2)
atom t = 1, r = 0
while t<=n do
if t=n then return r end if
t *= 10
r += 1
end while
end if
return log(n) * INVLN10
end function
global function log2(atom n)
--Added 26/9/22:
if integer(n) and n>0
and and_bits(n,n-1)=0 then -- "if Kernigans bit counter would yield 1"
-- KBC says and_bits(xxx1{0},xxx0{1}) is xxx0{0}, ie
-- least significant set bit is cleared - clever, eh?
integer t = 1, r = 0 -- 2^0==1
while true do
if t=n then return r end if
t *= 2
r += 1 -- 2^r==t
end while
end if
--</26/9/22>
-- return log(n) * INVLN2
return log(n) * 1.44269504088896340739
end function