1
$\begingroup$

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.

$\endgroup$

2 Answers 2

2
$\begingroup$

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.

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Youss
    Feb 12, 2012 at 20:39
  • $\begingroup$ You might also want to consider a logistic curve: en.wikipedia.org/wiki/Logistic_function $\endgroup$ Feb 12, 2012 at 22:01
1
$\begingroup$

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

$\endgroup$
4
  • $\begingroup$ You could be right but I have to implement this in my javascript (could take a while..) $\endgroup$
    – Youss
    Feb 12, 2012 at 15:08
  • $\begingroup$ 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..?) $\endgroup$
    – Youss
    Feb 12, 2012 at 15:21
  • $\begingroup$ 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$? $\endgroup$ Feb 12, 2012 at 15:55
  • $\begingroup$ 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:) $\endgroup$
    – Youss
    Feb 12, 2012 at 20:24

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .