# calculate the % difference between two numbes.

I want a function that returns the the % differnece between two numbers.
I got the formula from this website:
http://www.ehow.co.uk/how_6331196_calculate-percent-difference.html
This is my function:

``````int MyClass::CalculatePercentageDifference( uint64_t firstNumber, uint64_t secondNumber ) const
{
if( secondNumber == 0 || firstNumber == 0 )
return -1;

double percentDiff = ((((double)secondNumber - (double)firstNumber) / (double)firstNumber) * 100);

return (int)percentDiff;
}
``````

Please can someine tell me if this is ok??  Is there a better way to do this??
###### Who is Participating?

Commented:
Try multiply first, divide last - it will give smaller error of rounding
0

Author Commented:
ok I'll do that, apart from that, is it ok??
0

Commented:
if( secondNumber == 0 || firstNumber == 0 )
return -1;

shouldnt this return 100 as the percentage is of 100% difference
0

Commented:
Other than that it looks OK
0

Commented:
(secondNumber - firstNumber)*100.0/firstNumber
is one kind of percentage difference
(secondNumber - firstNumber)*100.0/secondNumber
is another
(secondNumber - firstNumber)*200.0/(firstNumber+secondNumber)
is a more symmetric kind of difference
abs(secondNumber - firstNumber)*100.0/max(abs(firstNumber),abs(secondNumber))
may be an appropriate kind of percentage difference in other cases
What is the application of this function?
0

Author Commented:
re: What is the application of this function?
This function will accept a number and return that same number increased or decreased by a certain percentage.

re: shouldnt this return 100 as the percentage is of 100% difference.
If any of the numbers are 0 I want to ignore the result.
0

Commented:
yes It's OK. Percentage difference is calculated as per formula.

Regards

Dani
0

Commented:
This function will accept a number and return that same number increased or decreased by a certain percentage.
That sounds like the opposite of what CalculatePercentageDifference does
What you describe sounds like it would take firstNumber and percentDiff  as parameters and return secondNumber as a result.
0

Commented:
If any of the numbers are 0 I want to ignore the result.
does that mean that
CalculatePercentageDifference(100,99)
CalculatePercentageDifference(100,0)
CalculatePercentageDifference(0,99)
are all considered equivalent?

0

Commented:
Don't forget that there will be two different answers. The percentage that a smaller number is less than a larger number is not the same as the percentage that a larger number is greater than a smaller number.

If you have two numbers - 95 and 100 - 95 is 5 percent less than 100 - but 100 is not 5 percent greater than 95. It is 5.263 percent greater.

0

Freelance programmer / ConsultantCommented:
I think you have a major problem with that formula.

Consider the following two where n is any integer number
CalculatePercentageDifference(0, n);
CalculatePercentageDifference(n, 0);

the return of both of these is -1.  (Which you appear to use to show an invalid value being passed in.)

Now consider eg.
CalculatePercentageDifference(100, 99);
The return value is -1.
0

Commented:
Hi there,
The webpage you have as reference indicates that you followed almost the right way...
One small addition in case of dividing by zero error or other errors:
1. As the webpage says the difference will be good to report a double value...
2. In case of error it will be better to have a boolean return value.

bool MyClass::CalculatePercentageDifference( uint64_t firstNumber, uint64_t secondNumber double &diff) const
{
if( secondNumber == 0 || firstNumber == 0 )
return false;

diff = ((((double)secondNumber - (double)firstNumber) / (double)firstNumber) * 100);

return true;
}

George Tokas.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.