From a370e2ee6ead86363ddf4ebe6ac757e1855e270a Mon Sep 17 00:00:00 2001 From: Joel Johansson Date: Fri, 20 Jul 2018 12:54:44 +0200 Subject: [PATCH] faster round_value Seems to cut about 1/3 of the time. The very fastest would simply be floor(num+0.5) with math.floor being cached, but it's only slightly faster than my suggestion. And btw, all of these variations seem to handle negative values perfectly fine in Lua 5.1, contrary to what someone said (? please verify). --- classes/cLib.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/classes/cLib.lua b/classes/cLib.lua index bdb1d71..41842de 100644 --- a/classes/cLib.lua +++ b/classes/cLib.lua @@ -269,12 +269,12 @@ function cLib.least_common(t) end --------------------------------------------------------------------------------------------------- --- [Static] Round value (from http://lua-users.org/wiki/SimpleRound) +-- [Static] Round value -- @param num (number) function cLib.round_value(num) - if num >= 0 then return math.floor(num+.5) - else return math.ceil(num-.5) end + local frac = num % 1 + return frac < 0.5 and num - frac or num + 1 - frac end ---------------------------------------------------------------------------------------------------