From b15d7ae49d0d315138b867792f5fb41ff9eef44e Mon Sep 17 00:00:00 2001 From: Karl F Date: Sun, 3 Jan 2021 20:13:16 +0100 Subject: [PATCH] Fix utils round, to handle negative numbers, and fix doc string --- data/libs/utils.lua | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/data/libs/utils.lua b/data/libs/utils.lua index ad3086c83..55e5495b7 100644 --- a/data/libs/utils.lua +++ b/data/libs/utils.lua @@ -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