Created
January 7, 2020 17:26
-
-
Save Tachytaenius/1a51b28f5beb496228fde923db270c48 to your computer and use it in GitHub Desktop.
Get rounding mode in LuaJIT
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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