Percentage difference of two numbers?

Click For Summary

Homework Help Overview

The discussion revolves around defining the percentage difference between two numbers, particularly in the context of a computer science exercise. The original poster presents a method for calculating this difference and seeks alternative suggestions.

Discussion Character

  • Exploratory, Conceptual clarification, Mathematical reasoning

Approaches and Questions Raised

  • The original poster proposes a method for calculating percentage difference and compares it with two alternative methods, questioning their effectiveness. Other participants discuss the implications of positive and negative values in the calculations and suggest modifications to handle these cases.

Discussion Status

Participants are actively exploring different methods for calculating percentage difference, particularly in scenarios involving both positive and negative numbers. Some guidance has been offered regarding handling edge cases, but no consensus has been reached on a definitive method.

Contextual Notes

There are constraints regarding the definitions of the numbers involved, particularly concerning their signs and the implications of zero values in the calculations. Participants are questioning the validity of the problem setup when one number is positive and the other is negative.

pivoxa15
Messages
2,250
Reaction score
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
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);
 
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?
 
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.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 2 ·
Replies
2
Views
909
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 64 ·
3
Replies
64
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
2
Views
2K