modulo

Using bitwise & instead of modulus operator to randomly sample integers from a range

好久不见. 提交于 2019-12-11 04:39:58
问题 I need to randomly sample from a uniform distribution of integers over the interval [LB,UB] in C++. To do so, I start with a "good" RN generator (from Numerical Recipes 3rd ed.) that uniformly randomly samples 64-bit integers; let's call it int64() . Using the mod operator, I can sample from the integers in [LB,UB] by: LB+int64()%(UB-LB+1); The only issue with using the mod operator is the slowness of the integer division. So, I then tried the method suggested here, which is: LB + (int64()&

Modulo returns wrong result when using negatives

安稳与你 提交于 2019-12-11 03:23:14
问题 I want to calculate: (-15 % 3) which should be 0 but instead i'm getting 1: When i explicitly do: int IntFcn (const void *key, size_t tableSize) { printf("%d\n",(*(int*)key)); // prints -15 printf("%d\n",tableSize); // prints 3 printf("%d\n",(-15) % 3); // prints 0 } I get the right result (0) but when i try to use the variables below i get 1: int IntFcn (const void *key, size_t tableSize) { printf("%d\n",(*(int*)key)); // prints -15 printf("%d\n",tableSize); // prints 3 printf("%d\n",((*(int

Why is 0 mod 0 an error?

断了今生、忘了曾经 提交于 2019-12-10 19:03:23
问题 If I type: int main() { return 0 % 0; } I get back an error: error C2124: divide or mod by zero What is the reason behind this? Isn't the answer zero? 回答1: In mathematics, x mod 0 is undefined, hence the error. 回答2: From C++ standard, section 5.5: If during the evaluation of an expression the result is not mathematically defined or not in the range of representable mathematical values for its type, the behavior is undefined. [...] Treatment of division by zero, forming a remainder using a

How to find the remainder of large number division in C++?

混江龙づ霸主 提交于 2019-12-10 18:47:14
问题 I have a question regarding modulus in C++. What I was trying to do was divide a very large number, lets say for example, M % 2, where M = 54,302,495,302,423. However, when I go to compile it says that the number is to 'long' for int. Then when I switch it to a double it repeats the same error message. Is there a way I can do this in which I will get the remainder of this very large number or possibly an even larger number? Thanks for your help, much appreciated. 回答1: For large number

modulo n generic counter

帅比萌擦擦* 提交于 2019-12-10 17:22:37
问题 I am required to design a modulo "n" counter with generic parameters. I am having trouble fixing the length of the std_logic_vector which will hold the output. Firstly, I get errors regarding the use of airthmetic operators on numeric types. And secondly, I am not allowed to use a dynamic expression in the range specification of the vector. Here is my code so far: library IEEE; use IEEE.std_logic_1164.all; use IEEE.std_logic_unsigned.all; use IEEE.math_real.all; entity counter_mod is generic

Why does mod give a different result in an expression than in a function call?

时光总嘲笑我的痴心妄想 提交于 2019-12-10 14:26:45
问题 Say one wants to calculate the function: f (x,y) = ((x `mod` 3)+(y `mod` 3)) `mod` 2 Then, if one expands f (-1,0) manually, one gets: ((-1 `mod` 3)+(0 `mod` 3)) `mod` 2 1 If one however uses an inline function, the result is: let f (x,y) = ((x `mod` 3)+(y `mod` 3)) `mod` 2 in f (-1,0) 0 What happens when storing the function that yields not the expected result? I assume this is because f uses Integral instead of Int ? 回答1: Looks like it's a matter of parsing. -1 `mod` 3 gets parsed as -(1

When taking modulo of negative numbers makes sense?

我的梦境 提交于 2019-12-10 11:19:33
问题 I'm curious because we found a bug in our code written 2 years ago. We initialized a 16 bit signed integer with 0x8000 (the platform is of course uses 2's complement representation for negative numbers). In a hardly reproducible special case, modulo by 10 was being performed on this variable to extract individual digits in decimal representation while the value is still 0x8000 (-32768). -32768 % 10 == 248 which makes no sense for our application. Our platform is OKI 411 micro-controller. I'm

How can I add a class to every 4th - 1 element?

爷,独闯天下 提交于 2019-12-10 10:18:23
问题 Don't ask why but I need to add class zebra to the <li> elements with the content next to them. This is as far as I've got, but I'm not sure what calculation to use: $("li").each(function(index){ if(index % ??? == 0) { // <-- not sure what to put here } }); <ul> <li></li> <li></li> <li></li> <!-- add the zebra class here --> <li></li> <li></li> <li></li> <li></li> <!-- add the zebra class here --> <li></li> <li></li> <li></li> <li></li> <!-- add the zebra class here --> <li></li> </ul> Can

FizzBuzz Ruby one-liner

☆樱花仙子☆ 提交于 2019-12-08 18:28:53
问题 Rosettacode.org has this excellent one-line FizzBuzz solution in Ruby. 1.upto(100){|n|puts'FizzBuzz '[i=n**4%-15,i+13]||n} The trouble is, I don’t understand it. The part that puzzles me is the ”n to the power of 4 modulo -15”. Does anyone have an explanation or a reference to an explanation? I want to use this way of selecting substrings in other problems. For more information on FizzBuzz, see [https://rosettacode.org/wiki/FizzBuzz] 回答1: I don't know how they discovered to raise to the

Fast multiplication and subtraction modulo a prime

梦想与她 提交于 2019-12-08 16:04:44
问题 I need to optimize some code where I multiply a vector of ints (32 bit) by a scalar modulo p (where p is the prime number (2^32)-5) and then subtract that vector from another vector modulo p. The code looks like this: public static void multiplyAndSubtract(long fragmentCoefficient, long[] equationToSubtractFrom, long[] equationToSubtract) { for (int i = 0; i < equationToSubtractFrom.length; i++) { equationToSubtractFrom[i] = modP(equationToSubtractFrom[i] - multiplyModP(fragmentCoefficient,