Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

.net

math

Microsoft Mathematics and Google's calculator give me 358 for -2 % 360, but C# and windows calculator are outputting -2 ... which is the right answer ?

like image 295
Jonny Avatar asked Sep 26 '11 19:09

Jonny


4 Answers

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?

like image 167
Eric Lippert Avatar answered Oct 21 '22 01:10

Eric Lippert


Which is the right answer?

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

like image 22
David Heffernan Avatar answered Oct 21 '22 01:10

David Heffernan


Both, see Modulo operation on Wikipedia.

like image 3
svick Avatar answered Oct 21 '22 01:10

svick


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/

like image 3
TWA Avatar answered Oct 21 '22 01:10

TWA