objective-c 百分比符号 (%) 在 Objective C 中的数学作用是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22599292/
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-09-03 21:13:32  来源:igfitidea点击:

What does a percentage sign (%) do mathematically in Objective C?

objective-c

提问by Aero Wang

I am super confused what the percentage sign does in Objective C. Can someone explain to me in language that an average idiot like myself can understand?! Thanks.

我对 Objective C 中百分比符号的作用感到非常困惑。有人可以用像我这样的普通白痴可以理解的语言向我解释吗?!谢谢。

回答by Michael Dautermann

Same as what it does in C, it's "modulo" (also known as integer remainder).

与在 C 中所做的相同,它是“模”(也称为整数余数)。

回答by Charles Martin

% is the modulo operator, so for example 10 % 3 would result in 1.

% 是模运算符,因此例如 10 % 3 将导致 1。

If you have some numbers aand b, a % bgives you just the remainder of adivided by b.

如果您有一些数字aba % b则只给出a除以的余数b

So in the example 10 % 3, 10 divided by 3 is 3 with remainder 1, so the answer is 1.

所以在这个例子中10 % 3,10 除以 3 是 3,余数是 1,所以答案是 1。

If there is no remainder to adivided by b, the answer is zero, so for example, 4 % 2 = 0.

如果a除以没有余数b,则答案为零,例如,4 % 2 = 0

Here's a relevant SO question about modular arithmetic.

这是一个关于模算术的相关 SO 问题

回答by Jojodmo

%is the modulo operator. It returns the remainder of <number> / <number>. For example:

%是模运算符。它返回 的余数<number> / <number>。例如:

5 % 2 

means 5 / 2, which equals 2 with a remainder of 1, so, 1is the value that is returned. Here's some more examples:

mean 5 / 2,等于 2 余数为 1,因此,1是返回的值。以下是更多示例:

3 % 3 == 0 //remainder of 3/3 is 0
6 % 3 == 0 //remainder of 6/3 is 0
5 % 3 == 2 //remainder of 5/3 is 2
15 % 4 == 3 //remainder of 15/4 is 3
99 % 30 == 9 //remainder of 99/30 is 9

The definition of modulo is:

模数的定义是:

mod·u·lo

(in number theory) with respect to or using a modulus of a specified number. Two numbers are congruent modulo a given number if they give the same remainder when divided by that number.

模·u·洛

(在数论中)关于或使用指定数的模数。如果两个数字在除以该数字时得到相同的余数,则它们对给定数字取模是全等的。