z_mirror/flib

git clone https://git.raiguard.me/z_mirror/flib

Caleb HeuerRemove upstream math functions from math moduled80f6ce

trunk
4.3 KiB155 linesraw
1--- Additional mathematical utilities.
2--- ```lua
3--- local flib_math = require("__flib__.math")
4--- ```
5--- @class flib_math
6local flib_math = {}
7
8--- Multiply by degrees to convert to radians.
9--- ```lua
10--- local rad = 1 x flib_math.deg_to_rad -- 0.0174533
11--- ```
12flib_math.deg_to_rad = math.pi / 180 --- @type number
13
14--- Multiply by radians to convert to degrees.
15---
16--- ```lua
17--- local deg = 1 x flib_math.rad_to_deg -- 57.2958
18--- ```
19flib_math.rad_to_deg = 180 / math.pi --- @type number
20
21--- One radian.
22flib_math.radian = 2 * math.pi
23
24flib_math.max_double = 0X1.FFFFFFFFFFFFFP+1023
25flib_math.min_double = -0X1.FFFFFFFFFFFFFP+1023
26flib_math.max_int8 = 127 --- 127
27flib_math.min_int8 = -128 --- -128
28flib_math.max_uint8 = 255 --- 255
29flib_math.max_int16 = 32767 --- 32,767
30flib_math.min_int16 = -32768 --- -32,768
31flib_math.max_uint16 = 65535 --- 65,535
32flib_math.max_int = 2147483647 --- 2,147,483,647
33flib_math.min_int = -2147483648 --- -2,147,483,648
34flib_math.max_uint = 4294967295 --- 4,294,967,295
35flib_math.max_int53 = 0x1FFFFFFFFFFFFF --- 9,007,199,254,740,991
36flib_math.min_int53 = -0x20000000000000 --- -9,007,199,254,740,992
37
38--- Round a number to the nearest multiple of divisor.
39--- Defaults to nearest integer if divisor is not provided.
40---
41--- From [lua-users.org](http://lua-users.org/wiki/SimpleRound).
42--- @param num number
43--- @param divisor? number `num` will be rounded to the nearest multiple of `divisor` (default: 1).
44--- @return number
45function flib_math.round(num, divisor)
46  divisor = divisor or 1
47  if num >= 0 then
48    return math.floor((num / divisor) + 0.5) * divisor
49  else
50    return math.ceil((num / divisor) - 0.5) * divisor
51  end
52end
53
54--- Ceil a number to the nearest multiple of divisor.
55--- @param num number
56--- @param divisor? number `num` will be ceiled to the nearest multiple of `divisor` (default: 1).
57function flib_math.ceiled(num, divisor)
58  if divisor then
59    return math.ceil(num / divisor) * divisor
60  end
61  return math.ceil(num)
62end
63
64--- Floor a number to the nearest multiple of divisor.
65--- @param num number
66--- @param divisor? number `num` will be floored to the nearest multiple of `divisor` (default: 1).
67function flib_math.floored(num, divisor)
68  if divisor then
69    return math.floor(num / divisor) * divisor
70  end
71  return math.floor(num)
72end
73
74--- Returns the argument with the maximum value from a set.
75--- @param set number[]
76--- @return number
77function flib_math.maximum(set)
78  return math.max(table.unpack(set))
79end
80
81--- Returns the argument with the minimum value from a set.
82--- @param set number[]
83--- @return number
84function flib_math.minimum(set)
85  return math.min(table.unpack(set))
86end
87
88--- Calculate the sum of a set of numbers.
89--- @param set number[]
90--- @return number
91function flib_math.sum(set)
92  local sum = set[1] or 0
93  for i = 2, #set do
94    sum = sum + set[i]
95  end
96  return sum
97end
98
99--- Calculate the mean (average) of a set of numbers.
100--- @param set number[]
101--- @return number
102function flib_math.mean(set)
103  return flib_math.sum(set) / #set
104end
105
106--- Calculate the mean of the largest and the smallest values in a set of numbers.
107--- @param set number[]
108--- @return number
109function flib_math.midrange(set)
110  return 0.5 * (flib_math.minimum(set) + flib_math.maximum(set))
111end
112
113--- Calculate the range in a set of numbers.
114--- @param set number[]
115--- @return number
116function flib_math.range(set)
117  return flib_math.maximum(set) - flib_math.minimum(set)
118end
119
120--- Clamp a number between minimum and maximum values.
121--- @param x number
122--- @param min? number default 0
123--- @param max? number default 1
124--- @return number
125function flib_math.clamp(x, min, max)
126  x = x == 0 and 0 or x -- Treat -0 as 0
127  min, max = min or 0, max or 1
128  return x < min and min or (x > max and max or x)
129end
130
131--- Return the signedness of a number as a multiplier.
132--- @param x number
133--- @return number
134function flib_math.sign(x)
135  return (x >= 0 and 1) or -1
136end
137
138--- Linearly interpolate between `num1` and `num2` by `amount`.
139---
140--- The parameter `amount` is clamped between `0` and `1`.
141---
142--- When `amount = 0`, returns `num1`.
143---
144--- When `amount = 1`, returns `num2`.
145---
146--- When `amount = 0.5`, returns the midpoint of `num1` and `num2`.
147--- @param num1 number
148--- @param num2 number
149--- @param amount number
150--- @return number
151function flib_math.lerp(num1, num2, amount)
152  return num1 + (num2 - num1) * flib_math.clamp(amount, 0, 1)
153end
154
155return flib_math