General Question

jaguarcy's avatar

How can I find the largest of two numbers mathematically?

Asked by jaguarcy (10points) 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

phoenyx's avatar

Why not show us the code?

jaguarcy's avatar

edited to include extra info, including the code.

andrew's avatar

@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?

jaguarcy's avatar

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.

CyanoticWasp's avatar

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

lilikoi's avatar

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…...

ratboy's avatar

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. */

}

jaguarcy's avatar

@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

Login

or

Join

to answer.

This question is in the General Section. Responses must be helpful and on-topic.

Your answer will be saved while you login or join.

Have a question? Ask Fluther!

What do you know more about?
or
Knowledge Networking @ Fluther