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? 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?
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
"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.
"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.
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
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
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]
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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)
|
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...
|
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)
|
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;
|
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>
| |
by: deepak |
last post by:
Using 'char' as an array index is an undefined behavior?
|
by: kyagrd |
last post by:
<code>
#include <iostream>
int main(void)
{
using namespace std;
int p;
int* p1 = p;
int* p11 = p + 2;
|
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...
|
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/>";
|
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...
|
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...
| |
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...
|
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,...
|
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...
|
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...
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |