Add math.log[base](number)

This commit is contained in:
Lars Mueller 2022-01-17 11:58:16 +01:00
parent 66315d0219
commit e33360ee49
2 changed files with 14 additions and 1 deletions

@ -23,6 +23,15 @@ function sign(number)
if number > 0 then return 1 end
end
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})
-- one-based mod
function onemod(number, modulus)
return ((number - 1) % modulus) + 1

@ -19,11 +19,15 @@ setfenv(1, setmetatable({}, {
-- math
do
local function assert_tonumber(num, base)
local str = modlib.math.tostring(num, base)
local str = math.tostring(num, base)
assert(tonumber(str, base) == num, str)
end
assert_tonumber(134217503, 36)
assert_tonumber(3.14, 10)
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
end
end
-- func