在没有条件比较的情况下以数学方式查找最大值
----------更新 ------------
到目前为止,codymanix 和 Moonshadow 提供了很大的帮助。我能够使用方程解决我的问题,而不是使用右移除以 29。因为使用 32 位有符号 2^31 = 溢出到 29。这有效!
PHP 原型
$r = $x - (($x - $y) & (($x - $y) / (29)));
LEADS 的实际代码(每行只能执行一个数学函数!啊哈哈哈!!!)
DERIVDE1 = IMAGE1 - IMAGE2;
DERIVED2 = DERIVED1 / 29;
DERIVED3 = DERIVED1 AND DERIVED2;
MAX = IMAGE1 - DERIVED3;
----------原始问题------------
我认为由于我的应用程序的限制,这不太可能,但我认为值得一试。
我会尝试让这变得简单。我需要找到两个数字之间的最大值,而无法使用 IF 或任何条件语句。
为了找到最大值,我只能执行以下功能
Divide, Multiply, Subtract, Add, NOT, AND ,OR
假设我有两个数字
A = 60;
B = 50;
现在,如果A总是大于B,那么找到最大值就会很简单
MAX = (A - B) + B;
ex.
10 = (60 - 50)
10 + 50 = 60 = MAX
问题是A并不总是大于B。我不能使用我正在使用的脚本应用程序执行 ABS、MAX、MIN 或条件检查。
有没有办法使用上面的有限运算来找到非常接近最大值的值?
----------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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
我想如果我们设法找到两个数字之间的差异(只有幅度而不是符号),这将是最简单的,
其中
|ab|
是a
之间差异的幅度和b。I guess this one would be the most simplest if we manage to find difference between two numbers (only the magnitude not sign)
where
|a-b|
is a magnitude of difference betweena
andb
.查找 2 个变量中的最大值:
max = a-((ab)& ((ab)>>31))
其中>>按位右移(也称为 SHR 或 ASR,具体取决于符号)。
您使用数字的位数减一而不是 31。
finding the maximum of 2 variables:
max = a-((a-b)&((a-b)>>31))
where >> is bitwise right-shift (also called SHR or ASR depeding on signedness).
Instead of 31 you use the number of bits your numbers have minus one.
如果您不相信您的环境能够在可用时生成适当的无分支操作,请参阅 此页面了解如何继续。注意输入范围的限制;如果您不能保证您的输入适合,请使用更大的整数类型进行操作。
If you can't trust your environment to generate the appropriate branchless operations when they are available, see this page for how to proceed. Note the restriction on input range; use a larger integer type for the operation if you cannot guarantee your inputs will fit.
无条件解决方案。转换为 uint 然后返回 int 以获得abs。
Solution without conditionals. Cast to uint then back to int to get abs.
仅使用逻辑运算、短路求值并假设向零舍入的 C 约定,可以将其表示为:
基本思想是实现一个将返回 0 或 1 的比较运算符。如果满足以下条件,则可以执行类似的技巧:您的脚本语言遵循向下限值舍入的惯例,就像 python 一样。
Using logical operations only, short circuit evaluation and assuming the C convention of rounding towards zero, it is possible to express this as:
The basic idea is to implement a comparison operator that will return 0 or 1. It's possible to do a similar trick if your scripting language follows the convention of rounding toward the floor value like python does.
嗯。我假设 NOT、AND 和 OR 是按位的?如果是这样,就会有一个按位表达式来解决这个问题。请注意,A | B 将给出一个数字 >= A 且 >= B。也许有一种剪枝方法来选择具有最多位数的数字。
为了扩展,我们需要以下来确定 A (0) 或 B (1) 是否更大。
真值表:
因此,将给出较大位的索引。因此,比较两个数字中的每一位,当它们不同时,使用上面的表达式(Not A And B)来确定哪个数字更大。从最高有效位开始,向下两个字节进行。如果没有循环结构,请手动比较每一位。
实现“当它们不同时”:
(A!= B)AND(我的逻辑在这里)
Hmmm. I assume NOT, AND, and OR are bitwise? If so, there's going to be a bitwise expression to solve this. Note that A | B will give a number >= A and >= B. Perhaps there's a pruning method for selecting the number with the most bits.
To extend, we need the following to determine whether A (0) or B (1) is greater.
truth table:
therefore, will give the index of the greater bit. Ergo, compare each bit in both numbers, and when they are different, use the above expression (Not A And B) to determine which number was greater. Start from the most significant bit and proceed down both bytes. If you have no looping construct, manually compare each bit.
Implementing "when they are different":
(A != B) AND (my logic here)
试试这个,(但要注意溢出)
(C#代码)
try this, (but be aware for overflows)
(Code in C#)
您可以将其表示为一系列算术和按位运算,例如:
You can express this as a series of arithmetic and bitwise operations, e.g.:
这是我的实现,仅使用 +、-、*、%、/ 运算符
This is my implementation using only
+, -, *, %, /
operators我刚刚想出了一个表达:
(( (ab)-|ab| ) / (2(ab)) )*b + (( (ba)-|ba| )/(2(ba)) )*a
如果 a>b,则等于 a;当 a>b 时,如果 b>a,则等于 b
:
ab>0,ab = |ab|,(ab)-|ab| = 0
因此 b 的系数为 0ba<0, ba = -|ba|, (ba)-|ba| = 2(ba)
所以a的系数是
2(ba)/2(ba)
,即1所以如果 a 更大,它最终会返回 0*b+1*a ,反之亦然
I just came up with an expression:
(( (a-b)-|a-b| ) / (2(a-b)) )*b + (( (b-a)-|b-a| )/(2(b-a)) )*a
which is equal to a if a>b and is equal to b if b>a
when a>b:
a-b>0, a-b = |a-b|, (a-b)-|a-b| = 0
so the coeficcient for b is 0b-a<0, b-a = -|b-a|, (b-a)-|b-a| = 2(b-a)
so the coeficcient for a is
2(b-a)/2(b-a)
which is 1so it would ultimately return
0*b+1*a
if a is bigger and vice versa找出 n 与 n 之间的 MAX m
在 c: 中使用 #define
或
Find MAX between n & m
Using #define in c:
or
请看看这个程序..这可能是迄今为止此页面上最好的答案...
please look at this program.. this might be the best answer till date on this page...
不需要。只需使用:
int maxA(int A, int B){ return A;}
(1) 如果允许条件,您会执行
max = a>b 吗? a:b
。(2) 任何其他方法要么使用一组定义的数字,要么依赖隐式条件检查。
(2a)
max = a-((ab)&((ab)>>31))
这很简洁,但只有在你使用 32 时才有效位数字。您可以将其扩展为任意大的数字 N,但如果您尝试找到 max(N-1, N+1),该方法将失败。该算法适用于有限状态自动机,但不适用于图灵机。
(2b) 幅度
|ab|
是一个条件|ab| = ab>0 ab : ba
怎么样:
平方根也是一个条件。每当
c>0
和c^2 = d
时,我们就有第二个解决方案-c
,因为(-c)^2 = ( -1)^2*c^2 = 1*c^2 = d
。平方根返回该对中最大的一个。我附带了一个 int max(int c1, int c2){return max(c1, c2);} 的构建,没有比较运算符,数学是非常对称的,并且功率有限。如果没有某种
if
,就无法区分正数和负数。No need. Just use:
int maxA(int A, int B){ return A;}
(1) If conditionals are allowed you do
max = a>b ? a : b
.(2) Any other method either use a defined set of numbers or rely on the implicit conditional checks.
(2a)
max = a-((a-b)&((a-b)>>31))
this is neat, but it only worksif
you use 32 bit numbers. You can expand it arbitrary large number N, but the method will fail if you try to find max(N-1, N+1). This algorithm works for finite state automata, but not a Turing machine.(2b) Magnitude
|a-b|
is a condition|a-b| = a-b>0 a-b : b-a
What about:
Square root is also a condition. Whenever
c>0
andc^2 = d
we have second solution-c
, because(-c)^2 = (-1)^2*c^2 = 1*c^2 = d
. Square root returns the greatest in the pair. I comes with a build inint max(int c1, int c2){return max(c1, c2);}
Without comparison operator math is very symmetric as well as limited in power. Positive and negative numbers cannot be distinguished without
if
of some sort.这取决于您使用的语言,但三元运算符可能很有用。
但是,如果您无法在“脚本应用程序”中执行条件检查,则可能没有三元运算符。
It depends which language you're using, but the Ternary Operator might be useful.
But then, if you can't perform conditional checks in your 'scripting application', you probably don't have the ternary operator.