math - How to get the true Euclidean remainder? -
i have 2 unsigned long
s a
, q
, find number n between 0 , q-1 such n + divisible q (without overflow).
in other words, i'm trying find (portable) way of computing (-a)%q
lies between 0 , q-1. (the sign of expression implementation-defined in c89.) what's way this?
what you're looking mathematically equivalent (q - a) mod q, in turn equivalent (q - (a mod q)) mod q. think should therefore able compute follows:
unsigned long result = (q - (a % q)) % q;
Comments
Post a Comment