Add math.log(number, [base])

This commit is contained in:
Lars Mueller 2022-01-17 14:16:42 +01:00
parent e33360ee49
commit 479afb6c88
2 changed files with 18 additions and 8 deletions

@ -23,14 +23,22 @@ function sign(number)
if number > 0 then return 1 end
end
log = setmetatable({}, {__index = function(self, base)
log = setmetatable({}, {
__index = function(self, base)
local div = math.log(base)
local function base_log(number)
return math.log(number) / div
end
self[base] = base_log
return base_log
end})
end,
__call = function(_, number, base)
if not base then
return math.log(number)
end
return math.log(number) / math.log(base)
end
})
-- one-based mod
function onemod(number, modulus)

@ -27,6 +27,8 @@ do
for i = -100, 100 do
local log = math.log[2](2^i)
assert(_G.math.abs(log - i) < 2^-40) -- Small tolerance for floating-point precision errors
assert(math.log(2^i) == _G.math.log(2^i))
assert(math.log(2^i, 2) == log)
end
end