-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmathx.lua
More file actions
73 lines (62 loc) · 1.52 KB
/
mathx.lua
File metadata and controls
73 lines (62 loc) · 1.52 KB
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
math.atan2 = math.atan2 or math.atan
math.pow = math.pow or function(a, b)
return a ^ b
end
math.log10 = math.log10 or function(a)
return math.log(a, 10)
end
math.ldexp = math.ldexp or function(x, exp)
return x * 2.0 ^ exp
end
math.frexp = math.frexp or function(x)
return auxFunc('frexp', x)
end
math.mod = math.mod or math.fmod
math.hypot = math.hypot or function(...)
local sum = 0
for _, x in ipairs {...} do sum = sum + x * x end
return math.sqrt(sum)
end
math.sign = math.sign or function(x)
if x >= 0 then
return 1
else
return -1
end
end
function math.random2(lower, upper)
-- same as math.random, but each script has its own generator
local r = auxFunc('rand')
if lower then
local b = 1
local d
if upper then
b = lower
d = upper - b
else
d = lower - b
end
local e = d / (d + 1)
r = b + math.floor(r * d / e)
end
return r
end
function math.randomseed2(seed)
-- same as math.randomseed, but each script has its own generator
auxFunc('randseed', seed)
end
math.randomseed = wrap(math.randomseed, function(origFunc)
return function(a, ...)
return origFunc(math.floor(a), ...) -- in Lua 5.4 only integer accepted
end
end)
function math.median(t)
local n = #t
if n == 0 then return nil end
local s = table.sorted(t)
if n % 2 == 1 then
return s[(n + 1) // 2]
else
return 0.5 * (s[n // 2] + s[n // 2 + 1])
end
end