Fix utils round, to handle negative numbers, and fix doc string

master
Karl F 2021-01-03 20:13:16 +01:00 committed by Webster Sheets
parent b5dcfc8e85
commit b15d7ae49d
1 changed files with 8 additions and 5 deletions

View File

@ -273,13 +273,16 @@ utils.reverse = function(t)
end
--
-- round: Round x to closest multiple of N, but never lower than N
-- round: Round any real number, x, to closest multiple of magnitude |N|,
-- but never lower. N defaults to 1, if omitted.
--
-- value = round(unsorted_table, 25)
-- x_steps_of_N = round(x, N)
--
utils.round = function(x, n)
local step = n or 1
x = math.round(x/n)*n
return x < n and n or x
local s = math.sign(x)
n = n or 1
n = math.abs(n)
x = math.round(math.abs(x)/n)*n
return x < n and n*s or x*s
end
return utils