-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmathLib.lua
102 lines (67 loc) · 1.48 KB
/
mathLib.lua
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
local t = {}
local function log10(x)
return math.log(x, 10)
end
--t.log10 = log10
local QUARTER_ANGLE = 1.57
t.QUARTER_ANGLE = QUARTER_ANGLE
local function cutFloat(a, decPlaces)
if (decPlaces == nil) then
decPlaces = 2
end
local factor = math.pow(10, decPlaces)
return (math.floor(a * factor) / factor)
end
t.cutFloat = cutFloat
local function isInt(val)
if (type(val) ~= 'number') then
return false
end
return (math.floor(val) == val)
end
t.isInt = isInt
local function hex2dec(val)
assert(val, 'no val')
return tonumber(val, 16)
end
t.hex2dec = hex2dec
local function dec2hex(val, places)
assert(val, 'no val')
if (places == nil) then
places = 0
end
local res = string.format("%X", val)
if (res:len() < places) then
res = string.format('%s%s', string.rep('0', places - res:len()), res)
end
return res
end
t.dec2hex = dec2hex
local function countDigits(val)
assert(val, 'no value')
return math.floor(math.log10(val))
end
t.countDigits = countDigits
local function setDigits(val, digits)
assert(val, 'no value')
assert(digits, 'no digits')
return string.rep('0', digits - countDigits(val))..val
end
t.setDigits = setDigits
local pow2Table = {}
for i = -256, 256, 1 do
pow2Table[i] = math.pow(2, i)
end
local function pow2(i)
return pow2Table[i]
end
t.pow2 = pow2
local pow256Table = {}
for i = 0, 4, 1 do
pow256Table[i] = math.pow(256, i)
end
local function pow256(i)
return pow256Table[i]
end
t.pow256 = pow256
moduleLib.expose('math', t)