mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-22 07:13:45 +01:00
Add math.log(number, [base])
This commit is contained in:
parent
e33360ee49
commit
479afb6c88
12
math.lua
12
math.lua
@ -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)
|
||||
|
2
test.lua
2
test.lua
@ -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
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user