This scripting language doesn't have a % or Mod(). I do have a Fix() that chops off the decimal part of a number. I only need positive results, so don't get too robust.
-
2Could you mention and maybe tag which "silly" scripting language you're talking about? – Stephen P May 03 '10 at 23:58
-
1I think it's a silly language called "homework" – Gareth May 04 '10 at 00:37
-
2Heh. It's some embedded language on this Roku digital signage video player. It probably does have a Mod somewhere but I sure can't find it and it has like Arctan() and NaturalLog() so I'm really confused how they skipped Mod. – tladuke May 04 '10 at 00:53
-
glsl before 3.0 also lacks mod for integers – Soylent Graham Apr 17 '15 at 14:32
7 Answers
Will
// mod = a % b
c = Fix(a / b)
mod = a - b * c
do? I'm assuming you can at least divide here. All bets are off on negative numbers.

- 7,551
- 31
- 64
-
depending on what you want for negative numbers, it can be adjusted. there is `fix()` that always truncates and there is `int()` that always rounds downwards (`int(-2.5)=-3`) – Nas Banov Dec 30 '13 at 03:16
For posterity, BrightScript now has a modulo operator, it looks like this:
c = a mod b

- 28,347
- 6
- 48
- 67
-
-
4@MDXF - it does not provide answer to the *title* of the question but materially answers is: if you read the body, it says "This silly scripting language doesn't have a % or Mod()" - that was the case at 2010 but no more. – Nas Banov Sep 28 '17 at 16:10
If someone arrives later, here are some more actual algorithms (with errors...read carefully)
https://eprint.iacr.org/2014/755.pdf
There are actually two main kind of reduction formulae: Barett and Montgomery. The paper from eprint repeat both in different versions (algorithms 1-3) and give an "improved" version in algorithm 4.
Overview
I give now an overview of the 4. algorithm:
1.) Compute "A*B" and Store the whole product in "C" that C and the modulus $p$ is the input for that algorithm.
2.) Compute the bit-length of $p$, say: the function "Width(p)" returns exactly that value.
3.) Split the input $C$ into N "blocks" of size "Width(p)" and store each in G. Start in G[0] = lsb(p) and end in G[N-1] = msb(p). (The description is really faulty of the paper)
4.) Start the while loop: Set N=N-1 (to reach the last element) precompute $b:=2^{Width(p)} \bmod p$
while N>0 do:
T = G[N]
for(i=0; i<Width(p); i++) do: //Note: that counter doesn't matter, it limits the loop)
T = T << 1 //leftshift by 1 bit
while is_set( bit( T, Width(p) ) ) do // (N+1)-th bit of T is 1
unset( bit( T, Width(p) ) ) // unset the (N+1)-th bit of T (==0)
T += b
endwhile
endfor
G[N-1] += T
while is_set( bit( G[N-1], Width(p) ) ) do
unset( bit( G[N-1], Width(p) ) )
G[N-1] += b
endwhile
N -= 1
endwhile
That does alot. Not we only need to recursivly reduce G[0]:
while G[0] > p do
G[0] -= p
endwhile
return G[0]// = C mod p
The other three algorithms are well defined, but this lacks some information or present it really wrong. But it works for any size ;)
-
Hi @shalec - we do not encourage answers that only contain links to outside resources. These [link-only-answers are discouraged](https://meta.stackexchange.com/questions/8231/are-answers-that-just-contain-links-elsewhere-really-good-answers). – Lix Nov 15 '17 at 13:56
-
Can you copy and paste the relevant parts of the linked PDF into your answer? That would greatly improve the quality of this post. – JonathanDavidArndt Nov 15 '17 at 14:11
-
What language is it?
A basic algorithm might be:
hold the modulo in a variable (modulo);
hold the target number in a variable (target);
initialize modulus variable;
while (target > 0) {
if (target > modulo) {
target -= modulo;
}
else if(target < modulo) {
modulus = target;
break;
}
}

- 5,818
- 3
- 43
- 55

- 714
- 1
- 6
- 22
-
I think there is an error for the case where `target==modulo`. Infinite loop. – Ice-Blaze Jan 15 '17 at 18:36
This may not work for you performance-wise, but:
while (num >= mod_limit)
num = num - mod_limit
-
@tloflin, hope you don't mind but it should have been ">=" rather than ">". – paxdiablo May 04 '10 at 00:00
-
3by not working performance-wise, did you mean like when num is around 2**63 and mod_limit is 3? – President James K. Polk May 04 '10 at 00:05
In javascript:
function modulo(num1, num2) {
if (num2 === 0 || isNaN(num1) || isNaN(num2)) {
return NaN;
}
if (num1 === 0) {
return 0;
}
var remainderIsPositive = num1 >= 0;
num1 = Math.abs(num1);
num2 = Math.abs(num2);
while (num1 >= num2) {
num1 -= num2
}
return remainderIsPositive ? num1 : 0 - num1;
}

- 1
-
4While this code snippet may solve the question, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-code-based-answers) really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. Please also try not to crowd your code with explanatory comments, this reduces the readability of both the code and the explanations! – kayess Mar 10 '17 at 08:48