calculate the % difference between two numbes

Oct 27, 2011 at 10:57am
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:

1
2
3
4
5
6
7
8
9
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;
} 


Oct 27, 2011 at 11:55am
I have a few comments.

First, returning -1 in the failure case is not the best plan, since it's possible to have a valid result of -1. There are a few ways to address this problem. One is by throwing an exception, another is by also returning a separate boolean status of the operation.

Second, you probably want to return a double rather than an int to retain the precision of the calculation.
Oct 27, 2011 at 12:25pm
Er, the percent difference between zero and zero is zero.

Your formula looks off. I think you are calculating "percent error"?

Also, the percent difference is always a non-negative number, unless you are talking about something else?

Oct 27, 2011 at 11:33pm
you'r calculations are wrong and the return function is flawed. to correctly calculate the percent difference between two numbers, you must first find the difference (hence percent DIFFERENCE, which means by what percentage they differ) betweend your two numbers. Not to mention you dont include user input in your code. I would also recomend you not over complicate things by useing classes, as functions are a lot easier to handle.

run this as a .cpp file in your IDE:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main()
{
    float one, two, tot, percent, diff;
    cout<< "First number: ";
    cin>> one;
    cout<< ""<< endl;
    cout<< "Second number: ";
    cin>> two;
    cout<< ""<< endl;
    tot = (one + two);
    diff = (one - two);
    percent = ((diff / (tot)) * 100);
    if (percent < 0)
    {
        percent = ((percent - percent) - percent);
    }
    cout<< "Percent difference: "<< percent<< endl;
    cout<< "press enter to continue...";
    cin.get();
    cin.get();
    return 0;
}


notice how when you enter a greater number before a smaller number, it comes positive anyway. Also note that i use no classes at all. Life's simpler this way...
Oct 28, 2011 at 12:21am
@IWishIKnew
Your calculations are also incorrect, and you don't need to give the OP a hard time for the structure of his code when it is not wrong -- his choice of a function is generally a much better decision than inlining the code for a separable calculation.

Percent Difference and related measures are very well explained here (actually linked to by the OP's reference article):
http://www.mathsisfun.com/data/percentage-difference-vs-error.html

At no point should the denominator be the sum of inputs. Also, lines 16-19 make no sense.

Be careful when you criticize others' code or methodologies -- you very likely don't understand everything there is to know about it -- all you know is what the OP posted about a specific piece of it.
Nov 2, 2011 at 1:21am
16-19 are to prevent negative outputs by the program. if variable 'percent' is less than zero, it is negative, and percents arent negative, so we make it positive.

Also, i tried to answer to the best of my abilities, and that's about all I can do. ^^ I hope I helped somewhat.
Last edited on Nov 2, 2011 at 1:22am
Nov 2, 2011 at 1:41am
Use the unary negation operator to flip the sign on a number.

16
17
18
    if (percent < 0)
        percent = -percent;
    cout << "Percent difference: " << percent << endl;

You can also use the abs() function:

16
17
    cout << "Percent difference: " << abs( percent ) << endl;
Topic archived. No new replies allowed.