# Avoid dividing by zero with just variables and basic operators

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?

-
Can't you just set SOTP=-1 when SHOTS=0? – Shahab Feb 13 '14 at 1:57
Check when the denominator is $0$, and set the variable to some special value. That's the standard thing. – ShreevatsaR Feb 13 '14 at 2:01
How would I do that? I can only use operators, and not IF() statements, etc. However, I can do $0^0$ and get one. Is there some way I could use that? If I do $SOTP = Shotsontarget/shots^shots$, I get $0$ if shots is zero (since $0/0^0$ somehow equals $0/1$ to the software), but then if shots is two or something, the result is wrong. – davidsbro Feb 13 '14 at 2:02

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.

-
Thanks. That actually works well because the program rounds. The only thing I did differently was this $SOTP = Shots on target/(shots + 0.0001)$ since I need $SOTP$ to be equal to zero if shots is zero. But thanks. That's much easier that what I was trying to do, which was raise $shots$ to the zero if $shots$ was zero, and otherwise raise $shots$ to the one if $shots$ was one or greater (I'd be really impressed if there were some way to do this). – davidsbro Feb 13 '14 at 2:28
Oh, btw, a player can have points if they have passes, tackles, etc. :) – davidsbro Feb 13 '14 at 2:30
I thought of another solution using the $0^0 = 1$ bit, if you're interested. See the latest edit. – Omnomnomnom Feb 13 '14 at 2:50
Wow, thanks! Now I don't have to worry about the rounding. Wish I could +2 ya! Actually, if you wanted to, it's fine with me if you put that as another answer, since it's not the same as the original. – davidsbro Feb 13 '14 at 2:54
Fair enough, and glad to help. – Omnomnomnom Feb 13 '14 at 2:55

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

-