The additive inverse for negative values only (otherwise zero)

106 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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$.