math.clamp?

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Codiak
Prole
Posts: 12
Joined: Mon Aug 30, 2010 4:05 pm

math.clamp?

Post 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 :)
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: math.clamp?

Post by bartbes »

It's not part of lua's standard library. (the site you linked is referring to gmod's library, I guess)
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: math.clamp?

Post 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.
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Taehl
Dreaming in associative arrays
Posts: 1025
Joined: Mon Jan 11, 2010 5:07 am
Location: CA, USA
Contact:

Re: math.clamp?

Post 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
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+.
User avatar
vrld
Party member
Posts: 917
Joined: Sun Apr 04, 2010 9:14 pm
Location: Germany
Contact:

Re: math.clamp?

Post by vrld »

:ehem: That is practically the same as mine minus error-checking...
I have come here to chew bubblegum and kick ass... and I'm all out of bubblegum.

hump | HC | SUIT | moonshine
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: math.clamp?

Post 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
Help us help you: attach a .love.
mlepage
Prole
Posts: 10
Joined: Sat Jun 30, 2012 9:26 pm

Re: math.clamp?

Post 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
User avatar
veethree
Inner party member
Posts: 877
Joined: Sat Dec 10, 2011 7:18 pm

Re: math.clamp?

Post by veethree »

You bumped a 5 year old thread for that?
User avatar
Nixola
Inner party member
Posts: 1949
Joined: Tue Dec 06, 2011 7:11 pm
Location: Italy

Re: math.clamp?

Post 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?
lf = love.filesystem
ls = love.sound
la = love.audio
lp = love.physics
lt = love.thread
li = love.image
lg = love.graphics
User avatar
DeltaF1
Citizen
Posts: 64
Joined: Mon Apr 27, 2015 4:12 pm
Location: The Bottom of the Stack
Contact:

Re: math.clamp?

Post 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
Post Reply

Who is online

Users browsing this forum: Amazon [Bot], Google [Bot] and 17 guests