开发者

Why does -2 % 360 give -2 instead of 358 in c#

Microsoft Mathematics and Google's calculator give me 358 for -2 % 360, but C# and win开发者_StackOverflow中文版dows calculator are outputting -2 ... which is the right answer ?


The C# compiler is doing the right thing according to the C# specification, which states that for integers:

The result of x % y is the value produced by x – (x / y) * y.

Note that (x/y) always rounds towards zero.

For the details of how remainder is computed for binary and decimal floating point numbers, see section 7.8.3 of the specification.

Whether this is the "right answer" for you depends on how you view the remainder operation. The remainder must satisfy the identity that:

dividend = quotient * divisor + remainder

I say that clearly -2 % 360 is -2. Why? Well, first ask yourself what the quotient is. How many times does 360 go into -2? Clearly zero times! 360 doesn't go into -2 at all. If the quotient is zero then the remainder must be -2 in order to satisfy the identity. It would be strange to say that 360 goes into -2 a total of -1 times, with a remainder of 358, don't you think?


Which is the right answer?

Both answers are correct. It's merely a matter of convention which value is returned.


Both, see Modulo operation on Wikipedia.


I found this very easy to understand explanation at http://mathforum.org/library/drmath/view/52343.html

There are different ways of thinking about remainders when you deal 
with negative numbers, and he is probably confusing two of them. The 
mod function is defined as the amount by which a number exceeds the 
largest integer multiple of the divisor that is not greater than that 
number. In this case, -340 lies between -360 and -300, so -360 is the 
greatest multiple LESS than -340; we subtract 60 * -6 = -360 from -340 
and get 20:

-420 -360 -300 -240 -180 -120  -60   0   60   120  180  240  300  360
--+----+----+----+----+----+----+----+----+----+----+----+----+----+--
       | |                                                    |  |
   -360| |-340                                             300|  |340
       |=|                                                    |==|
        20                                                     40

Working with a positive number like 340, the multiple we subtract is 
smaller in absolute value, giving us 40; but with negative numbers, we 
subtract a number with a LARGER absolute value, so that the mod 
function returns a positive value. This is not always what people 
expect, but it is consistent.

If you want the remainder, ignoring the sign, you have to take the 
absolute value before using the mod function.

Doctor Peterson, The Math Forum http://mathforum.org/dr.math/


IMO, -2 is much easier to understand and code with. If you divide -2 by 360, your answer is 0 remainder -2 ... just as dividing 2 by 360 is 0 remainder 2. It's not as natural to consider that 358 is also the remainder of -2 mod 360.


From wikipedia:

if the remainder is nonzero, there are two possible choices for the remainder, one negative and the other positive, and there are also two possible choices for the quotient. Usually, in number theory, the positive remainder is always chosen, but programming languages choose depending on the language and the signs of a and n.[2] However, Pascal and Algol68 do not satisfy these conditions for negative divisors, and some programming languages, such as C89, don't even define a result if either of n or a is negative.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜