How is the module different in C # or Excel?

I am experimenting with systems with negative numbers, and I use Excel to play and validate my calculations.

I noticed that there are differences in C # and Excel. Why does C # return a different result than Excel?

For instance:

C #: 146% -3 = 2

Excel: mod (146, -3) = -1

+4
source share
2 answers

As the article says , a modular operation is a dividend% divisor == remainder. A problem occurs when any of the operands is a negative value. At this point, the naive mathematical definition breaks down, and the result becomes implementation dependent.

Excel mod , . , modulo, ( & minus; & infin;). :

quotient = floor(dividend / divisor)
mod      = dividend - (divisor * quotient)

146 -3:

quotient = -49      // floor(146 / -3)
mod      = -1       // 146 - (-3 * -49) == 146 - 147

# : , . , 0. :

quotient = truncate(dividend / divisor)
mod      = dividend - (divisor * quotient)

:

quotient = -48     // truncate(146 / -3)
mod      = 2       // 146 - (-3 * -48) == 146 - 144
+4

, : x, y, q r ,

 q = x / y
 r = x - q * y   

, , .

# Excel. , , , . # , Excel . , # 8/-3 -2, excel INT (8/-3) - -3.

, .

+10

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


All Articles