mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 01:03:46 +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
|
if number > 0 then return 1 end
|
||||||
end
|
end
|
||||||
|
|
||||||
log = setmetatable({}, {__index = function(self, base)
|
log = setmetatable({}, {
|
||||||
|
__index = function(self, base)
|
||||||
local div = math.log(base)
|
local div = math.log(base)
|
||||||
local function base_log(number)
|
local function base_log(number)
|
||||||
return math.log(number) / div
|
return math.log(number) / div
|
||||||
end
|
end
|
||||||
self[base] = base_log
|
self[base] = base_log
|
||||||
return 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
|
-- one-based mod
|
||||||
function onemod(number, modulus)
|
function onemod(number, modulus)
|
||||||
|
2
test.lua
2
test.lua
@ -27,6 +27,8 @@ do
|
|||||||
for i = -100, 100 do
|
for i = -100, 100 do
|
||||||
local log = math.log[2](2^i)
|
local log = math.log[2](2^i)
|
||||||
assert(_G.math.abs(log - i) < 2^-40) -- Small tolerance for floating-point precision errors
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user