模数在数组索引中的奇怪行为 [英] Strange behavior of modulo in array index
问题描述
我有一个以特定顺序迭代数组的循环:
for(j = 0; j <16; j ++)T [(4 - j)% 4] = ...;
循环在j = 0到j = 4时正常进行,但是在j = 5时给出了一个
的IndexOutOfRangeException。看守窗口似乎
表示模数被完全忽略:
j = 0x5(int)
T [ (4 - j)%4] =错误:索引''4-j%4''越界指针/数组''T''
(4 - j)%4 = 0xffffffff (int)
4 - j = 0xffffffff(int)
0xffffffff%4 = 0x3(长)
我''我很难过。有什么想法吗?
I have a loop which iterates over an array in a particular order:
for (j = 0; j < 16; j++) T[(4 - j) % 4] = ...;
The loop proceeds normally for j = 0 through j = 4, but gives an
IndexOutOfRangeException at j = 5. Playing with the watch window seems to
indicate that the modulo is being completely ignored:
j = 0x5 (int)
T[(4 - j) % 4] = error: index ''4-j%4'' out of bound for pointer/array ''T''
(4 - j) % 4 = 0xffffffff (int)
4 - j = 0xffffffff (int)
0xffffffff % 4 = 0x3 (long)
I''m stumped. Any ideas?
推荐答案
嗨Ben,
-1%4 = -1这是正确的结果(0 * 4 - 1)。
除非T是您自己编写的集合并且它支持
否定索引或dictionalry任何其他.NET集合或数组将
抛出异常
-
HTH
Stoitcho Goutsev(100)[C#MVP ]
Ben Blank <成为****** @ discussions.microsoft.com>在消息中写道
新闻:EA ********************************** @ microsof t.com ...
Hi Ben,
-1 % 4 = -1 that is the correct result (0*4 - 1).
Unless T is collection that you have written by yourself and it supposrts
negative indices or dictionalry any other .NET collection or array will
throw that exception
--
HTH
Stoitcho Goutsev (100) [C# MVP]
"Ben Blank" <Be******@discussions.microsoft.com> wrote in message
news:EA**********************************@microsof t.com...
我有一个以特定顺序迭代数组的循环:
for(j = 0; j< 16; j ++)T [(4-j)%4] = ...;
循环在j = 0到j = 4时正常进行,但在j = 5时给出了一个
IndexOutOfRangeException。监视窗口似乎表明模数被完全忽略:
j = 0x5(int)
T [(4 - j)%4] =错误:索引''4-j%4'超出指针/数组的范围''T''(4-j)%4 = 0xffffffff(int)
4 - j = 0xffffffff(int)
0xffffffff%4 = 0x3(长)
我很难过。有什么想法吗?
I have a loop which iterates over an array in a particular order:
for (j = 0; j < 16; j++) T[(4 - j) % 4] = ...;
The loop proceeds normally for j = 0 through j = 4, but gives an
IndexOutOfRangeException at j = 5. Playing with the watch window seems to
indicate that the modulo is being completely ignored:
j = 0x5 (int)
T[(4 - j) % 4] = error: index ''4-j%4'' out of bound for pointer/array ''T''
(4 - j) % 4 = 0xffffffff (int)
4 - j = 0xffffffff (int)
0xffffffff % 4 = 0x3 (long)
I''m stumped. Any ideas?
Ben Blank< Be ****** @ discussion.microsoft.com>写道:
Ben Blank <Be******@discussions.microsoft.com> wrote:
我有一个以特定顺序迭代数组的循环:
for(j = 0; j< 16; j ++)T [(4 - j)%4] = ...;
循环在j = 0到j = 4时正常进行,但是在j = 5时给出了一个
IndexOutOfRangeException。用监视窗口看似乎
表示模数被完全忽略:
j = 0x5(int)
T [(4 - j)%4] =错误:索引''4- j%4''越界指针/数组''T''(4 - j)%4 = 0xffffffff(int)
4 - j = 0xffffffff(int)
0xffffffff %4 = 0x3(长)
我很难过。任何想法?
I have a loop which iterates over an array in a particular order:
for (j = 0; j < 16; j++) T[(4 - j) % 4] = ...;
The loop proceeds normally for j = 0 through j = 4, but gives an
IndexOutOfRangeException at j = 5. Playing with the watch window seems to
indicate that the modulo is being completely ignored:
j = 0x5 (int)
T[(4 - j) % 4] = error: index ''4-j%4'' out of bound for pointer/array ''T''
(4 - j) % 4 = 0xffffffff (int)
4 - j = 0xffffffff (int)
0xffffffff % 4 = 0x3 (long)
I''m stumped. Any ideas?
它根本不被忽视。 x%y的范围是(-y,y)(注意
边界的排他性)。
注意它是余数运算符,而不是模数运算符,这就是为什么存在差异的原因。 (没有模运算符。)
-
Jon Skeet - < sk *** @ pobox.com>
http://www.pobox.com/~skeet
如果回复小组,请不要给我发邮件
It''s not being ignored at all. The range for x % y is (-y, y) (note
exclusivity of bounds).
Note that it''s the remainder operator, not the modulo operator, which
is why there''s the difference. (There''s no modulo operator.)
--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
" Stoitcho Goutsev(100)[C#MVP]"写道:
"Stoitcho Goutsev (100) [C# MVP]" wrote:
-1%4 = -1这是正确的结果(0 * 4 - 1)。
-1 % 4 = -1 that is the correct result (0*4 - 1).
奇数。我的印象是模N总是会在0 ... N-1的范围内返回
的正结果。我将不得不玩
将j改为无符号整数。感谢您的快速回复。
Odd. I was under the impression that modulo N would always return a
positive result in the range of 0 .. N-1. I''ll have to play around with
changing j to an unsigned integer instead. Thanks for your quick response.
这篇关于模数在数组索引中的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!