Why -2% 360 give -2 instead of 358 in C #

Microsoft Mathematics and the Google calculator give me 358 for -2% 360, but C # and the Windows calculator output -2 ... what is the correct answer?

+6
source share
6 answers

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

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

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

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

Whether this will be the β€œright answer” for you depends on how you view the stop operation. The rest should satisfy an identity that:

 dividend = quotient * divisor + remainder 

I say clear -2% 360 = -2. What for? First ask yourself what a factor is. How many times does 360 go -2? Clearly zero time! 360 doesn't go at -2 at all. If the factor is zero, then the remainder must be -2 to satisfy the identity. It would be strange to say that 360 goes -2 a total of -1 times, with a remainder of 358, do not you think?

+12
source

What is the correct answer?

Both answers are correct. It is simply a matter of agreement, the meaning of which is returned.

+7
source
+3
source

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. 

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

+3
source

IMO, -2 is much easier to understand and code. If you divide -2 by 360, your answer will be 0 the rest of -2 ... just like dividing 2 by 360 is equal to 0 of remainder 2. It is not so natural to assume that 358 is also the remainder of -2 mod 360.

0
source

From wikipedia :

if the remainder is non-zero, there are two possible options for the remainder, one negative and the other positive, as well as two possible options for the quotient. Usually in number theory, a positive remainder is always chosen, but programming languages ​​are chosen depending on the language and the signs a and n. [2] However, Pascal and Algol68 do not satisfy these conditions for negative divisors, and some programming languages, such as C89, do not even determine the result if either n or a is negative.

0
source

Source: https://habr.com/ru/post/898104/


All Articles