|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
07-13-2006 09:56
default { state_entry() { integer a = 10; integer b = 20; integer c = (a - b) % 100;
llSay(0, (string)c); } }
What would you expect to see? If I remember from when I tried this, it said -10. That can't be right, can it? I would expect 90. I looked it up, and apparently there are two definitions for how to do mod, Excel does it one way and Lotus does it a different way or something. As far as I remember, the Excel way seemed right to me  Anyway, has anyone else run into this? I was using this to compensate for clock rollover (where 'later' ends up being less than 'earlier'), and it took me a long time to figure out why the math wasn't working.
|
|
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
|
07-13-2006 10:09
I would expect -10 -- I ran into that exact problem in javascript, when I was writing a multi timezone clock.
I think it acts like that because of the definition of modulus-- the remainder left after an operation. -10 / 100 has a remainder of -10, so the modulus function will return -10. It's not too hard to do ( -10 + 100 ) % 100, so I wouldn't worry about it.
|
|
Ziggy Puff
Registered User
Join date: 15 Jul 2005
Posts: 1,143
|
07-13-2006 10:42
Well I'll be damned. C gives me -10 too. Never mind  I always viewed modulo as 'around a circle' - I guess you could call it 'clock math'  So -10 means 10 back from 100, which is 90. Yeah, the fix is easy once I figured it out 
|
|
Keknehv Psaltery
Hacker
Join date: 11 Apr 2005
Posts: 1,185
|
07-13-2006 11:13
Excel does return 90 for this, but Microsoft tends to not care about standards.
Here's one way to look at it-- it's a clock face, but it goes to both positive and negative 100. So 0 isn't the start, it's the middle. -161 % 100 = -61.
|
|
Eloise Pasteur
Curious Individual
Join date: 14 Jul 2004
Posts: 1,952
|
07-14-2006 00:13
The way I was taught modulo maths you can have numbers for modulo n in the range -(n-1) to (n-1). Your choice of values is interchangeable in a way that they're not in non-modulo maths, so -10 mod 100 === 90 mod 100 in a way that -10 != 90 in non-modulo maths.
Depending on your educational background it's often first taught (certainly still is in this country) before negative numbers and so the always positive results tend to settle into people' memories, and the option of -ve number results tends to slip by.
But if you think of clock maths, I'm writing at 0811 local time, that makes is -3:49 in my modulo 12 clock, or -1549 in a 24h clock - conveniently the answer to how long is it until noon and midnight respectively, so we use negative numbers (or we cheat and say before and after in general conversation) for our actual clock maths too.
|
|
Seronis Zagato
Verified Resident
Join date: 30 Aug 2005
Posts: 454
|
07-14-2006 01:42
There are plenty of times when you need the modulus result to always be the positive value. In this case something along the lines of:
integer MOD = 100; rand = -123437; //some value to get a mod of
value = ((rand % MOD) + MOD) % MOD;
doing a mod/incr/mod on your value will make sure whether it was originally positive or neg the end mod will be a pos value. hope it helps.
_____________________
From: Johnny Mann Just cause SL redefines what a videogame can be doesnt mean it isnt a game. From: Ash Venkman I beat SL. (The end guy is really hard.)
|
|
Seifert Surface
Mathematician
Join date: 14 Jun 2005
Posts: 912
|
07-14-2006 02:01
... == -110 == -10 == 90 == 190 == 290 == ... (all mod 100).
But it beats me why you'd want to get an answer out that wasn't between 0 and 99.
_____________________
-Seifert Surface 2G!tGLf 2nLt9cG
|