Why does the Java% operator give different results than my calculator for a negative dividend?

How to get the calculator -1 mod 26 = 25, but in C or Java -1 % 26 == -1 . I need a program that solves it like a calculator. Is there a difference between the two?

+4
source share
3 answers

Both answers (25 and -1) are valid. Just different systems have different conventions.

The one that I see most common (in math) is as follows:

 quotient = floor(x / y) remainder = x - quotient * y 

Where floor() rounded to negative infinity.

This is an agreement that gives you your calculator. (Mathematica also uses this convention.)

I think most programming languages:

 quotient = integerpart(x / y) remainder = x - quotient * y 

Where integerpart() matches the application (float -> integer). (rounded to zero)

Some conventions are how to keep the rest of the same character as one of the operands.

The same applies to the sign of the divider. Different conventions are different.

+6
source

"Why...?"

There are two general definitions of a modulo operation. Java chose one ("the same sign as a dividend"), and the calculator implements another; apparently "the same sign as the divisor", although you need to do some experimentation to be sure.

In fact, the Wikipedia page in modulo operation provides 4 different operator definitions that are used by different programming languages. (And some programming languages ​​give you two statements with different semantics.)

In case of C, the definition depends on the version of the C standard that you are talking about. For ISO C 1999, the modulo operator follows the same definition as Java. For earlier versions of the C standard, the semantics of % implementation dependent

"Is there a difference between the two?"

Obviously there is!

"I need a program that solves it like a calculator."

Feel free to write one thing :-).

But if you just want to know how to get the form of the module "same sign as divisor" in Java, here is one way:

 int otherModulus = (a % b) + (a < 0 ? b : 0); // works for b > 0. int otherModulus = (a % b) + // works for b != 0 (b > 0 ? (a < 0 ? b : 0) : (a > 0 ? -b : 0)); 
+5
source

Modular arithmetic with negative operands differs in different languages ​​and corresponds to the definition of the language for driving. In Java, a Module is more like a Remainder .

Generally, if you want to get a negative number for negative inputs, you can use this:

 int r = x % n; if (r > 0 && x < 0) { r -= n; } 

Or, if you used a language that returns a negative number on a negative input, and you prefer positive:

 int r = x % n; if (r < 0) { r += n; } 

So, based on what you need as the desired result, I recommend that you use the appropriate implementation, rather than relying on the language to calculate for you.

Also note that the result should be described in the case of Java in JLS: in section 15.17.3 - Stop operator%. They give motivation there (that% b should be such that (a / b) * b + (a% b) is equal to a), but this is the inclusion of this section in JLS, which makes the result this way.

+3
source

All Articles