From 5230e8f9d6db99e259f5b4529cec477d519d080b Mon Sep 17 00:00:00 2001 From: Max Cahill <1bardesign@gmail.com> Date: Thu, 9 Jul 2020 15:32:42 +1000 Subject: [PATCH] [fixed] mathx.wrap over-complicated impl, mathx.round (thanks @MikuAuahDark/@sharpobject) --- mathx.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/mathx.lua b/mathx.lua index afad105..35207e3 100644 --- a/mathx.lua +++ b/mathx.lua @@ -8,12 +8,7 @@ local mathx = setmetatable({}, { --wrap v around range [lo, hi) function mathx.wrap(v, lo, hi) - local range = hi - lo - local relative = v - lo - local relative_wrapped = relative % range - local relative_add = relative_wrapped + range - local final_wrap = relative_add % range - return lo + final_wrap + return (v - lo) % (hi - lo) + lo end --wrap i around the indices of t @@ -31,8 +26,11 @@ function mathx.clamp01(v) return mathx.clamp(v, 0, 1) end ---round v to nearest whole +--round v to nearest whole, away from zero function mathx.round(v) + if v < 0 then + return math.ceil(v - 0.5) + end return math.floor(v + 0.5) end