C# 比较两个数字并返回 -1、0 或 1
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12184667/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two numbers and return -1, 0 or 1
提问by Daan
Is there a simple math function available that compares numbers x and y and returns -1 when x is less than y, 1 when x is more than y and 0 when they're equal?
是否有一个简单的数学函数可以比较数字 x 和 y 并在 x 小于 y 时返回 -1,当 x 大于 y 时返回 1,当它们相等时返回 0?
If not, would there be a elegant solution (without any if's) to convert the output of Math.Max(x, y)to these returns? I was thinking of dividing the numbers by themselves, e.g. 123/123 = 1 but that will introduce the problem of dividing by 0.
如果没有,是否有一个优雅的解决方案(没有任何if's)将输出转换Math.Max(x, y)为这些回报?我正在考虑将数字除以自己,例如 123/123 = 1 但这会引入除以 0 的问题。
采纳答案by Jon Skeet
For your strict -1, 0 or 1 requirement, there's no single method that is guaranteed to do this. However, you can use a combination of Int32.CompareToand Math.Sign:
对于严格的 -1、0 或 1 要求,没有一种方法可以保证做到这一点。但是,你可以使用的组合Int32.CompareTo和Math.Sign:
int value = Math.Sign(x.CompareTo(y));
Alternatively, if you're happy with the normal CompareTocontract which is just stated in terms of negative numbers, positive numbers and 0, you can use CompareToon its own.
或者,如果您对CompareTo仅以负数、正数和 0 表示的正常合同感到满意,您可以单独使用CompareTo。
回答by RBarryYoung
It's the Math.Sign() function.
它是 Math.Sign() 函数。
Like this:
像这样:
return Math.Sign(x-y);
回答by Chris Gessler
Use the CompareTo()function
使用CompareTo()功能
int i = 5;
int n = 6;
int c = i.CompareTo(n);
I generally use it in ifstatements:
我通常在if语句中使用它:
int x = 34;
int y = 25;
if(x.CompareTo(y) == 0)
{
Console.WriteLine("Yes, they are equal");
}
else
{
Console.WriteLine("No, they are not equal");
}
Edit:
编辑:
After some people claimed that Int32.CompareTo() could return something other than -1|0|1, I decided to research the possibility myself.
在有人声称 Int32.CompareTo() 可以返回 -1|0|1 之外的其他内容之后,我决定自己研究这种可能性。
Here's the reflected code for Int32.CompareTo(). I fail to see how either one would ever return anything but -1|0|1.
这是 的反射代码Int32.CompareTo()。我看不出任何一个人会返回 -1|0|1 之外的任何东西。
[TargetedPatchingOptOut("Performance critical to inline across NGen image boundaries")]
public int CompareTo(int value)
{
if (this < value)
{
return -1;
}
if (this > value)
{
return 1;
}
return 0;
}
public int CompareTo(object value)
{
if (value == null)
{
return 1;
}
if (!(value is int))
{
throw new ArgumentException(Environment.GetResourceString("Arg_MustBeInt32"));
}
int num = (int) value;
if (this < num)
{
return -1;
}
if (this > num)
{
return 1;
}
return 0;
}
回答by Aghilas Yakoub
you can try with this code
你可以试试这个代码
var result = a.CompareTo(b);
回答by John Koerner
回答by Ascension
You tried use compareTo()? Look here: http://msdn.microsoft.com/en-us/library/y2ky8xsk.aspx
你试过用compareTo()吗?看这里:http: //msdn.microsoft.com/en-us/library/y2ky8xsk.aspx
回答by vane
You can do it without using any .NET calls at all and on 1 line. NOTE: Math.Sign and type.CompareTo both use logical if statements and comparison operators which you said you wanted to avoid.
您可以在不使用任何 .NET 调用的情况下在 1 行上完成此操作。注意:Math.Sign 和 type.CompareTo 都使用逻辑 if 语句和您说过要避免的比较运算符。
int result = (((x - y) >> 0x1F) | (int)((uint)(-(x - y)) >> 0x1F));
as a function
作为函数
//returns 0 if equal
//returns 1 if x > y
//returns -1 if x < y
public int Compare(int x, int y)
{
return (((x - y) >> 0x1F) | (int)((uint)(-(x - y)) >> 0x1F));
}
Basically, all this does is SHIFTthe sign bits all the way to the first position. If the result is unsigned then it will be 0; then it does the same operation and flips the sign bits then ORs them together and the result is wither 1, 0 or -1.
基本上,所有这样做是SHIFT的符号位一路到第一位置。如果结果是无符号的,那么它将是 0;然后它执行相同的操作和翻转的符号位则ORš在一起,其结果是枯萎1,0或-1。
Case where result is -1
结果为 -1 的情况
IS 12 > 15:
12 - 15 = -3 (11111111111111111111111111111101)
-3 >> 0x1F = -1 (11111111111111111111111111111111)
-(12 - 15) = 3 (00000000000000000000000000000011)
3 >> 0x1F = ((uint)0)=0 (00000000000000000000000000000000) cast to uint so 0
11111111111111111111111111111111
OR
00000000000000000000000000000000
= 11111111111111111111111111111111 (-1)
Case where result is 1
结果为 1 的情况
IS 15 > 12:
15 - 12 = 3 (00000000000000000000000000000011)
3 >> 0x1F = 0 (00000000000000000000000000000000)
-(15 - 12) = -3 (11111111111111111111111111111101)
-3 >> 0x1F = ((uint)-1)=1 (00000000000000000000000000000001) cast to uint so 1
00000000000000000000000000000000
OR
00000000000000000000000000000001
= 00000000000000000000000000000001 (1)
Case where result is 0
结果为 0 的情况
IS 15 == 15:
15 - 15 = 0 (00000000000000000000000000000000)
0 >> 0x1F = 0 (00000000000000000000000000000000)
-(15 - 15) = 0 (00000000000000000000000000000000)
0 >> 0x1F = ((uint)0)=0 (00000000000000000000000000000000) cast to uint so 1
00000000000000000000000000000000
OR
00000000000000000000000000000000
= 00000000000000000000000000000000 (0)
This should also be much faster than using any calls to Math or any other .NET methods.
这也应该比使用任何对 Math 或任何其他 .NET 方法的调用快得多。

