Send to a Friend
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.
Using Fluther
or