473,795 Members | 2,863 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Strange behavior of modulo in array index

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
IndexOutOfRange Exception 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?
Nov 16 '05 #1
11 2671
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******@discu ssions.microsof t.com> wrote in message
news:EA******** *************** ***********@mic rosoft.com...
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
IndexOutOfRange Exception 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?

Nov 16 '05 #2
Ben Blank <Be******@discu ssions.microsof t.com> wrote:
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
IndexOutOfRange Exception 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?


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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
"Stoitcho Goutsev (100) [C# MVP]" wrote:
-1 % 4 = -1 that is the correct result (0*4 - 1).


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.
Nov 16 '05 #4
"Jon Skeet [C# MVP]" wrote:
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.)


Ah! And that would be the thinko which caused this; thanks for pointing it
out. For the record (and anyone else making the same mistake I did),
changing j to an unsigned integer caused the expression to behave as I
expected it to.
Nov 16 '05 #5
Jon,

Actually this is exactly modulo operation according to the latest definition

"According to the newer convention, in general, a mod n is the remainder on
integer division of a by n. Depending on the implementation, the remainder r
is typically constrained to 0 < |r| < |n|, with a negative remainder only
resulting when n < 0."

--

Stoitcho Goutsev (100) [C# MVP]
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Ben Blank <Be******@discu ssions.microsof t.com> wrote:
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
IndexOutOfRange Exception 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?


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.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #6
Stoitcho Goutsev (100) [C# MVP] <10*@100.com> wrote:
Actually this is exactly modulo operation according to the latest definition

"According to the newer convention, in general, a mod n is the remainder on
integer division of a by n. Depending on the implementation, the remainder r
is typically constrained to 0 < |r| < |n|, with a negative remainder only
resulting when n < 0."


Well, that "typically" certainly isn't the case here (-1 % 2 == -1).

I dare say it depends on what definition you end up using, but it looks
like the OP would expect modulo to give a positive value but
understands that a remainder can be negative.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #7
Jon,
Actually this is exactly modulo operation according to the latest
definition

"According to the newer convention, in general, a mod n is the remainder
on
integer division of a by n. Depending on the implementation, the
remainder r
is typically constrained to 0 < |r| < |n|, with a negative remainder only
resulting when n < 0."
Well, that "typically" certainly isn't the case here (-1 % 2 == -1).


n % x = r
-1 % 2 = -1
this is exactly the case

r is negative because n is negative

I dare say it depends on what definition you end up using, but it looks
like the OP would expect modulo to give a positive value but
understands that a remainder can be negative.


Well, I'd expect the result to be positive if the operation was defined only
for positive numbers. In the case it is defined for negative as well I'd
expect negative result

--

Stoitcho Goutsev (100) [C# MVP]

Nov 16 '05 #8
Stoitcho Goutsev (100) [C# MVP] <10*@100.com> wrote:
Actually this is exactly modulo operation according to the latest
definition

"According to the newer convention, in general, a mod n is the remainder
on
integer division of a by n. Depending on the implementation, the
remainder r
is typically constrained to 0 < |r| < |n|, with a negative remainder only
resulting when n < 0."
Well, that "typically" certainly isn't the case here (-1 % 2 == -1).


n % x = r


No, a % n = r, using the terminology above.
-1 % 2 = -1
this is exactly the case

r is negative because n is negative


No it's not - n here is 2, it's a which is -1. Your definition is
talking about situations like 13 % -5.
I dare say it depends on what definition you end up using, but it looks
like the OP would expect modulo to give a positive value but
understands that a remainder can be negative.


Well, I'd expect the result to be positive if the operation was defined only
for positive numbers. In the case it is defined for negative as well I'd
expect negative result


Well, not by the definition you were using above, unless n is negative.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Well actually now I'm confused. Frankly, I haven't noticed that actually the
definition talks about a % n = r.

If it is the reminder of the integer devision I would say that 13 % -3 = -4
and reminder 1 it looks like the definition has an error it should be that
r < 0 when a < 0

--

Stoitcho Goutsev (100) [C# MVP]
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Stoitcho Goutsev (100) [C# MVP] <10*@100.com> wrote:
>> Actually this is exactly modulo operation according to the latest
>> definition
>>
>> "According to the newer convention, in general, a mod n is the
>> remainder
>> on
>> integer division of a by n. Depending on the implementation, the
>> remainder r
>> is typically constrained to 0 < |r| < |n|, with a negative remainder
>> only
>> resulting when n < 0."
>
> Well, that "typically" certainly isn't the case here (-1 % 2 == -1).


n % x = r


No, a % n = r, using the terminology above.
-1 % 2 = -1
this is exactly the case

r is negative because n is negative


No it's not - n here is 2, it's a which is -1. Your definition is
talking about situations like 13 % -5.
> I dare say it depends on what definition you end up using, but it looks
> like the OP would expect modulo to give a positive value but
> understands that a remainder can be negative.


Well, I'd expect the result to be positive if the operation was defined
only
for positive numbers. In the case it is defined for negative as well I'd
expect negative result


Well, not by the definition you were using above, unless n is negative.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2362
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
6
2937
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd have better luck here. So here goes! My program ignores any command line arguments, or at least it's supposed to. However, when I pass any command line arguments to the program, the behaviour of one of the functions changes mysteriously. I have...
2
1539
by: Bruno van Dooren | last post by:
Hi All, i have some (3) different weird pointer problems that have me stumped. i suspect that the compiler behavior is correct because gcc shows the same results. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
9
1871
by: Alan Schroeder | last post by:
The following code produces the expected results on a PC using gcc, but I need to port it (or least something similar) to a different platform/compiler. I don't think I've introduced any undefined behavior but would like another set of eyes to check. #include <math.h> #include <stdlib.h> extern float window;
2
3564
by: rkk | last post by:
Hi, My mergesort program is below. There is a small piece of logical error/bug in this code which I can't figure out, as a result the output array isn't completely sorted. Requesting your help to resolve this problem. Thanks in advance. #include <stdlib.h> #include <string.h> #include <stdio.h>
7
2216
by: deepak | last post by:
Using 'char' as an array index is an undefined behavior?
10
3503
by: kyagrd | last post by:
<code> #include <iostream> int main(void) { using namespace std; int p; int* p1 = p; int* p11 = p + 2;
5
1513
by: rconradharris | last post by:
A co-worker of mine came across some interesting behavior in the Python interpreter today and I'm hoping someone more knowledgeable in Python internals can explain this to me. First, we create an instance of an Old-Style class without defining a __contains__ but instead define a __getitem__ method in which we raise KeyError. Next we repeatedly use the 'in' operator to test to see whether something, a string, an int, etc is an attribute...
2
1594
by: KDawg44 | last post by:
Hi, I have the following code: $resultSet = mysql_query($sqlString); $numResults = mysql_num_rows($resultSet); echo "Num Results = " . $numResults . "<br/>"; for ($i = 0; $i < $numResults; $i++) { $row = mysql_fetch_array($resultSet); echo "i = " . $i . "<br/>";
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.