1
0

Add math.round and fix vector.round (#10803)

This commit is contained in:
Vincent Robinson 2021-04-01 15:18:58 -07:00 committed by Maksym H
parent 4f93cf68cc
commit 685a78c18b
4 changed files with 17 additions and 5 deletions

View File

@ -20,7 +20,7 @@ read_globals = {
string = {fields = {"split", "trim"}}, string = {fields = {"split", "trim"}},
table = {fields = {"copy", "getn", "indexof", "insert_all"}}, table = {fields = {"copy", "getn", "indexof", "insert_all"}},
math = {fields = {"hypot"}}, math = {fields = {"hypot", "round"}},
} }
globals = { globals = {

View File

@ -244,6 +244,15 @@ function math.factorial(x)
return v return v
end 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) function core.formspec_escape(text)
if text ~= nil then if text ~= nil then
text = text:gsub("\\", "\\\\") text = text:gsub("\\", "\\\\")

View File

@ -1,6 +1,6 @@
vector = {} vector = {}
local floor, hypot = math.floor, math.hypot local floor, hypot, round = math.floor, math.hypot, math.round
local min, max, pi = math.min, math.max, math.pi local min, max, pi = math.min, math.max, math.pi
function vector.new(a, b, c) function vector.new(a, b, c)
@ -43,9 +43,9 @@ end
function vector.round(v) function vector.round(v)
return { return {
x = floor(v.x + 0.5), x = round(v.x),
y = floor(v.y + 0.5), y = round(v.y),
z = floor(v.z + 0.5) z = round(v.z)
} }
end end

View File

@ -3164,6 +3164,7 @@ For the following functions, `v`, `v1`, `v2` are vectors,
* Returns a vector, each dimension rounded down. * Returns a vector, each dimension rounded down.
* `vector.round(v)`: * `vector.round(v)`:
* Returns a vector, each dimension rounded to nearest integer. * Returns a vector, each dimension rounded to nearest integer.
* At a multiple of 0.5, rounds away from zero.
* `vector.apply(v, func)`: * `vector.apply(v, func)`:
* Returns a vector where the function `func` has been applied to each * Returns a vector where the function `func` has been applied to each
component. component.
@ -3342,6 +3343,8 @@ Helper functions
* If the absolute value of `x` is within the `tolerance` or `x` is NaN, * If the absolute value of `x` is within the `tolerance` or `x` is NaN,
`0` is returned. `0` is returned.
* `math.factorial(x)`: returns the factorial of `x` * `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)` * `string.split(str, separator, include_empty, max_splits, sep_is_pattern)`
* `separator`: string, default: `","` * `separator`: string, default: `","`
* `include_empty`: boolean, default: `false` * `include_empty`: boolean, default: `false`