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.

4
  • 2
    Could you mention and maybe tag which "silly" scripting language you're talking about?
    – Stephen P
    Commented May 3, 2010 at 23:58
  • 1
    I think it's a silly language called "homework"
    – Gareth
    Commented May 4, 2010 at 0:37
  • 2
    Heh. 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
    Commented May 4, 2010 at 0:53
  • glsl before 3.0 also lacks mod for integers Commented Apr 17, 2015 at 14:32

7 Answers 7

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.

1
  • 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
    Commented Dec 30, 2013 at 3:16
7

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

c = a mod b
2
  • This does not provide an answer to the question.
    – MD XF
    Commented Sep 27, 2017 at 3: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
    Commented Sep 28, 2017 at 16:10
6

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

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

3
  • Hi @shalec - we do not encourage answers that only contain links to outside resources. These link-only-answers are discouraged.
    – Lix
    Commented Nov 15, 2017 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. Commented Nov 15, 2017 at 14:11
  • Of cause. There are 4 algorithms mentioned. I will edit those.
    – Shalec
    Commented Nov 15, 2017 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;
  }
}
1
  • I think there is an error for the case where target==modulo. Infinite loop.
    – Ice-Blaze
    Commented Jan 15, 2017 at 18:36
0

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

while (num >= mod_limit)
    num = num - mod_limit
2
  • @tloflin, hope you don't mind but it should have been ">=" rather than ">".
    – paxdiablo
    Commented May 4, 2010 at 0:00
  • 3
    by not working performance-wise, did you mean like when num is around 2**63 and mod_limit is 3? Commented May 4, 2010 at 0:05
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;
}
1
  • 4
    While this code snippet may solve the question, including an explanation 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
    Commented Mar 10, 2017 at 8:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.