From 4dc773865ec236c64392eedd930d968c37768a7f Mon Sep 17 00:00:00 2001 From: David Briscoe Date: Wed, 24 Nov 2021 09:37:28 -0800 Subject: [PATCH] Reference private.round instead of util.round Fix "Error: src/lib/cpml/modules/_private_utils.lua:8: attempt to index global 'utils' (a nil value)" Looks like when round was moved to _private_utils, this didn't get replaced. There is no utils defined and utils.round just points to private.round. Add a test. This never triggered test failures because test don't pass precision. Test A love2d program with main.lua: local Vec2 = require "cpml.modules.vec2" local v = Vec2(1.234, 3.5326) print(v:round(0.1)) --- modules/_private_utils.lua | 2 +- spec/utils_spec.lua | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/modules/_private_utils.lua b/modules/_private_utils.lua index 8376a08..2c4aca0 100644 --- a/modules/_private_utils.lua +++ b/modules/_private_utils.lua @@ -5,7 +5,7 @@ local floor = math.floor local ceil = math.ceil function private.round(value, precision) - if precision then return utils.round(value / precision) * precision end + if precision then return private.round(value / precision) * precision end return value >= 0 and floor(value+0.5) or ceil(value-0.5) end diff --git a/spec/utils_spec.lua b/spec/utils_spec.lua index 1b7a8eb..337361e 100644 --- a/spec/utils_spec.lua +++ b/spec/utils_spec.lua @@ -27,6 +27,18 @@ describe("utils:", function() local v = utils.decay(0, 1, 0.5, 1) assert.is_true(tolerance(v, 0.39346934028737)) end) + + it("rounds a number", function() + -- round up + local v = utils.round(1.3252525, 0.01) + assert.is_true(tolerance(v, 1.33)) + -- round down + v = utils.round(1.3242525, 0.1) + assert.is_true(tolerance(v, 1.3)) + -- no precision + v = utils.round(1.3242525) + assert.is_true(tolerance(v, 1)) + end) end) --[[ @@ -37,7 +49,6 @@ tolerance(value, threshold) map(value, min_in, max_in, min_out, max_out) lerp(progress, low, high) smoothstep(progress, low, high) -round(value, precision) wrap(value, limit) is_pot(value) project_on(out, a, b)