mirror of
https://github.com/minetest/minetest.git
synced 2025-01-04 07:00:26 +01:00
Fix math.round floating point bug (#14757)
This commit is contained in:
parent
157d129e30
commit
28857841aa
@ -240,12 +240,15 @@ function math.factorial(x)
|
|||||||
return v
|
return v
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
function math.round(x)
|
function math.round(x)
|
||||||
if x >= 0 then
|
if x < 0 then
|
||||||
return math.floor(x + 0.5)
|
local int = math.ceil(x)
|
||||||
|
local frac = x - int
|
||||||
|
return int - ((frac <= -0.5) and 1 or 0)
|
||||||
end
|
end
|
||||||
return math.ceil(x - 0.5)
|
local int = math.floor(x)
|
||||||
|
local frac = x - int
|
||||||
|
return int + ((frac >= 0.5) and 1 or 0)
|
||||||
end
|
end
|
||||||
|
|
||||||
local formspec_escapes = {
|
local formspec_escapes = {
|
||||||
|
@ -176,3 +176,17 @@ describe("formspec_escape", function()
|
|||||||
assert.equal("\\[Hello\\\\\\[", core.formspec_escape("[Hello\\["))
|
assert.equal("\\[Hello\\\\\\[", core.formspec_escape("[Hello\\["))
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("math", function()
|
||||||
|
it("round()", function()
|
||||||
|
assert.equal(0, math.round(0))
|
||||||
|
assert.equal(10, math.round(10.3))
|
||||||
|
assert.equal(11, math.round(10.5))
|
||||||
|
assert.equal(11, math.round(10.7))
|
||||||
|
assert.equal(-10, math.round(-10.3))
|
||||||
|
assert.equal(-11, math.round(-10.5))
|
||||||
|
assert.equal(-11, math.round(-10.7))
|
||||||
|
assert.equal(0, math.round(0.49999999999999994))
|
||||||
|
assert.equal(0, math.round(-0.49999999999999994))
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
Loading…
Reference in New Issue
Block a user