Hi,
How do I access math.clamp because simply putting it in a lua file doesn't work. It's strange because http://wiki.garrysmod.com/?title=Math says it is part of the standard library but http://www.lua.org/manual/5.1/manual.html#5.6 doesn't have it. I need it for 2 or 3 separate things.
Sorry if the answer is obvious and thanks
math.clamp?
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- bartbes
- Sex machine
- Posts: 4946
- Joined: Fri Aug 29, 2008 10:35 am
- Location: The Netherlands
- Contact:
Re: math.clamp?
It's not part of lua's standard library. (the site you linked is referring to gmod's library, I guess)
Re: math.clamp?
Code: Select all
function math.Clamp(val, lower, upper)
assert(val and lower and upper, "not very useful error message here")
if lower > upper then lower, upper = upper, lower end -- swap if boundaries supplied the wrong way
return math.max(lower, math.min(upper, val))
end
- Taehl
- Dreaming in associative arrays
- Posts: 1025
- Joined: Mon Jan 11, 2010 5:07 am
- Location: CA, USA
- Contact:
Re: math.clamp?
Here's my version (because I like listing first the low number, then the variable, then the high number):
Code: Select all
-- Clamps a number to within a certain range, with optional rounding
function math.clamp(low, n, high) return math.min(math.max(n, low), high) end
Earliest Love2D supporter who can't Love anymore. Let me disable pixel shaders if I don't use them, dammit!
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Lenovo Thinkpad X60 Tablet, built like a tank. But not fancy enough for Love2D 0.10.0+.
Re: math.clamp?
That is practically the same as mine minus error-checking...
- Robin
- The Omniscient
- Posts: 6506
- Joined: Fri Feb 20, 2009 4:29 pm
- Location: The Netherlands
- Contact:
Re: math.clamp?
To be fair, there isn't really another way you would implement this function.vrld wrote: That is practically the same as mine minus error-checking...
Although you could.
To be original:
Code: Select all
function math.clamp(...)
local s = {...}
table.sort(s)
return s[2] --fixed //thelinx
end
Code: Select all
function math.clamp(a, b, c)
return (a < b and b < c and b) or (c < b and b < a and b) or (a < b and c < b and c) or (b < a and a < c and a) or c
end
Help us help you: attach a .love.
Re: math.clamp?
Here's another way.
Note a subtlety... if min is 0 and your value is -0, it will remain -0. If you really want it to become 0 (e.g. could matter for printouts) then change the comparisons like this:
Code: Select all
function clamp(val, min, max)
if val < min then
val = min
elseif max < val then
val = max
end
return val
end
Code: Select all
function clamp(val, min, max)
if val <= min then
val = min
elseif max <= val then
val = max
end
return val
end
Re: math.clamp?
Maybe he was searching, noticed this and didn't notice the topic was so old
EDIT: why don't we turn this topic into a list of original and weird ways to write math.clamp?
EDIT: why don't we turn this topic into a list of original and weird ways to write math.clamp?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
- DeltaF1
- Citizen
- Posts: 64
- Joined: Mon Apr 27, 2015 4:12 pm
- Location: The Bottom of the Stack
- Contact:
Re: math.clamp?
You're onNixola wrote:Maybe he was searching, noticed this and didn't notice the topic was so old
EDIT: why don't we turn this topic into a list of original and weird ways to write math.clamp?
Code: Select all
function math.clamp(val, min, max)
if min - val > 0 then
return min
end
if max - val < 0 then
return max
end
return val
end
Who is online
Users browsing this forum: Ahrefs [Bot] and 9 guests