Avoid dividing by zero with just variables and basic operators

7.8k Views Asked by At

I am working on stats for a sports team, and one of the stats I have the ratio of Shots and Shots on Target (Which I call SOTP). So, for instance, if a player has 2 shots, and one's on target, their SOTP ratio is 1/2 or 50%.

Now I have a problem that I can't seem to solve. The software that generates the stats lets me put in numbers, variables, or basic operators (+, -, /, *, and **, which represents ^). So for the SOTP I have $SOTP = SHOTSONTARGET/SHOTS$. My problem arises when a player has zero shots since the denominator is zero. If this is the case, the software automatically returns nothing, which works until I need to use the SOTP ratio in another operation. For instance, I have something that generates points based off passes, goals, etc, but when SOTP is equal to nothing, the points return nothing, even if the player has points.

So my question is, is there anything I can do to avoid the zero in the denominator by just using basic operators, constants, and variables?

Thanks for your help.

2

There are 2 best solutions below

0
On BEST ANSWER

Alternative solution:

$$ \text{SOTP} = \frac{\text{shots on target}}{\text{shots} + 0^{\text{shots}}} $$ Also, if you want to set it to be $-1$ when there are no shots (as one of the comments suggested), you could have $$ \text{SOTP} = \frac{\text{shots on target}-0^{\text{shots}}}{\text{shots} + 0^{\text{shots}}} $$

5
On

That is a strange problem, and I feel like that shouldn't happen. Also, how can a player have points without having any shots?

Anyway, here's an easy fix: $$ \text{SOTP} = \frac{\text{shots on target} +.001}{\text{shots} + .001} $$ This will give you approximately the right answer most of the time, and will give you $1$ when $\text{shots} = 0$. Instead of $.001$, you could use any sufficiently small value.