Skip to content

Commit

Permalink
math.round
Browse files Browse the repository at this point in the history
  • Loading branch information
v-rob committed Jan 14, 2021
1 parent 58a7090 commit f24094f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
9 changes: 9 additions & 0 deletions builtin/common/misc_helpers.lua
Expand Up @@ -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,"\\","\\\\")
Expand Down
6 changes: 3 additions & 3 deletions builtin/common/vector.lua
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions doc/lua_api.txt
Expand Up @@ -2113,7 +2113,7 @@ Examples
list[current_player;main;0,3.5;8,4;]
list[current_player;craft;3,0;3,3;]
list[current_player;craftpreview;7,1;1,1;]

Version History
---------------

Expand Down Expand Up @@ -3221,6 +3221,7 @@ 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.
* `string.split(str, separator, include_empty, max_splits, sep_is_pattern)`
* `separator`: string, default: `","`
* `include_empty`: boolean, default: `false`
Expand Down Expand Up @@ -7641,12 +7642,12 @@ Used by `minetest.register_node`.
-- intensity: 1.0 = mid range of regular TNT.
-- If defined, called when an explosion touches the node, instead of
-- removing the node.

mod_origin = "modname",
-- stores which mod actually registered a node
-- if it can not find a source, returns "??"
-- useful for getting what mod truly registered something
-- example: if a node is registered as ":othermodname:nodename",
-- example: if a node is registered as ":othermodname:nodename",
-- nodename will show "othermodname", but mod_orgin will say "modname"
}

Expand Down

0 comments on commit f24094f

Please sign in to comment.