12

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.

tladuke
  • 1,337
  • 2
  • 11
  • 22

7 Answers7

21

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.

John Källén
  • 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
7

For posterity, BrightScript now has a modulo operator, it looks like this:

c = a mod b
Nas Banov
  • 28,347
  • 6
  • 48
  • 67
  • This does not provide an answer to the question. – MD XF Sep 27 '17 at 03:10
  • 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
6

a mod n = a - (n * Fix(a/n))

Strelok
  • 50,229
  • 9
  • 102
  • 115
2

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 ;)

Community
  • 1
  • 1
Shalec
  • 172
  • 9
  • 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
  • Of cause. There are 4 algorithms mentioned. I will edit those. – Shalec Nov 15 '17 at 14:25
1

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;
  }
}
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
user97410
  • 714
  • 1
  • 6
  • 22
0

This may not work for you performance-wise, but:

while (num >= mod_limit)
    num = num - mod_limit
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
tloflin
  • 4,050
  • 1
  • 25
  • 33
0

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;
}
  • 4
    While 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