-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath.lua
More file actions
26 lines (14 loc) · 745 Bytes
/
math.lua
File metadata and controls
26 lines (14 loc) · 745 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
-- Gets the smallest power of two number (2^x) that is greater than or equal to the input number 'n'.
local function nextPowerOfTwo(n) -- For Lua 5.1.
n = math.floor(n) -- Make sure it's an integer.
if n <= 1 then return 1 end -- Edge cases: 0 and 1
local p = 1 while p < n do p = p * 2 end return p
end
local function safeDivision( a, b ) return b ~= 0 and a / b or 0 end
local function safeModulo( a, b ) return b ~= 0 and a % b or 0 end
local function isEven(a) return a % 2 == 0 end
local function isOdd(a) return isEven( a + 1 ) end
return {
nextPowerOfTwo = nextPowerOfTwo, safeDivision = safeDivision, safeModulo = safeModulo,
isEven = isEven, isOdd = isOdd
}