Page 1 of 2

math.clamp?

Posted: Fri Sep 03, 2010 11:59 am
by Codiak
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 :)

Re: math.clamp?

Posted: Fri Sep 03, 2010 12:36 pm
by bartbes
It's not part of lua's standard library. (the site you linked is referring to gmod's library, I guess)

Re: math.clamp?

Posted: Fri Sep 03, 2010 12:37 pm
by vrld

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
First two lines are just for error checking and can be omitted if you have a lot of self esteem.

Re: math.clamp?

Posted: Fri Sep 03, 2010 5:09 pm
by Taehl
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

Re: math.clamp?

Posted: Fri Sep 03, 2010 6:08 pm
by vrld
:ehem: That is practically the same as mine minus error-checking...

Re: math.clamp?

Posted: Fri Sep 03, 2010 6:42 pm
by Robin
vrld wrote: :ehem: That is practically the same as mine minus error-checking...
To be fair, there isn't really another way you would implement this function.

Although you could.

To be original:

Code: Select all

function math.clamp(...)
    local s = {...}
    table.sort(s)
    return s[2] --fixed //thelinx
end
and

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

Re: math.clamp?

Posted: Mon Apr 27, 2015 3:34 pm
by mlepage
Here's another way.

Code: Select all

function clamp(val, min, max)
	if val < min then
		val = min
	elseif max < val then
		val = max
	end
	return val
end
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

Re: math.clamp?

Posted: Mon Apr 27, 2015 6:54 pm
by veethree
You bumped a 5 year old thread for that?

Re: math.clamp?

Posted: Mon Apr 27, 2015 6:58 pm
by Nixola
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?

Re: math.clamp?

Posted: Thu Jun 25, 2015 1:39 am
by DeltaF1
Nixola 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?
You're on

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