java如何用负数进行模数计算?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4403542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 17:03:34  来源:igfitidea点击:

How does java do modulus calculations with negative numbers?

javamathmodulonegative-number

提问by Jakir00

Am I doing modulus wrong? Because in Java -13 % 64is supposed to evaluate to -13but I get 51.

我做模数错了吗?因为在 Java-13 % 64中应该评估为-13但我得到51.

采纳答案by Mark Byers

Both definitions of modulus of negative numbers are in use - some languages use one definition and some the other.

负数模数的两种定义都在使用 - 有些语言使用一种定义,另一些使用另一种定义。

If you want to get a negative number for negative inputs then you can use this:

如果你想得到负输入的负数,那么你可以使用这个:

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

Likewise if you were using a language that returns a negative number on a negative input and you would prefer positive:

同样,如果您使用的语言在负输入上返回负数,并且您更喜欢正数:

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

回答by Caner

Since "mathematically" both are correct:

由于“数学上”两者都是正确的:

-13 % 64 = -13 (on modulus 64)  
-13 % 64 = 51 (on modulus 64)

One of the options had to be chosen by Java language developers and they chose:

Java 语言开发人员必须选择其中一个选项,他们选择了:

the sign of the result equals the sign of the dividend.

结果的符号等于被除数的符号。

Says it in Java specs:

在 Java 规范中说:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.3

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.17.3

回答by wallyk

Modulo arithmetic with negative operands is defined by the language designer, who might leave it to the language implementation, who might defer the definition to the CPU architecture.

带有负操作数的模运算由语言设计者定义,他可能会将其留给语言实现,而后者可能会将定义推迟到 CPU 架构。

I wasn't able to find a Java language definition.
Thanks Ishtar, Java Language Specification for the Remainder Operator %says that the sign of the result is the same as the sign of the numerator.

我找不到 Java 语言定义。
感谢 Ishtar,余数运算符 % 的Java 语言规范说结果的符号与分子的符号相同。

回答by wallyk

x = x + m = x - min modulus m.
so -13 = -13 + 64in modulus 64and -13 = 51in modulus 64.
assume Z = X * d + r, if 0 < r < Xthen in division Z/Xwe call rthe remainder.
Z % Xreturns the remainder of Z/X.

x = x + m = x - m在模数中m
所以-13 = -13 + 64在模数64-13 = 51模数中64
假设Z = X * d + r,如果0 < r < X那么在除法中Z/X我们称r余数。
Z % X返回 的余数Z/X

回答by ruslik

you can use

您可以使用

(x % n) - (x < 0 ? n : 0);

回答by Nava Carmon

Your answer is in wikipedia: modulo operation

你的答案在维基百科: 模运算

It says, that in Java the sign on modulo operation is the same as that of dividend. and since we're talking about the rest of the division operation is just fine, that it returns -13 in your case, since -13/64 = 0. -13-0 = -13.

它说,在 Java 中,模运算的符号与被除数的符号相同。由于我们正在讨论除法运算的其余部分,因此在您的情况下它返回 -13,因为 -13/64 = 0。-13-0 = -13。

EDIT: Sorry, misunderstood your question...You're right, java should give -13. Can you provide more surrounding code?

编辑:对不起,误解了你的问题......你是对的,java 应该给出 -13。你能提供更多的周边代码吗?

回答by Andreas

To overcome this, you could add 64(or whatever your modulus base is) to the negative value until it is positive

为了克服这个问题,您可以将64(或任何您的模数基础)添加到负值,直到它为正

int k = -13;
int modbase = 64;

while (k < 0) {
    k += modbase;
}

int result = k % modbase;

The result will still be in the same equivalence class.

结果仍将在同一个等价类中。

回答by Salman Paracha

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. So in your case of

mod 函数定义为一个数超过不大于该数的除数的最大整数倍的量。所以在你的情况下

-13 % 64

the largest integer multiple of 64 that does not exceed -13 is -64. Now, when you subtract -13 from -64 it equals 51 -13 - (-64) = -13 + 64 = 51

不超过-13 的64 的最大整数倍是-64。现在,当您从 -64 中减去 -13 时,它等于 51-13 - (-64) = -13 + 64 = 51

回答by starblue

Your result is wrong for Java. Please provide some context how you arrived at it (your program, implementation and version of Java).

您的结果对于 Java 是错误的。请提供一些您是如何到达它的上下文(您的程序、实现和 Java 版本)。

From the Java Language Specification

来自Java 语言规范

15.17.3 Remainder Operator %
[...]
The remainder operation for operands that are integers after binary numeric promotion (§5.6.2) produces a result value such that (a/b)*b+(a%b) is equal to a.
15.17.3 余数运算符 %
[...]
二进制数字提升(第 5.6.2 节)后整数操作数的余数运算产生的结果值使得 (a/b)*b+(a%b) 等于一种。
15.17.2 Division Operator /
[...]
Integer division rounds toward 0.
15.17.2 除法运算符 /
[...]
整数除法向 0舍入。

Since / is rounded towards zero (resulting in zero), the result of % should be negative in this case.

由于 / 向零四舍五入(结果为零),因此在这种情况下 % 的结果应为负。

回答by Keyxeq

Are you sure you are working in Java? 'cause Java gives -13 % 64 = -13 as expected. The sign of dividend!

你确定你在用 Java 工作吗?因为 Java 按预期给出 -13 % 64 = -13。分红的标志!