Take the 2-minute tour ×
Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Im designing a website and I need to make a sum which will allow the result to be a maximum of 500. So the variable can be anything from '0' to 'a billion' and second number is 12. So just by basic math I would like to always get either 500 or less and never more than 500. Can this be done by basic math?

Example variable x/x * 500 will always give 500 (without 12), so that is wrong.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

Well, there are tons and tons of functions that never go higher than 500. One of them is $f(x) = \min (x, 500)$ (in Javascript, Math.min(x, 500)). This function simply gives you either $x$ or 500, whichever is smaller.

We might be able to "build" a function that behaves more nicely. The function $e^x$ is never less than 0, which means that $-e^x$ is never more than 0, which means that $500 - e^x$ is never more than 500. But if you look at a graph of this function, you'll see that as $x$ gets larger, $500 - e^x$ gets smaller.

We can make things go the other way around, though. We know that as $x$ gets larger, $-x$ gets smaller, and that as $-x$ gets smaller, $500 - e^{-x}$ gets larger. So, as $x$ gets larger, $500 - e^{-x}$ gets larger. Here's a graph of this function. In Javascript, that function is 500 - Math.exp(-x).

The thing is, if $x = 0$, then $500 - e^{-x} = 499$, so this function doesn't do a great job of leaving small values of $x$ alone. After a bit of experimenting, I've come up with the function $f(x) = 500 (1 - e^{-x/500})$ (in Javascript, 500 * (1 - Math.exp(-x/500))). For small values of $x$, $f(x)$ is approximately $x$, but for large values of $x$, $f(x)$ is approximately 500.

share|improve this answer
    
Thanks, this is what I was looking for. I found a better way of doing things but I will keep this in mind just in case. –  Youss Feb 12 '12 at 20:39
    
You might also want to consider a logistic curve: en.wikipedia.org/wiki/Logistic_function –  Ivan Andrus Feb 12 '12 at 22:01

How about $$ f(x) = \frac{x + 12}{10^9 + 12} \cdot 500? $$ ($10^9$ is a billion). Am I understanding you correctly?

share|improve this answer
    
You could be right but I have to implement this in my javascript (could take a while..) –  Youss Feb 12 '12 at 15:08
    
I have put this down 500*(variable+12)/(1000000000+12) but chanching variable sometimes gives me more then max 500...(mayby Im writing it wrong..?) –  Youss Feb 12 '12 at 15:21
    
Looks allright to me. You should not be getting results $> 500$, provided variable <= 1000000000. Do you have any concrete inputs that result in a value strictly larger than $500$? –  Martin Wanvik Feb 12 '12 at 15:55
    
No, you are right. But I was mistaken..there is a lot more variables to consider. But anyway I found much simpler method. However your answer was good. Thanks for the help:) –  Youss Feb 12 '12 at 20:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.