了解余数运算符
Understanding Remainder operator
只是做一些基本的模运算,并试图用问号围绕下面的运算。
0%5 // 0 - Totally understand
1%5 // 1 ?
2%5 // 2 ?
3%5 // 3 ?
4%5 // 4 ?
5%5 // 0 - Totally understand
也许我的想法不对。例如 1/5 会 return 0.2 的 Double
而不是单个 integer
那么它如何 return 1
的余数?
这些我都明白了。说的有道理,但是上面的我想不通。
9%4 // 1
10%2 // 0
10%6 // 4
如果有人能解释一下那就太好了。看来我脑子放屁了。 Source of learning。
从您 link 到的同一 Basic Operators 页面:
The remainder operator (a % b
) works out how many multiples of b
will fit inside a
and returns the value that is left over (known as the remainder).
专用于1 % 5
:
5
不适合 1
,因此适合 0
次。
这意味着 1
可以描述为
1 = (5 * multiplier) + remainder
因为乘数是0
,所以remainder
是1
1 = (5 * 0) + remainder
1 = remainder
如果我们改为查看 6 % 5
,余数也是 1
。这是因为 5
适合 6
一次:
6 = (5 * multiplier) + remainder
6 = (5 * 1) + remainder
6-5 = remainder
1 = remainder
这个 / 除法运算符当你说 1/5
如果除法是整数它会给出 0
,但是这个 1.0/0.5
当你在 Double 中制作它时,它会给出 0.2
但是 % 模运算符当你说 1%5 = 1
因为你有 1 = 0*5 + 1
这意味着 1 有零个 5
和提醒是 1
只是做一些基本的模运算,并试图用问号围绕下面的运算。
0%5 // 0 - Totally understand
1%5 // 1 ?
2%5 // 2 ?
3%5 // 3 ?
4%5 // 4 ?
5%5 // 0 - Totally understand
也许我的想法不对。例如 1/5 会 return 0.2 的 Double
而不是单个 integer
那么它如何 return 1
的余数?
这些我都明白了。说的有道理,但是上面的我想不通。
9%4 // 1
10%2 // 0
10%6 // 4
如果有人能解释一下那就太好了。看来我脑子放屁了。 Source of learning。
从您 link 到的同一 Basic Operators 页面:
The remainder operator (
a % b
) works out how many multiples ofb
will fit insidea
and returns the value that is left over (known as the remainder).
专用于1 % 5
:
5
不适合 1
,因此适合 0
次。
这意味着 1
可以描述为
1 = (5 * multiplier) + remainder
因为乘数是0
,所以remainder
是1
1 = (5 * 0) + remainder
1 = remainder
如果我们改为查看 6 % 5
,余数也是 1
。这是因为 5
适合 6
一次:
6 = (5 * multiplier) + remainder
6 = (5 * 1) + remainder
6-5 = remainder
1 = remainder
这个 / 除法运算符当你说 1/5
如果除法是整数它会给出 0
,但是这个 1.0/0.5
当你在 Double 中制作它时,它会给出 0.2
但是 % 模运算符当你说 1%5 = 1
因为你有 1 = 0*5 + 1
这意味着 1 有零个 5
和提醒是 1