tags:

views:

136

answers:

4

This silly 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.

+7  A: 

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
A: 

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

while (num >= mod_limit)
    num = num - mod_limit
tloflin
@tloflin, hope you don't mind but it should have been ">=" rather than ">".
paxdiablo
@paxdiablo, true, thanks.
tloflin
by not working performance-wise, did you mean like when num is around 2**63 and mod_limit is 3?
GregS
+4  A: 

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

Strelok
A: 

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