0
$\begingroup$

I want to create a formula that only applies the additive inverse for negative values because I am trying to come up with a simple formula whereby two numbers are entered and if the second is larger the difference is the result but if the second is smaller then zero is the result.

where x=200 and y=100 then result=0

where x=200 and y=300 then result=100

For reference this is part of a score calculator for a game and I could just inspect with an IF but my pride will not let me mostly because I would like to be able to express the whole system in terms of a single mathematical expression.

Update:

While writing this question I came up with a possible answer.

My thinking has led me towards the use of squares. Something like:

$$ \frac{(y-x)+\sqrt{(y-x)^2}}{2} $$

Is this the best solution for this problem or have I made things too complicated for no reason?

$\endgroup$
  • 1
    $\begingroup$ Are you allowing yourself to use something like $\max$? In that case, $\max\{y-x,0\}$ is what you want. Otherwise, perhaps you could be a little more specific about what you're allowed to use (like, for example, only polynomial equations) $\endgroup$ – Hayden Feb 2 '15 at 14:23
  • $\begingroup$ To be honest it's my project so I could use anything that PHP has available but I would like to post a purely mathematical explanation for score on the help page. $\endgroup$ – Matthew Brown aka Lord Matt Feb 2 '15 at 14:25
1
$\begingroup$

I would suggest $\max\{y-x,0\}$. The idea is that $y-x\geq 0$ if and only if $y\geq x$, so either it gives you $y-x$ in the situation you want, or it simply returns $0$.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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