Skip to content

Instantly share code, notes, and snippets.

@Tachytaenius
Created January 7, 2020 17:26
Show Gist options
  • Save Tachytaenius/1a51b28f5beb496228fde923db270c48 to your computer and use it in GitHub Desktop.
Save Tachytaenius/1a51b28f5beb496228fde923db270c48 to your computer and use it in GitHub Desktop.
Get rounding mode in LuaJIT
local ffi = require("ffi")
ffi.cdef("double nextafter(double from, double to);")
local small = ffi.C.nextafter(0, 1)
local modes = {
nearest_toEven = {2, 2, -2, -2},
nearest_truncate = {2, 3, -2, -3},
truncate = {1, 2, -1, -2},
ceiling = {2, 3, -1, -2},
floor = {1, 2, -2, -3}
}
local input = {1.5, 2.5, -1.5, -2.5}
local function getRoundingMode()
for name, results in pairs(modes) do
local this = true
for i = 1, 4 do
if small * input[i] ~= small * results[i] then
this = false
break
end
end
if this then return name end
end
end
print(getRoundingMode())
function love.draw()
love.graphics.print(getRoundingMode())
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment