mirror of
https://github.com/appgurueu/modlib.git
synced 2024-11-26 01:03:46 +01:00
Add math.log[base](number)
This commit is contained in:
parent
66315d0219
commit
e33360ee49
9
math.lua
9
math.lua
@ -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
|
||||
|
6
test.lua
6
test.lua
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user