473,480 Members | 2,592 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

modulo

Mathematically, r = a modulo b (b being the divisor) is defined such
that if b <0, and if r <>0, its sign should be the sign of b.

C# does not follow this definition.

int indx = -1;
indx %= 100;
will let indx == -1, not == 99 as it should (mathematical definition).

Is that "by specification"? and if so, is there an alternate predefined
'modulo' operator which behaves like the mathematical definition?
Thanks for your time,
Vanderghast, Access MVP
Sep 25 '07 #1
3 3210
On Sep 25, 11:11 am, "Michel Walsh"
<vanderghast@VirusAreFunnierThanSpamwrote:
Mathematically, r = a modulo b (b being the divisor) is defined such
that if b <0, and if r <>0, its sign should be the sign of b.

C# does not follow this definition.

int indx = -1;
indx %= 100;

will let indx == -1, not == 99 as it should (mathematical definition).

Is that "by specification"? and if so, is there an alternate predefined
'modulo' operator which behaves like the mathematical definition?
C# takes the sign of the dividend, not divisor. (There is another
thread recently discussing this very thing).
a = dividend;
n = divisor;
r = a - n * Floor(a / n) when result of a/n is positive
r = a - n * Cieling(a / n) when result of a/n is negative.

As a result of this, r takes the sign of the dividend...

Besides, longhand, the remainder comes up -1 <g>
-1 % 100 = -1:

___0_____
100 | -1
0
______
-1 <--- remainder

1 % -100 = 1:

___0_____
-100 | 1
0
______
1 <--- remainder
Sep 25 '07 #2
On Sep 25, 4:11 pm, "Michel Walsh"
<vanderghast@VirusAreFunnierThanSpamwrote:
Mathematically, r = a modulo b (b being the divisor) is defined such
that if b <0, and if r <>0, its sign should be the sign of b.

C# does not follow this definition.

int indx = -1;
indx %= 100;

will let indx == -1, not == 99 as it should (mathematical definition).

Is that "by specification"? and if so, is there an alternate predefined
'modulo' operator which behaves like the mathematical definition?
Yes, it's by specification. It's also what many (but not all) other
languages do.

I believe it's partly the choice due to division rounding towards 0.
So if you consider:

div = x / y;
rem = x % y;

x = div*y + rem

If x is -1 and y is 100, then div will be 0, and rem is -1.
The alternative would be for div to be -1 with rem as 99, *or* make
the above relation not hold.

Jon

Sep 25 '07 #3
Thanks to everyone for the answers.

Vanderghast, Access MVP
Sep 25 '07 #4

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

Similar topics

5
4837
by: Griff | last post by:
Test program: =============================================================== t = 5.8 interval = 2.0 while t < 6.1: print "%s mod %s = %s " % (t, interval, t % interval ) t += 0.1...
3
2439
by: Tjerk Wolterink | last post by:
I posted my problem earlier, but i simplified the examples, and i know what the cause of the problem is, but i dont know the solution, my xml file: <?xml version="1.0" encoding="ISO-8859-1"?>...
1
3106
by: jt | last post by:
Looking up into Crypto.PublicKey.RSA, I see there is a computed value named "u" for which I can't see the use. The value of "u" is the inverse of p modulo q, in the code: obj.u =...
10
2067
by: john blackburn | last post by:
Hi, I am trying to get an extremely simple character string encryption function to work using modulo 10 arithmetic. void ccencode(UNSIGNED8* pattern) { UNSIGNED8 key = CCKEY; UNSIGNED8...
2
5338
by: Alexander | last post by:
I played a little bit with modulo cause I wanted to implement a small parser for functions as I stumbled over a calculation error. First I thought the reason might be a type conversion I did...
11
2650
by: Ben Blank | last post by:
I have a loop which iterates over an array in a particular order: for (j = 0; j < 16; j++) T = ...; The loop proceeds normally for j = 0 through j = 4, but gives an IndexOutOfRangeException at...
12
2824
by: Chadwick Boggs | last post by:
I need to perform modulo operations on extremely large numbers. The % operator is giving me number out of range errors and the mod(x, y) function simply seems to return the wrong results. Also,...
4
18256
by: inkexit | last post by:
I'm writing a basic piece of software that will tell me how many shares of stock I can buy if the shares cost X each and I have Y to spend. I have it coded as: int main() { float price,...
5
3013
by: shumaker | last post by:
The VS help files list % as the modulo operator, but I read in another post that someone said it was the remainder operator. I'm getting -2 % 64 = -2, but I need 62 or -62, because I am trying...
0
7055
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,...
0
6920
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7060
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,...
0
7106
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...
1
6760
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...
0
7022
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...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3013
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...
0
3004
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.