modulo

Modulo of a negative number

纵然是瞬间 提交于 2019-12-04 09:23:24
Consider the following expression: (a - b) mod N Which of the following is equivalent to the above expression? 1) ((a mod N) + (-b mod N)) mod N 2) ((a mod N) - (b mod N)) mod N Also, how is (-b mod N) calculated, i.e., how is the mod of a negative number calculated? Thanks. I don't want to bother you with some complex mathematical concepts, so i'll try to keep it simple. When we say that a = b (mod c), we simply say that a-b is a multiple of c. This means that when we want to know what is the value of a mod c, saying that it is a or a-c or a+c or a+1000*c is true. Thus, your 2 formulas are

MySQL Case Select not behaving as expected

牧云@^-^@ 提交于 2019-12-04 06:12:28
问题 While creating a more complex stored procedure in MySQL, I encountered a weird problem with my CASE statement. I have simplified my procedure to show the issue. I am selecting three things per loop to clarify the problem: the count variable, the modTemp variable before the CASE, and then a number representing which CASE path was taken. DELIMITER // CREATE PROCEDURE `AddPlants` (IN plantNum int) BEGIN DECLARE count int; DECLARE modTemp int; SET count = 1; WHILE count <= plantNum DO SELECT

Unable to use '%' in glsl

ⅰ亾dé卋堺 提交于 2019-12-04 04:24:29
While writing a shader program today, I encountered a situation where I have to use % to find the remainder. GLSL gave me an error saying that it is not available in the current version. I've tried several problems. GLSL doesn't support recursive function and while loops, which is needed if I want to create a function that can give me the result of (a % b) . So, I'm currently stuck. Can someone help me with this? Edit . I was trying to emulate a CRT screen using some shader code from this website as reference code. I wanted to modify the color of the pixels at certain row and columns, so I

Python OpenCV cv.WaitKey spits back weird output on Ubuntu modulo 256 maps correctly

白昼怎懂夜的黑 提交于 2019-12-04 03:08:09
I am running Ubuntu 11.10 (Lenovo T400) with OpenCV 2.2 (I believe as imports are done as import cv2.cv as cv). This problem also happens if i just 'import cv' instead. I recently started having this problem, and it's kind of a weird one. I don't know anything significant I did, I have restarted since it started happening. I installed a couple programs, but I don't think those would affect this. When I run with an artificial image showing (just a black image), I try to poll cv.WaitKey(10). It spits back garbage. Here's my OpenCV code: import cv2.cv as cv import time cv.NamedWindow("camera", 1)

Algorithm to find Sum of the first r binomial coefficients for fixed n modulo m

妖精的绣舞 提交于 2019-12-03 18:13:23
I am trying to find the sum of the first r binomial coefficients for a fixed n. (nC1 + nC2 + nC3 + ... + nCr) % M where r < = n. Is there an efficient algorithm to solve this problem ? Note that the "first" binomial coefficient for fixed n is nC0 . Let f(n) = nC0 + nC1 + ... + nC(r-1) . Using the "Pascal's triangle" identity, nCk = (n-1)C(k-1) + (n-1)Ck we have nC0 + nC1 + nC2 + ... + nC(r-1) = (n-1)C(-1) + (n-1)C0 + (n-1)C0 + (n-1)C1 + (n-1)C1 + (n-1)C2 + ... + (n-1)C(r-2) + (n-1)C(r-1) = 2[(n-1)C0 + (n-1)C1 + (n-1)C2 + ... + (n-1)C(r-2)] + (n-1)C(r-1) = 2[(n-1)C0 + ... + (n-1)C(r-1)] - (n-1

Is it possible to rewrite modulo (2^n - 1) using bitwise and restricted operators

两盒软妹~` 提交于 2019-12-03 17:02:57
问题 For unsigned int x, is it possible to calculate x % 255 (or 2^n - 1 in general) using only the following operators (plus no loop, branch or function call)? ! , ~ , & , ^ , | , + , << , >> . 回答1: Yes, it's possible. For 255, it can be done as follows: unsigned int x = 4023156861; x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); x = (x & 255) + (x >> 8); // At this point, x will be in the range: 0 <= x < 256. // If the answer 0, x could potentially be 255 which is

Fast way to manually mod a number

霸气de小男生 提交于 2019-12-03 13:40:50
I need to be able to calculate (a^b) % c for very large values of a and b (which individually are pushing limit and which cause overflow errors when you try to calculate a^b). For small enough numbers, using the identity (a^b)%c = (a%c)^b%c works, but if c is too large this doesn't really help. I wrote a loop to do the mod operation manually, one a at a time: private static long no_Overflow_Mod(ulong num_base, ulong num_exponent, ulong mod) { long answer = 1; for (int x = 0; x < num_exponent; x++) { answer = (answer * num_base) % mod; } return answer; } but this takes a very long time. Is

How is the modulo operator (%) actually computed?

Deadly 提交于 2019-12-03 12:49:36
Recently I've been confused about the modulo operator, % . It's known that a % b == a-a/b*b when we have integers a and b where a > b , and we can do this calculation by hand if a and b are small enough. However, when it comes to the way a processor computes it, does the processor use the same method as previously mentioned, a-a/b*b ? Maybe just by translating the division into subtraction or addition, or is there some shifting involved perhaps? Except for powers of 2, where the modulo operator can (and in most optimizing compilers is) be turned into a simple bitwise operation, I'm afraid the

re implement modulo using bit shifts?

夙愿已清 提交于 2019-12-03 10:48:46
I'm writing some code for a very limited system where the mod operator is very slow. In my code a modulo needs to be used about 180 times per second and I figured that removing it as much as possible would significantly increase the speed of my code, as of now one cycle of my mainloop does not run in 1/60 of a second as it should. I was wondering if it was possible to re-implement the modulo using only bit shifts like is possible with multiplication and division. So here is my code so far in c++ (if i can perform a modulo using assembly it would be even better). How can I remove the modulo

Does INT_MIN % -1 produce undefined behavior?

馋奶兔 提交于 2019-12-03 09:37:45
gcc generates floating code that raises SIGFPE for the following code: #include <limits.h> int x = -1; int main() { return INT_MIN % x; } However I can find no statement in the standard that this code invokes undefined or implementation-defined behavior. As far as I can tell, it's required to return 0. Is this a bug in gcc or am I missing some special exception the standard makes? Jens Gustedt You are probably right that this can be considered as a bug in the actual standard. The current draft addresses this problem: If the quotient a/b is representable, the expression (a/b)*b + a%b shall