integer-division

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()&

Which is the best way, in C, to see if a number is divisible by another?

◇◆丶佛笑我妖孽 提交于 2019-12-11 03:59:22
问题 Which is the best way, in C, to see if a number is divisible by another? I use this: if (!(a % x)) { // this will be executed if a is divisible by x } Is there anyway which is faster? I know that doing, i.e, 130 % 13 will result into doing 130 / 13 per 10 times. So there are 10 cycles when just one is needed (I just want to know if 130 is divisible by 13). Thanks! 回答1: I know that doing, i.e, 130 % 13 will result into doing 130 / 13 per 10 times Balderdash. % does no such thing on any

How do I get a positive modulo on a negative dividend

若如初见. 提交于 2019-12-11 03:27:25
问题 I am trying to get a modulo-n (0 < n) in the range [0 ... n-1] . The instruction idiv n divides EDX:EAX (64 bits) by n, and leaves EAX=quotient, EDX=remainder. ( n is a register in my code) My problem is that when the content of EDX:EAX is negative, I get a negative result in EDX. The simplest solution I found was: cdq ; extend EAX sign bit to EDX idiv n ; edx = (possibly neg.) remainder add edx, n mov eax, edx ; eax = remainder + n cdq idiv n ; edx = positive remainder Is there a cleaner

How to check if a integer is sum of given integers?

不羁的心 提交于 2019-12-10 17:19:16
问题 Lets say I have a integer result and an array of integers, lets say [a,b,c] (not a fixed length). I need to detect if result=a*i +b*j + c*k , with i,j,k>=0 . I prefer a solution in C/C# if it is possible. PS The problem is from a reservation system, a trip can be sold if its durations is a combination of given durations. Thanks! Ex: if we have a=3, b=7 than rezult 20 = 3*2 + 7*2 result 9 = 3*3 + 7*0 回答1: This is the Frobenius Problem which is in general NP-Hard. For small instances,

Locating numerical errors due to Integer division

和自甴很熟 提交于 2019-12-10 03:56:01
问题 Is there a g++ warning or other tool that can identify integer division (truncation toward zero)? I have thousands of lines of code with calculations that inevitably will have numerical errors typically due to "float = int/int" that need to be located. I need a reasonable method for finding these. 回答1: Try -Wconversion . From gcc's man page: Warn for implicit conversions that may alter a value. This includes conversions between real and integer, like "abs (x)" when "x" is "double";

What does the “variable //= a value” syntax mean in Python? [duplicate]

自作多情 提交于 2019-12-08 17:00:14
问题 This question already has answers here : What does //= in python do? [duplicate] (3 answers) Closed 3 years ago . I came across with the code syntax d //= 2 where d is a variable. This is not a part of any loop, I don't quite get the expression. Can anybody enlighten me please? 回答1: // is a floor division operator. The = beside it means to operate on the variable "in-place". It's similar to the += and *= operators, if you've seen those before, except for this is with division. Suppose I have

Any way to do integer division in sympy?

放肆的年华 提交于 2019-12-08 16:37:23
问题 I have a very long expression that I think can be simplified, and I thought sympy would be the perfect way to do it. Unfortunately the formula relies on a couple of integer divides, and I can't find any way to represent those in sympy . >>> x=Symbol('x') >>> (x+1)/2 x/2 + 1/2 Clearly not what I want, 1/2 isn't an integer. >>> (x+1)//2 TypeError: unsupported operand type(s) for //: 'Add' and 'int' Obviously sympy doesn't handle // . >>> Integer((x+1)/2) # A long list of error messages, ending

Objective-C Integer Arithmetic

两盒软妹~` 提交于 2019-12-08 15:31:56
问题 I'm trying to calculate some numbers in an iPhone application. int i = 12; int o = (60 / (i * 50)) * 1000; I would expect o to be 100 (that's milliseconds) in this example but it equals 0 as displayed by NSLog(@"%d", o). This also equals 0. int o = 60 / (i * 50) * 1000; This equals 250,000, which is straight left-to-right math. int o = 60 / i * 50 * 1000; What's flying over my head here? Thanks, Nick 回答1: In Objective-C / performs integer division on integer arguments, so 4/5 is truncated to

C++: Emulated Fixed Point Division/Multiplication

与世无争的帅哥 提交于 2019-12-08 06:44:43
问题 I'm writing a Fixedpoint class, but have ran into bit of a snag... The multiplication, division portions, I am not sure how to emulate. I took a very rough stab at the division operator but I am sure it's wrong. Here's what it looks like so far: class Fixed { Fixed(short int _value, short int _part) : value(long(_value + (_part >> 8))), part(long(_part & 0x0000FFFF)) {}; ... inline Fixed operator -() const // example of some of the bitwise it's doing { return Fixed(-value - 1, (~part)

C++: Emulated Fixed Point Division/Multiplication

白昼怎懂夜的黑 提交于 2019-12-07 02:46:24
I'm writing a Fixedpoint class, but have ran into bit of a snag... The multiplication, division portions, I am not sure how to emulate. I took a very rough stab at the division operator but I am sure it's wrong. Here's what it looks like so far: class Fixed { Fixed(short int _value, short int _part) : value(long(_value + (_part >> 8))), part(long(_part & 0x0000FFFF)) {}; ... inline Fixed operator -() const // example of some of the bitwise it's doing { return Fixed(-value - 1, (~part)&0x0000FFFF); }; ... inline Fixed operator / (const Fixed & arg) const // example of how I'm probably doing it