Merge pull request #65 from idbrii/fix-round

Fix index nil error in private.round
This commit is contained in:
shakesoda 2021-11-24 12:08:57 -08:00 committed by GitHub
commit a3aacc354c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 2 deletions

View File

@ -5,7 +5,7 @@ local floor = math.floor
local ceil = math.ceil
function private.round(value, precision)
if precision then return utils.round(value / precision) * precision end
if precision then return private.round(value / precision) * precision end
return value >= 0 and floor(value+0.5) or ceil(value-0.5)
end

View File

@ -27,6 +27,18 @@ describe("utils:", function()
local v = utils.decay(0, 1, 0.5, 1)
assert.is_true(tolerance(v, 0.39346934028737))
end)
it("rounds a number", function()
-- round up
local v = utils.round(1.3252525, 0.01)
assert.is_true(tolerance(v, 1.33))
-- round down
v = utils.round(1.3242525, 0.1)
assert.is_true(tolerance(v, 1.3))
-- no precision
v = utils.round(1.3242525)
assert.is_true(tolerance(v, 1))
end)
end)
--[[
@ -37,7 +49,6 @@ tolerance(value, threshold)
map(value, min_in, max_in, min_out, max_out)
lerp(progress, low, high)
smoothstep(progress, low, high)
round(value, precision)
wrap(value, limit)
is_pot(value)
project_on(out, a, b)