# Formula to limit a number within a minimum and maximum value

I'm looking for a formula that can limit a specific input within a specific range.

I want to limit a number within 0 and 100. Example:

• Input: -100 / Output: 0
• Input: -1 / Output: 0
• Input: 0 / Output: 0
• Input: 10 / Output: 10
• Input: 50 / Output: 50
• Input: 100 / Output: 100
• Input: 101 / Output: 100
• Input: 150 / Output: 100

I really only know the basics of math. I'm not even sure what to search to see if this question was already asked.

Is this possible? I'm a developer/programmer so I could use conditions but I'm wondering if there's a formula I could use instead.

-
I always use $\min(\max(x,0),100)$, which has a nice cadence to it. – Rahul Feb 19 '13 at 5:04
So simple — I love it. thanks! codepad.org/eGBQp1Il – iDev247 Feb 19 '13 at 7:43

$$f(x)={\rm median}(\{{0,x,100\}})$$

-
Works great! Thanks! codepad.org/T1ACyHdl – iDev247 Feb 19 '13 at 7:47

if x<0 return 0

else if x>100 return 100

else return x

We get a number of questions wanting to avoid if statements, which I don't understand. Why?

-
I think there's a certain desire when coding to use exceptionally elegant constructions. Speaking from personal experience I recall being very satisfied when I could come up with a one line formula that did what a four line elseif statement would have done. It's probably a bad habit. The only example I have on hand is an old stackoverflow question of mine. Of course here being overly clever meant more code, rather than less. – JSchlather Feb 19 '13 at 5:18
@JacobSchlather: my poster child is asking how to return 0 if x=0, otherwise 1 without an if statement. Another is return the sign of x without an if. Why? Why is x/abs(x) better than if x>0... (and you have a problem if x=0)? – Ross Millikan Feb 19 '13 at 5:23
An if statement is probably how one should code it. It's the most readable and easiest to think of. But it's boring I think is the main issue. For instance to return the sign of an int $x$ one could do a bitwise XOR with the sign bit of an int32 and then return that. Stuff like this comes from the hacker community. It's why people try to see how few lines of code they can write to output the 12 days of christmas. – JSchlather Feb 19 '13 at 5:31
The accepted answer is one line, yours are 3. Also you repeat x 3 times (it could be a longer expression). It can become unreadable very quickly. – David Balažic Nov 21 '15 at 1:48

I think the best answer here is Ross's but I was a little bit bored and wanted to see if I could write a formula in c++ to compute your function. As it turns out I could, but you probably shouldn't use this. The following will give you the truncation of your number. The main point here is that bitwise anding with 0x80000000 will give you zero if your number is negative and a positive number otherwise.

(!(num & 0x80000000)*( (!((num-101)&0x80000000))*100+(!!((num-101)&0x80000000))*num))

-
Awesome! My solution (which I didn't like) before asking this question was: <?php echo $progress > 100 ? 100 :$progress < 1 ? 1 : round(\$progress); ?> – iDev247 Feb 19 '13 at 7:50