Percentage difference of two numbers?

AI Thread Summary
The discussion centers on defining the percentage difference between two numbers, with a proposed formula that uses the midpoint to ensure consistent results across positive and negative pairs. The suggested method is preferred because it yields the same percentage difference for pairs like (4, 6) and (-4, -6), unlike other common formulas. A participant raises concerns about handling cases where one number is positive and the other is negative, noting that such numbers cannot be within 0.01% of each other. A refined approach is proposed to account for zero values and ensure accurate comparisons without using if/else statements. The conversation emphasizes the importance of a robust method for calculating percentage differences across various scenarios.
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.
 
Thread 'Variable mass system : water sprayed into a moving container'
Starting with the mass considerations #m(t)# is mass of water #M_{c}# mass of container and #M(t)# mass of total system $$M(t) = M_{C} + m(t)$$ $$\Rightarrow \frac{dM(t)}{dt} = \frac{dm(t)}{dt}$$ $$P_i = Mv + u \, dm$$ $$P_f = (M + dm)(v + dv)$$ $$\Delta P = M \, dv + (v - u) \, dm$$ $$F = \frac{dP}{dt} = M \frac{dv}{dt} + (v - u) \frac{dm}{dt}$$ $$F = u \frac{dm}{dt} = \rho A u^2$$ from conservation of momentum , the cannon recoils with the same force which it applies. $$\quad \frac{dm}{dt}...
Back
Top