How can I find the largest of two numbers mathematically?
Asked by
jaguarcy (
10)
January 25th, 2010
This is stemming from a computer science question. Is it possible to write a mathematical formula with two variables, whose result will always be the largest of the two variables?
I have some code that appears to do just that, except I cannot for the life of me understand why or how.
Edit: to add the code
int func(int x, int y) {
int d=x-y;
int s=(2*d+2)/(2*d+1);
s=2*s/(s+1);
s=s*2–1;
d*=s;
return (x+y+d)/2;
}
Changing the return statement to (x+y-d)/2 appears to return the smallest of the two numbers.
I could be completely off about these results, they’ve only been verified by observation.
Observing members:
0
Composing members:
0
9 Answers
Why not show us the code?
edited to include extra info, including the code.
@jaguarcy The largest of two numbers? That seems like a lot of extra work.
What happens when you subtract two numbers A and B from each other? What does that tell you?
This question is more like whether or not this can be done without using any sort of comparison. No conditionals, no equality operators, and definitely no comparison operators :)
It’s more of a mental exercise or a math problem rather than code intended for production of any sorts.
All of the manipulations with “S” do what, exactly? I don’t see how they contribute to the final result. (If you used “d*” in your final calculation, then it would matter, but what you’ve shown is just wasting processing time on calculating d*—it’s never used in the final calculation.)
As for the return values you do have, that’s very simple if you think about it:
Integer 1 + Integer 2 + the difference between the integers is equivalent to adding:
The larger of Integer 1 and Integer 2 + the larger of Integer 1 and Integer 2.
Then dividing by 2 just gives: The larger of Integer 1 and Integer 2
Can’t you just use an if-else function to compare the two integers – i.e., if x > y return x else y??? It has been years since I’ve done programming like this, but it looks like basic C. The code you posted looks like it might be some kind of series thing, but I don’t remember…...
int func(int x, int y) {
int d=x-y;
int s=(2*d+2)/(2*d+1);
/* Since C truncates toward 0 on integer division, s is: 1 if x > y, 2 if x = y, 0 if x < y. (Multiplication of d by 2 prevents division by zero is case y = x+1). */
s=2*s/(s+1);
/* If s = 1 or x = 0, this is NOP; but if s = 2, then s becomes 1 upon truncation. */
s=s*2–1;
/* If s = 1, this is a NOP; if s = 0, then s becomes -1. Thus, s is the sign of the difference d. */
d*=s;
/* This is a NOP if s >= 0; d becomes -d if s is -1, i.e., x-y becomes y-x. */
return (x+y+d)/2;
/* The numerator here is: (x+y)+(x-y) = 2*x if x > y, x+y = 2*x = 2*y if x = y, and (x+y)+(y-x) = 2*y if x < y. */
}
@ratboy thanks! that makes perfect sense, and I really should have been able to figure it out. When you put it like that it looks trivially simple… :)
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.