Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: ejonesI just ran across a situation where a little more work than this was needed when dealing with division by zero. I was dealing with two values, a and b, where I was calculating a rate of difference between them. To avoid division by zero I defined the rate as (a - b) / average(a,b) but this wasn't 100% successful. (average(a,b) means (a + b)/2
- There were many cases where both a and b were zero which leads to need to report a result of zero instead of NULL. I tend to use NULL to report infinity when I can't otherwise represent infinity.
- But there were a few cases where for example, a = 10 and b = -10. The average(a,b) results in zero but zero isn't a valid answer in this context, zero would indicate there was no difference between a and b and that isn't correct since (a - b) is not zero. But a real value is preferable over NULL since there is a real difference between the a and b values that should be reported.
What I did in this case is add 0.01 to b and get a value, and then subtracted 0.01 from the original b and calculated a value, then averaged the two values. For example:
(10 - (-10.01))/((10 + (-10.01)) * 1/2) = 20.01 / -0.005 = -4002
(10 - (-9.99)/((10 + (-9.99)) * 1/2) = 19.99 / 0.005 = 3998
Then I averaged the two results. average(-4002,3998) = (3998 - 4002)/2 = -4/2 = -2
So in this case -2 is a better answer than NULL.
While writing this down and looking at the above I'm realizing that maybe the formula should instead be (a - b)/b. So it is only when b is zero we have an issue. If a is also zero then the valid result should be zero. But maybe not for other cases, for example what about a = 10 and b = 0? Testing values where b is near zero indicates that the result approaches infinity. This means that if a is non-zero the answer should be infinity and positive or negative infinity depending on the sign of a.
And now that I've realized this I've decided to try an approach that is almost the same as the earlier approach of averaging a and b in the denominator but with the slight modification of taking the absolute value of a and b separately in the denominator. So (a - b) / average(abs(a),abs(b)) = (a - b) / ((abs(a) + abs(b)) * 1/2)
Most cases where a = b it is zero but what about where a and b are both zero? To answer this I tried values where either a or b are near zero.
a = 0.01 and b= 0 results in 2. And a = -0.01 and b = 0 results in -2 averaging those two results in 0. Same thing happens when you do this with b leaving a at zero. So unless I'm mistaken, I can now say that even in the case where a = 0 and b = 0, that if a = b the result should be zero. (A real mathematics teacher might see something I'm missing) This last formula for a difference rate seems to have fewer issues, but I'm not yet ready to say it is an improvement over the previous two formulas since I suspect context matters. And also I need to see how it performs with the data before I can say it is even correct in the current context.