在没有条件比较的情况下数学地找到最大值 [英] Mathematically Find Max Value without Conditional Comparison
问题描述
---------更新了------------
----------Updated ------------
到目前为止,codymanix 和 moonshadow 提供了很大的帮助.我能够使用方程式解决我的问题,而不是使用右移除以 29.因为 32 位有符号 2^31 = 溢出到 29.哪个有效!
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!
PHP 中的原型
$r = $x - (($x - $y) & (($x - $y) / (29)));
LEADS 的实际代码(每行只能执行一个数学函数!!!啊啊啊!!!)
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.
我会尽量简化.我需要在无法使用 IF 或任何条件语句的情况下找到两个数字之间的最大值.
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.
为了找到 MAX 值,我只能执行以下功能
In order to find the the MAX values I can only perform the following functions
Divide, Multiply, Subtract, Add, NOT, AND ,OR
假设我有两个数字
A = 60;
B = 50;
现在如果 A 总是大于 B,那么找到最大值就很简单了
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
问题是 A 并不总是大于 B.我无法使用我正在使用的脚本应用程序执行 ABS、MAX、MIN 或条件检查.
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?
推荐答案
max = a-((a-b)&((a-b)>>31))
其中 >> 是按位右移(也称为 SHR 或 ASR,取决于符号).
where >> is bitwise right-shift (also called SHR or ASR depeding on signedness).
您使用数字的位数减一,而不是 31.
Instead of 31 you use the number of bits your numbers have minus one.
这篇关于在没有条件比较的情况下数学地找到最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!