]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Add `math.round` and fix `vector.round` (#10803)
authorVincent Robinson <robinsonvincent89@gmail.com>
Thu, 1 Apr 2021 22:18:58 +0000 (15:18 -0700)
committerGitHub <noreply@github.com>
Thu, 1 Apr 2021 22:18:58 +0000 (00:18 +0200)
.luacheckrc
builtin/common/misc_helpers.lua
builtin/common/vector.lua
doc/lua_api.txt

index e010ab95c02452fa3b899acec52ef129920c0f94..a922bdea9af7950a7777b8125dc98c1099e3157a 100644 (file)
@@ -20,7 +20,7 @@ read_globals = {
 
        string = {fields = {"split", "trim"}},
        table  = {fields = {"copy", "getn", "indexof", "insert_all"}},
-       math   = {fields = {"hypot"}},
+       math   = {fields = {"hypot", "round"}},
 }
 
 globals = {
index 0f3897f471c25eb82512913775d3bd79162e79a2..d5f25f2fee2369e25bd4f14430dc302cef6c7b9d 100644 (file)
@@ -244,6 +244,15 @@ function math.factorial(x)
        return v
 end
 
+
+function math.round(x)
+       if x >= 0 then
+               return math.floor(x + 0.5)
+       end
+       return math.ceil(x - 0.5)
+end
+
+
 function core.formspec_escape(text)
        if text ~= nil then
                text = string.gsub(text,"\\","\\\\")
index d6437deda20b45e2a51dbab87c7b2a65769704f3..b04c1261080e52b70211c5689425506c6baefbf1 100644 (file)
@@ -41,9 +41,9 @@ end
 
 function vector.round(v)
        return {
-               x = math.floor(v.x + 0.5),
-               y = math.floor(v.y + 0.5),
-               z = math.floor(v.z + 0.5)
+               x = math.round(v.x),
+               y = math.round(v.y),
+               z = math.round(v.z)
        }
 end
 
index d333ca58b24681698388aa84c56b0bc6c4f4515a..8a8f57eb3464ce30fe251cfc3665a35cf84aea5a 100644 (file)
@@ -3163,6 +3163,7 @@ For the following functions, `v`, `v1`, `v2` are vectors,
     * Returns a vector, each dimension rounded down.
 * `vector.round(v)`:
     * Returns a vector, each dimension rounded to nearest integer.
+    * At a multiple of 0.5, rounds away from zero.
 * `vector.apply(v, func)`:
     * Returns a vector where the function `func` has been applied to each
       component.
@@ -3237,6 +3238,8 @@ Helper functions
     * If the absolute value of `x` is within the `tolerance` or `x` is NaN,
       `0` is returned.
 * `math.factorial(x)`: returns the factorial of `x`
+* `math.round(x)`: Returns `x` rounded to the nearest integer.
+    * At a multiple of 0.5, rounds away from zero.
 * `string.split(str, separator, include_empty, max_splits, sep_is_pattern)`
     * `separator`: string, default: `","`
     * `include_empty`: boolean, default: `false`