# Mathematically Find Max Value without Conditional Comparison

Filed under:
|
|
|
|
##### conditional

----------Updated ------------

codymanix and moonshadow have been a big help thus far. I was able to solve my problem using the equations and instead of using right shift I divided by 29. Because with 32bits signed 2^31 = overflows to 29. Which works!

Prototype in PHP

``````\$r = \$x - ((\$x - \$y) & ((\$x - \$y) / (29)));
``````

Actual code for LEADS (you can only do one math function PER LINE!!! AHHHH!!!)

``````DERIVDE1 = IMAGE1 - IMAGE2;
DERIVED2 = DERIVED1 / 29;
DERIVED3 = DERIVED1 AND DERIVED2;
MAX = IMAGE1 - DERIVED3;
``````

----------Original Question----------- I don't think this is quite possible with my application's limitations but I figured it's worth a shot to ask.

I'll try to make this simple. I need to find the max values between two numbers without being able to use a IF or any conditional statement.

In order to find the the MAX values I can only perform the following functions

``````Divide, Multiply, Subtract, Add, NOT, AND ,OR
``````

Let's say I have two numbers

``````A = 60;
B = 50;
``````

Now if A is always greater than B it would be simple to find the max value

``````MAX = (A - B) + B;
ex.
10 = (60 - 50)
10 + 50 = 60 = MAX
``````

Problem is A is not always greater than B. I cannot perform ABS, MAX, MIN or conditional checks with the scripting applicaiton I am using.

Is there any way possible using the limited operation above to find a value VERY close to the max?

© Stack Overflow or respective owner

### Related posts about math

• #### how to make a function recursive

as seen on Stack Overflow - Search for 'Stack Overflow'
i have this huge function and i am wondering how to make it recursive. i have the base case which should never come true, so it should always go to else and keep calling itself with the variable t increases. any help would be great thanks def draw(x, y, t, planets): if 'Satellites' in planets["Moon"]: … >>> More

• #### Sunrise / set calculations

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm trying to calculate the sunset / rise times using python based on the link provided below. My results done through excel and python do not match the real values. Any ideas on what I could be doing wrong? My Excel sheet can be found under .. http://transpotools.com/sun_time.xls # Created on… >>> More

• #### Android-Java: Constructing a triangle based on Coordinates on a map and your bearing

as seen on Stack Overflow - Search for 'Stack Overflow'
Hi Guys, I'm constructing a geolocation based application and I'm trying to figure out a way to make my application realise when a user is facing the direction of the given location (a particular long / lat co-ord). I've got the math figured, I just have the triangle to construct. //UPDATE So… >>> More

• #### Math.max and Math.min outputting highest and lowest values allowed

as seen on Stack Overflow - Search for 'Stack Overflow'
so I'm trying to make a program that will output the sum, average, and smallest and largest values. I have everything basically figured out except the smallest and largest values are outputting 2147483647 and -2147483647, which I believe are the absolute smallest and largest values that Java will… >>> More

• #### Is Programming == Math?

as seen on Stack Overflow - Search for 'Stack Overflow'
I've heard many times that all programming is really a subset of math. Some suggest that OO, at its roots, is mathematically based. I don't get the connection. Aside from some obvious examples: using induction to prove a recursive algorithm formal correctness proofs functional languages lambda calculus asymptotic… >>> More

### Related posts about max

• #### SQL Server: SELECT rows with MAX(Column A), MAX(Column B), DISTINCT by related columns

as seen on Stack Overflow - Search for 'Stack Overflow'
Scenario: Table A MasterID, Added Date, Added By, Updated Date, Updated By, 1, 1/1/2010, 'Fred', null, null 2, 1/2/2010, 'Barney', 'Mr. Slate', 1/7/2010 3, 1/3/2010, 'Noname', null, null Table B MasterID, Added Date, Added By, Updated Date, Updated By, 1, 1/3/2010, 'Wilma', 'The Great Kazoo',… >>> More

• #### How do I determine if my controller is in IDE or AHCI mode in Linux?

as seen on Super User - Search for 'Super User'
I have an old MacBook Pro 4,1 (early 2008) - but I suspect an answer would apply to many MacBook Pros. It has an Intel IDE/SATA controller (ICH8M/ICH8M-E). I have installed a patched MBR that is supposed to put my controller into AHCI mode. It does this by setting some controller port value that… >>> More

• #### Positioning an image inside an ImageView width max height and max width set

as seen on Stack Overflow - Search for 'Stack Overflow'
I have an ImageView with max height and max width both set to 100. The figure below is clearly not a square, but you can use your imagination ;) Figure 1: +----------------------------------------------+ ¦ ImageView +--------------+ ¦ ¦ ¦ ¦ … >>> More

• #### Getting the index of the returned max or min item using max()/min() on a list

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm using Python's max and min functions on lists for a minimax algorithm, and I need the index of the value returned by max() or min(). In other words, I need to know which move produced the max (at a first player's turn) or min (second player) value. for i in range(9): newBoard = currentBoard… >>> More

• #### Storing varchar(max) & varbinary(max) together - Problem?

as seen on Stack Overflow - Search for 'Stack Overflow'
I have an app that will have entries of both varchar(max) and varbinary(max) data types. I was considering putting these both in a separate table, together, even if only one of the two will be used at any given time. The question is whether storing them together has any impact on performance. Considering… >>> More