Percentage of proximity between two integers that can be zero

251 Views Asked by At

I have two numbers, for example $4$ and $5$ and I need to calculate the percentage difference between them, in this case ~$77.8\%$, found by subtracting to $100$ the percentage difference of them:

$$100 - \left(\left(\frac{5-4}{\frac{5+4}{2}}\right) \cdot 100\right)$$

It would generate $$100 - (0.22 \cdot 100) = 100 - 22.2 = 77.8$$

This is fine, but it doesn't work with zero values.

If I need to compare $0$ and $5$ (or $5$ and $0$), the result should be $0$, since they are totally different, but $1$ and $0$ would be a greater number, of course.

How can I deal with them?