2025-01-04 23:00:19 +08:00

29 lines
765 B
Lua

---clamp
---@param v number @number
---@param Min number @min
---@param Max number @max
---@return number @clamp v between Min and Max
function math.clamp(v, Min, Max)
Min = math.min(Min, Max)
Max = math.max(Min, Max)
if v < Min then
return Min
end
if v > Max then
return Max
end
return v
end
---clamp
---@param a number @First number to compare
---@param b number @Second number to compare
---@param Tolerance number @Maximum allowed difference for considering them as 'nearly equal'
---@return boolean @true if a and b are nearly equal
function math.isNearlyEqual(a, b, Tolerance)
if Tolerance == nil then
Tolerance = 0.01
end
return math.abs(a - b) <= Tolerance
end