• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 408
  • Last Modified:

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;
}

Open in new window


Please can someine tell me if this is ok??  Is there a better way to do this??
0
Wanting2LearnMan
Asked:
Wanting2LearnMan
  • 3
  • 2
  • 2
  • +5
10 Solutions
 
morpheiosCommented:
Try multiply first, divide last - it will give smaller error of rounding
0
 
Wanting2LearnManAuthor Commented:
ok I'll do that, apart from that, is it ok??
0
 
eXpeLLeD_4RM_heLLCommented:
if( secondNumber == 0 || firstNumber == 0 )
          return -1;

shouldnt this return 100 as the percentage is of 100% difference
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
eXpeLLeD_4RM_heLLCommented:
Other than that it looks OK
0
 
ozoCommented:
(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
 
Wanting2LearnManAuthor 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
 
mcs0506Commented:
yes It's OK. Percentage difference is calculated as per formula.


Regards

Dani
0
 
ozoCommented:
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
 
ozoCommented:
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
 
vk3kjcCommented:
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
 
AndyAinscowCommented:
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
 
George TokasCommented:
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now