Fix utils round, to handle negative numbers, and fix doc string
parent
b5dcfc8e85
commit
b15d7ae49d
|
@ -273,13 +273,16 @@ utils.reverse = function(t)
|
||||||
end
|
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)
|
utils.round = function(x, n)
|
||||||
local step = n or 1
|
local s = math.sign(x)
|
||||||
x = math.round(x/n)*n
|
n = n or 1
|
||||||
return x < n and n or x
|
n = math.abs(n)
|
||||||
|
x = math.round(math.abs(x)/n)*n
|
||||||
|
return x < n and n*s or x*s
|
||||||
end
|
end
|
||||||
return utils
|
return utils
|
||||||
|
|
Loading…
Reference in New Issue