简体   繁体   English

为什么模数运算符(%)结果隐式地转换到C#的左侧而不是右侧?

[英]Why is a modulo operator (%) result implicitly cast to the left side rather than the right side in C#?

Take the following code: 请使用以下代码:

long longInteger = 42;
int normalInteger = 23;
object rem = longInteger % normalInteger;

If rem is the remainder of longInteger / normalInteger , shouldn't the remainder always be bounded by the smaller sized "int", the divisor? 如果remlongInteger / normalInteger的余数, longInteger / normalInteger余数总是不应该被较小的“int”(除数)限制吗? Yet in C#, the above code results in rem being a long . 然而在C#中,上面的代码导致remlong

Is it safe to convert rem to int without any loss of data? rem转换为int而不丢失任何数据是否安全?

int remainder = Convert.ToInt32(rem);

There is no overload of the modulo operator that takes a long and an int , so the int will be converted to long to match the other operand. 模数运算符没有带有longint重载,因此int将转换为long以匹配另一个操作数。

Looking at it at a lower level, in the CPU there is no separate instruction for calculating modulo, it's just one of the results of the division operation. 在较低级别查看它,在CPU中没有用于计算模数的单独指令,它只是除法运算的结果之一。 The output of the operation is the result of the division, and the reminder. 操作的输出是除法和提醒的结果。 Both are the same size, so as the result of the division has to be a long , so is the reminder. 两者都是相同的大小,因此除法的结果必须很long ,提醒也是如此。

As the reminder has to be smaller than the divisor, you can safely cast the result to int when the divisor comes from an int . 由于提醒必须小于除数,因此当除数来自int时,可以安全地将结果转换为int

No matter what your two integers, the outcome must be lower than "normalInteger". 无论你的两个整数是什么,结果必须低于“normalInteger”。 Therefore, the result "rem" will be constrained to the limits of an int type. 因此,结果“rem”将被限制为int类型的限制。 So, it will be safe to convert rem to int without loss of data. 因此,在不丢失数据的情况下将rem转换为int是安全的。

yes it has not problem, the convert function is going to round the result. 是的,它没有问题,转换函数将围绕结果。 then you should know if it is usefull for you or not. 然后你应该知道它是否对你有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TabControl c#右侧和左侧的选项卡 - Tabs in right and left side of TabControl c# 引用隐式强制转换运算符的左侧 - Reference to left hand side on implicit cast operator C# 是否可以在操作数的右侧使用 Func&lt;&gt; 重载运算符“&gt;”? - C# Possible to overload operator ">" with Func<> on right side of operand? 如何在 c# 中将数组的所有元素从左侧移动到右侧 - How do I shift all of the elements of an array from the left side to the right side in c# 在C#中,为什么条件运算符不能隐式转换为可空类型 - In C# why can't a conditional operator implicitly cast to a nullable type c#为什么ListBoxItem不能显示每张图片(左侧)? - c# why cannot ListBoxItem display each picture (left side)? 对于C#中的不同.N​​ET版本,模运算符(%)给出不同的结果 - The modulo operator (%) gives a different result for different .NET versions in C# 在C#中使用`x + = 1&#39;而不是`++ x`有副作用吗? - Are there any side-effects in using `x += 1` rather than `++x` in C#? C#指针:错误CS0254:固定语句赋值的右侧可能不是强制转换表达式 - C# Pointer: error CS0254: The right hand side of a fixed statement assignment may not be a cast expression C#,模运算比计算器给出不同的结果 - C#, Modulo operation gives diffrent result than a calculator
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM