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
22
math.lua
22
math.lua
@ -23,14 +23,22 @@ 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
|
||||
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,
|
||||
__call = function(_, number, base)
|
||||
if not base then
|
||||
return math.log(number)
|
||||
end
|
||||
return math.log(number) / math.log(base)
|
||||
end
|
||||
self[base] = base_log
|
||||
return base_log
|
||||
end})
|
||||
})
|
||||
|
||||
-- one-based mod
|
||||
function onemod(number, modulus)
|
||||
|
4
test.lua
4
test.lua
@ -26,7 +26,9 @@ do
|
||||
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
|
||||
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