本文介绍了负模算子结果的目的是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我以前是(幼稚的)假设,即模运算符返回了除法运算的余数.我显然是错的,因为-2%5返回3.我本以为5将-2零次除以-2作为余数.

I was previously under the (naive) assumption that the modulus operator returned the remainder of division. I was apparently wrong, as -2 % 5 returns 3. I would have thought that 5 divides -2 zero times with -2 as the remainder.

现在,我了解了如何执行此操作的机制,但是我的问题是为什么?有人可以给我链接到一些解释为什么模数和余数不是同义词的例子,还是一个有用的例子?

Now I understand the mechanics of how this operation is performed, but my question is why? Could someone give me a link to something that explains why modulus and remainder are not synonymous, or an example of a situation where it would be useful?

推荐答案

结果完全正确.模块化算术定义以下内容(由于无法用三行键入等号,因此我将使用"congruent")

The result is entirely correct. Modular arithmetic defines the following (I'll use "congruent" since I can't type the equal sign with three lines)

一个全等b mod c iff a-b是c的倍数,即x * c =(a-b)对于某个整数x.

a congruent b mod c iff a-b is a multiple of c, i.e. x * c = (a-b) for some integer x.

例如

0 congruent 0 mod 5 (0 * 5 = 0-0)
1 congruent 1 mod 5 (0 * 5 = 1-1)
2 congruent 2 mod 5 (0 * 5 = 2-2)
3 congruent 3 mod 5 (0 * 5 = 3-3)
4 congruent 4 mod 5 (0 * 5 = 4-4)
5 congruent 0 mod 5 (1 * 5 = 5-0)
6 congruent 1 mod 5 (1 * 5 = 6-1)
...

同样可以扩展为负整数:

The same can be extended to negative integers:

-1 congruent 4 mod 5 (-1 * 5 = -1-4)
-2 congruent 3 mod 5 (-1 * 5 = -2-3)
-3 congruent 2 mod 5 (-1 * 5 = -3-2)
-4 congruent 1 mod 5 (-1 * 5 = -4-1)
-5 congruent 5 mod 5 (-1 * 5 = -5-0)
-6 congruent 4 mod 5 (-2 * 5 = -6-4)
-7 congruent 3 mod 5 (-2 * 5 = -7-3)
...

如您所见,许多整数是一致的3 mod 5: ...,-12,-7,-2,3,8,13,...

As you can see, a lot of integers are congruent 3 mod 5: ..., -12, -7, -2, 3, 8, 13, ...

在数学中,这些数字的集合被称为由等价关系"congruence"推导的等价类.我们对其余部分的理解和"mod"函数的定义均基于此等效类. 余数"或mod计算的结果是等效类的代表元素.通过声明,我们选择了最小的 non-negative 元素(因此-2不是有效的候选项).

In mathematics, the set of these numbers is called the equivalence class induced by the equivalence relation "congruence". Our understanding of the remainder and the definition of the "mod" function are based on this equivalence class. The "remainder" or the result of a mod computation is a representative element of the equivalence class. By declaration we have chosen the smallest non-negative element (so -2 is not a valid candidate).

因此,当您阅读-2 mod 5 = x时,这会转化为找到最小的非负x,以便存在一个y * 5 = -2-x的整数y",这与全等的定义一致.如您所见,通过简单地尝试y的其他值,解决方案是y = 1和x = 3.

So when you read -2 mod 5 = x this translates to "Find the smallest non-negative x so that there exists an integer y with y * 5 = -2 - x", in concordance with the definition of congruence. The solution is y=1 and x = 3 as you can see by simply trying out other values for y.

这篇关于负模算子结果的目的是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-10 23:25