English 中文(简体)
“变式”和“试金”之间的区别是什么?
原标题:What s the difference between “mod” and “remainder”?

我的朋友说,“模拟”和“再提交”之间存在分歧。

如果是的话,C和C++的这些差异是什么? 百分比是否意味着C中的“mod”或“rem”?

问题回答

模块组合(Euclidean Division)和其余部分(Cs %营运人)之间存在差异。 例如:

<>条码> 21<>>> 代码> 由于<代码>-21 + 4 x 6,3

But -21 divided by 4 with truncation towards 0 (as C s / operator)
gives -5 with a remainder (C -21 % 4) of -1.

对积极价值而言,Euclidean和拖拉分裂之间没有差别。

https://en.wikipedia.org/wiki/Euclidean_igu# Other_intervals_for_the_remainder - 从C99开始,需要Cs选择将其余部分推向0()。 甚至在C89,当Euclidean的分区被/标准所允许时。

如引人<代码>a/b>可代议,则<代码>(a/b)*b + a%b 代码

www.un.org/ga/ga

百分比是否意味着C中的“mod”或“rem”?

在C,%remainder1

......,<代码>/的操作者是片面的报价人,任何片面部分被弃置...... (这常常被称为“向零倾斜”。) C11dr §6.5 6

<>%<>>>>>%的操作者应有分类。 C11dr §6.5 2

<>>>/的操作者是第一个操作体的引人;<>%<>>>> 代码/代码>操作者为remainder. C11dr §6.5 5


“变式”和“试金”之间的区别是什么?

C没有界定“mod”或“modulo”的操作者/功能,例如Euclidean Division otherlo

C defines remainder.

让我们把每条<>%的操作者与Euclidean“mod”相比较。

"Euclidean mod" differs from C s a%b operation when a is negative.

 // a % b, the remainder after an integer division that truncates toward 0.
 7 %  3 -->  1  
 7 % -3 -->  1  
-7 %  3 --> -1  
-7 % -3 --> -1   

“Mod”或模块化,如Euclidean分裂。 其结果总是零或积极。

 7 modulo  3 -->  1
 7 modulo -3 -->  1
-7 modulo  3 -->  2
-7 modulo -3 -->  2

候选人模块法:

int modulo_Euclidean(int a, int b) {
  int m = a % b;
  if (m < 0) {
    // m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
    m = (b < 0) ? m - b : m + b;
  }
  return m;
}

关于浮动点的说明:double fmod (double x, Double y),即使称为“fmod”,但与C integer的其余部分类似:

www.un.org/Depts/DGACM/index_french.htm C11dr §7.12.10.1 2

fmod( 7,  3) -->  1.0
fmod( 7, -3) -->  1.0
fmod(-7,  3) --> -1.0
fmod(-7, -3) --> -1.0

Disambiguation: C还具有类似的名称职能:double modf(双重价值,双重*iptr),这些功能将论点价值分为整体和分部分,每一部分都与论点相同。 这与这里的“混合”讨论没有什么关系,但名称相似。


[2020年12月]

对于希望在所有案件中都具备适当功能的人来说,经过改进的<代码>modulo_Euclidean(,1)检测到<代码>mod(x,0)和2)一种良好且无分辨率的结果modulo_Euclidean2(INT_MIN,-1)。 https://codereview.stackchange.com/q/253799/29485 模块化的不同实施,有充分定义的行为:

int modulo_Euclidean2(int a, int b) {
  if (b == 0) TBD_Code(); // perhaps return -1 to indicate failure?
  if (b == -1) return 0; // This test needed to prevent UB of `INT_MIN % -1`.
  int m = a % b;
  if (m < 0) {
    // m += (b < 0) ? -b : b; // avoid this form: it is UB when b == INT_MIN
    m = (b < 0) ? m - b : m + b;
  }
  return m;
}

<>1> 在C99之前,C s对%的界定仍为remainder,但随后,/允许否定的报价人进行四舍五入,而不是“限制零”。 见。 因此,在C99前的一些汇编中,%代码可像Euclidean“mod”司那样行事。 以上<代码>modulo_Euclidean()也将与这一替代的旧学校合作。

[Edit Apr 2024]
No sign
C2X will offer any help here.

其余部分的表示与分歧相同,单元表示与分歧相同。

Remainder is simply the remaining part after the arithmetic division between two integer number whereas Modulus is the sum of remainder and divisor when they are oppositely signed and remaining part after the arithmetic division when remainder and divisor both are of same sign.

<>Remainder>Example:

10% 3 = 1 [如果分歧为10个,得到积极签署,结果也将得到积极签署]

- 10 % 3 =-1 [如果存在分歧——10,消极签字,结果也将被否定签名]

10 % -3 = 1 [如果分辨率是10分辨率,结果也将得到积极签名]

- 10 % -3 =-1 [如果存在分歧,10 表示消极签字,结果也将被否定签名]

<>Modulus>Example:

5 % 3 = 2 [如果分歧为5个,得到积极的签署,其余部分也将得到积极的签署,分歧者也得到积极的签署。 由于其余部分和分歧都相同,结果将与剩余部分相同]

5% 3 = 1 [如果存在分歧,5个被否定签字,其余部分也将被否定签字,而分歧者则得到积极的签署。 其余部分和分歧都相反,结果将是剩余部分的总和2+3 = 1

5 % -3 = -1 [here divisible is 5 which is positively signed so the remainder will also be positively signed and the divisor is negatively signed. As both remainder and divisor are of opposite sign the result will be sum of remainder and divisor 2 + -3 = -1]

5%-3 = -2[如果存在5个负面签字,其余部分也将被否定签名,而且分歧也得到消极签字。 由于其余部分和分歧都相同,结果将与剩余部分相同]

I hope this will clearly distinguish between remainder and modulus.

在C和C++及许多语文中,%是其余NOT模块操作员。

For example in the operation -21 / 4 the integer part is -5 and the decimal part is -.25. The remainder is the fractional part times the divisor, so our remainder is -1. JavaScript uses the remainder operator and confirms this

console.log(-21 % 4 == -1);

模块操作员与你一样有“锁”。 在12个 o锁、3个 o锁、6个 o锁和9个 o锁位置上,一只圈子。 通过我们的组合行动,或以消极的引人为例,用反锁的方式,把我们的土地推向我们。

Note: Modulus is always the same sign as the divisor and remainder the same sign as the quotient. Adding the divisor and the remainder when at least one is negative yields the modulus.

在你再次提到的模块算法中,是算术后剩余的价值或剩余价值。 通常称为其余部分。 %为C/C++的其余运营商。 例:

7 % 3 = 1  // dividend % divisor = remainder

有待讨论的内容是如何处理这一百分比业务的负面投入。 现代C和C++为这项行动提供了一份签名的剩余价值,其中结果的标语总是与红利投入相吻合,而没有考虑到分裂分子的投入。

% is a remainder(leftover after dividend / divisor) NOT modulus. 

您可使用其余部分(按关系分列的百分比)撰写自己的组合功能。

  ((n%m)+m)%m

  where `n` is the given number and `m` is the modulus

Find below the difference between the remainder and modulus values for the range n = (-7,7) and m = 3

n       -7 -6 -5 -4 -3 -2 -1  0  1  2  3  4  5  6  7  
------------------------------------------------------------------------- 
%(-m)   -1  0 -2 -1  0 -2 -1  0  1  2  0  1  2  0  1  =>  remainder
% m     -1  0 -2 -1  0 -2 -1  0  1  2  0  1  2  0  1  =>  remainder
mod m    2  0  1  2  0  1  2  0  1  2  0  1  2  0  1  =>  ((n%m)+m)%m
mod(-m) -1  0 -2 -1  0 -2 -1  0 -2 -1  0 -2 -1  0 -2  =>  ((n%m)+m)%m

Tips to.15/:

n%(-m)   = +(remainder)
(-n)%(m) = -(remainder)
sign of  m  doesn t matter

n mod (-m) = -(result)
(-n) mod m = +(result)
sign of  n  doesn t matter

For +ve  n  and  %(-m)  or  %m  or  mod m  gives the same remainder




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签