Percentage difference of two numbers?

In summary, the conversation discusses different methods for determining the percentage difference between two numbers and the preferred method is to use (max(a,b)/min(a,b) <= 1.0001 && min(a,b)/max(a,b) >= 0.9999). The conversation also addresses the issue of negative numbers and how to handle them in the calculation.
  • #1
pivoxa15
2,255
1
I am doing a computer science course and one exercise is to show that two numbers are within 0.01% of each other.

The first step is to ask how one would define the percentage difference between two numbers?

I would define it as:
[(larger number – smaller number)/ (|the number midway between the two numbers|)] * 100

I think this way is better compared with other ways such as

1. [(larger number – smaller number)/(|smaller number|)] *100
or
2. [(larger number – smaller number)/(|larger number|)] * 100

Mainly because my way allows one to get the same percentage difference between for example, (4,6) and (-4,-6). While the other two ways does not.

Using 1.
(6 – 4)/4 = 50%
(-4 – -6)/|-6| = 33%

Using 2.
(6 – 4)/6 = 33%
(-4 – -6)/|-4| = 50%

Preferred Method.
(6 – 4)/5 = 40%
(-4 – -6)/|-5| = 40%

Do you have any other suggestions?

Thanks
 
Physics news on Phys.org
  • #2
Presumed that a, b are non-zero and both either positive or negative (at the same time). Then this should do it:

return (max(a,b)/min(a,b) <= 1.0001 && min(a,b)/max(a,b) >= 0.9999);
 
  • #3
That was a good idea but in my case either a or b could be positive and negative so I could have (2,-3). So the way you suggested has problems with this case (otherwise it is more superior than my way). Do you have a better method to cope with this one positive, one negative case?
 
  • #4
A negative number can never lie within 0.01% of a positive number or vice versa. That just can't happen! So if (a,b) can be (2,-3) the problem don't make any sense! But you could try:

return (abs(max(a,b)/min(a,b)) <= 1.0001 && abs(min(a,b)/max(a,b)) >= 0.9999);

Note that before using this method you have to take care of the case where a or b equals 0. And why is that? Cause division with zero is undefined. If a equals 0 and b is non-zero they can't lie within 0.01% of each other. But if a and b both equals 0 they will lie within each other. So I'd use (instead of the one obove):

return (a == b || a != 0 && b != 0 && abs(max(a,b)/min(a,b)) <= 1.0001 && abs(min(a,b)/max(a,b)) >= 0.9999);

If a equals b it will stop there and return the value true (even for the case a=b=0). But if a does not equal b it will continue to check that a and b are non-zero. If someone of them are zero it will stop there and return the value false. If they are both non-zero it will simply continue to check if they are within 0.01% each other.
This way we don't have to use if/else.
 

1. What is the formula for calculating the percentage difference of two numbers?

The formula for calculating the percentage difference of two numbers is: (|a - b| / ((a + b) / 2)) * 100, where a and b are the two numbers being compared.

2. How do I interpret the percentage difference of two numbers?

The percentage difference of two numbers is a measure of the difference between them, expressed as a percentage of the average of the two numbers. A positive percentage difference means that the first number is larger than the second number, while a negative percentage difference means that the first number is smaller than the second number. A percentage difference of 0% means that the two numbers are equal.

3. Can the percentage difference of two numbers be greater than 100%?

Yes, the percentage difference of two numbers can be greater than 100%. This occurs when the first number is significantly larger than the second number, resulting in a percentage difference that is greater than 100% of the average of the two numbers.

4. Is the percentage difference of two numbers affected by the order of the numbers?

No, the percentage difference of two numbers is not affected by the order of the numbers. The formula used to calculate the percentage difference takes the absolute value of the difference between the two numbers, so the result will always be the same regardless of which number is subtracted from the other.

5. How can I use the percentage difference of two numbers in my research or experiments?

The percentage difference of two numbers can be a useful tool for comparing data in research or experiments. It can help to identify significant differences between two sets of data and can also be used to track changes over time. Additionally, the percentage difference can be used to calculate other measures, such as percent change or percent error, which may be relevant in certain scientific fields.

Similar threads

  • Introductory Physics Homework Help
Replies
9
Views
919
  • Set Theory, Logic, Probability, Statistics
Replies
6
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
839
  • Introductory Physics Homework Help
Replies
23
Views
257
  • Precalculus Mathematics Homework Help
Replies
34
Views
2K
  • Science Fiction and Fantasy Media
Replies
0
Views
853
  • Set Theory, Logic, Probability, Statistics
Replies
3
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
12
Views
898
  • Introductory Physics Homework Help
Replies
5
Views
1K
  • Introductory Physics Homework Help
Replies
2
Views
1K
Back
Top