0

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?

Theraloss
  • 101
  • Avoiding a division by zero, change the definition and replace the denominator by $(|x|+|y|)/2$, see https://en.wikipedia.org/wiki/Relative_change_and_difference#Percentage_change. – Michael Hoppe Mar 07 '18 at 17:09
  • You shouldn't be subtracting from $100$. This would give the percentage difference between two equal numbers as $100%$, which is not what you want. The $22.2$ (aside from rounding) is the ratio of the difference to the average value, which is probably what you want. Comparing any number to $0$ will then give $200%$ because the difference is twice the average. – Ross Millikan Mar 07 '18 at 17:14
  • @Ross Ok... The goal is have the percentage more near 100% as the numbers are more equal. For example "5" and "5" = 100%, "1" and "5" = 20%, "4" and "5" = 77% (about?). For this reason I subtract the result to 100, if you have any idea please share :) – Theraloss Mar 08 '18 at 08:33
  • @MichaelHoppe isn't it already (x + y) / 2? – Theraloss Mar 08 '18 at 18:26
  • Not in case that $x=-y$. – Michael Hoppe Mar 08 '18 at 19:07

0 Answers0