0

I would like to know what is the best solution to find the percentage change of a double compared to another one. For example, I have a variable called 'current', I want to compare it to a variable called 'previous'. I need this for statistic purposes so that I can analyze the growth or the loss of value. I wrote my own algorithm, but it's very basic and it includes 2 if(s), which makes it too programming language dependent. Ideally I would like to find a one-line calculation.

double current = 10;
double previous = 20;
double result = 0.0;
// growth
if (current > previous)
{
  result = (100 / current) * previous;
}
// loss
if (current < previous)
{
  result = (100 / previous) * current;
}

Do you have any advice?

gelichor
  • 126
JBoy
  • 101
  • How about $100*(current+previous)/\max(previous,current)-100$? Note that $\max$ can be emulated using $|.|$, but that may not necessarily be faster than an if. (BTW, your code fails if $current == previous$) – Hagen von Eitzen Feb 13 '15 at 09:22
  • 1
    Your terminology seems odd to me - you're looking to find the percentage of the smaller number relative to the larger number right? In this case $\frac{\mathrm{min}(\text{previous,current})}{\mathrm{max}(\text{previous,current})}\times 100$ should work. – Dom Feb 13 '15 at 09:30
  • Hi @HagenvonEitzen thx for the advice, i will try it right now, well, i dont think it fails, if current==previous none of the ifs will be true, result will remain 0.0, which is correct because if current==previous there is no growth or loss, btw your algorithm returns 100% in the case (current==previous), ehich is not really what i would expect – JBoy Feb 13 '15 at 09:32
  • Just like @Dom I don't understand what is the base of your percentage? The lowest number or the first number? I just think that finding $%$-difference between two number without knowing the base makes no sense. – Andrei Rykhalski Feb 13 '15 at 09:51
  • Ok, i my bad, i was probably too superficial in my explanation, i have edited the initial post. i hope it helps, thx – JBoy Feb 13 '15 at 10:29
  • Possibly helpful: https://math.stackexchange.com/questions/2417020/what-is-the-difference-between-ratio-change-vs-percent-change-finance-questio/2417032#2417032 – Ethan Bolker Apr 11 '19 at 02:08

1 Answers1

2

If you're looking for the percentage loss/gain and 'compare current to previous' I would suggest that you want the denominator in your fraction to always be the previous value. This would make the value more consistent from a heuristic perspective, as it will always tell you the fraction relative to what you had, rather than changing behavior depending on whether you have loss or gain. Then you could with $\frac{\text{current}}{\text{previous}}\times 100$ for the percentage of 'previous' that 'current' is, or if you want the percentage change that would be $\frac{\text{current-previous}}{\text{previous}}\times100$.

This will give you a consistent interpretation of what the number means, which you don't get if you have that those if tests. Of course, if your doing some specific statistical analysis there could be much more to this story.

Dom
  • 1,378