Questions tagged [integer-division]

Anything related to the integer division operation, i.e. that special form of division which is performed between two integer numbers and which in math results in a quotient and a remainder. This is mostly relevant for languages which have specific integer data-types, for which the division is an integer division, or for languages having a specific operator/function to perform integer division.

Anything related to the integer division operation, i.e. that special form of division which is performed between two integer numbers and which in math results in a quotient and a remainder. This is mostly relevant for languages which have specific integer data-types, for which the division is an integer division, or for languages having a specific operator/function to perform integer division.

619 questions
19
votes
6 answers

Modulus division when first number is smaller than second number

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the…
Jessica M.
  • 1,451
  • 12
  • 39
  • 54
18
votes
2 answers

Integer division in Python 3 - strange result with negative number

I am new to Python, and I am learning operators right now. I understood that: The / operator is used for floating point division and // for integer division. Example: 7//3 = 2 And 7//-3=-3. Why is the answer -3? I am stuck here.
princess
  • 313
  • 2
  • 8
17
votes
5 answers

Rounding numbers to the nearest 10 in Postgres

I'm trying to solve this particular problem from PGExercises.com: https://www.pgexercises.com/questions/aggregates/rankmembers.html The gist of the question is that I'm given a table of club members and half hour time slots that they have booked…
neuron
  • 551
  • 3
  • 9
  • 16
17
votes
2 answers

How can I strength reduce division by 2^n + 1?

I need to perform some integer divisions in the hot path of my code. I've already determined via profiling and cycle counting that the integer divisions are costing me. I'm hoping there's something I can do to strength reduce the divisions into…
J.S.
  • 171
  • 4
17
votes
4 answers

Floor division with negative number

The expression 6 // 4 yields 1, where floor division produces the whole number after dividing a number. But with a negative number, why does -6 // 4 return -2?
Naz Islam
  • 409
  • 1
  • 6
  • 20
17
votes
6 answers

The opposite of the modulo operator?

I remember in java that, the modulo operator could be inverted so that rather than seeing what the remainder is of an operation, you could invert it, so instead it will tell you many times a number was divided by: Console.WriteLine(1000 %…
IbrarMumtaz
  • 4,235
  • 7
  • 44
  • 63
16
votes
7 answers

How can I use bit shifting to replace integer division?

I understand how to do it for powers of 2 so that's not my question. For example, if I want to find 5% of a number using a bit shift instead of an integer divide, how would i calculate that? So instead of (x * 20 / 19), I could do (x * 100 >> 11). …
glutz78
  • 181
  • 1
  • 2
  • 8
15
votes
3 answers

How does C++ integer division work for limit and negative values?

I am facing some strange results with integer division in C++. I am trying to calculate this: -2147483648 / -1. What I get is 3 different results in 3 different scenarios: int foo(int numerator, int denominator) { int res = numerator /…
Kareem Ergawy
  • 647
  • 1
  • 8
  • 17
14
votes
2 answers

Why does x = x * y / z give a different result from x *= y / z for integers?

I have the following function: pub fn s_v1(n: &u64) -> u64 { let mut x: u64 = 1; for i in 1..=*n { x = x * (*n + i) / i; } x } This code gives the correct answer for s_v1(&20) == 137846528820 However, if I change the line…
Aditeya
  • 303
  • 2
  • 10
14
votes
3 answers

Fast method to multiply integer by proper fraction without floats or overflow

My program frequently requires the following calculation to be performed: Given: N is a 32-bit integer D is a 32-bit integer abs(N) <= abs(D) D != 0 X is a 32-bit integer of any value Find: X * N / D as a rounded integer that is X scaled to N/D…
14
votes
3 answers

Fast signed 16-bit divide by 7 for 6502

I am working on an assembly language program for a 6502 cpu, and am finding that I need a fast-as-possible divide-by-seven routine, in particular one which could take a 16-bit dividend. I am familiar with the routines found here, but generalizing…
markt1964
  • 2,638
  • 2
  • 22
  • 54
14
votes
1 answer

Why does 1 // 0.1 == 9.0?

In Python 2.7 and 3.x, why does integer division give me a non-correct number when dividing by a number 0 < x < 1? Negative numbers -1 < x < 0 even work correctly: >>> 1//.1 9.0 >>> 1//-.1 -10.0 I understand that integer division with a negative…
Bryce Guinta
  • 3,456
  • 1
  • 35
  • 36
14
votes
1 answer

Integer division by 7

Source my answer in: Is this expression correct in C preprocessor I'm a little bit out of my forte here, and I'm trying to understand how this particular optimization works. As mentioned in the answer, gcc will optimize integer division by 7 to: mov…
13
votes
4 answers

Java - get the quotient and remainder in the same step?

It seems that in order to find both the quotient and remainder of a division in Java, one has to do: int a = ... int b = ... int quotient = a / b; int remainder = a % b; Is there a way to write this so that the quotient and remainder are found in…
FuzzyCat444
  • 315
  • 2
  • 7
13
votes
1 answer

When and why do we sign extend and use cdq with mul/div?

I had a test todayand the only question I didn't understand involved converting a doubleword to a quad word. That got me thinking, why/when do we sign extend for multiplication or division? In addition, when do we use instructions like cdq?
user5469576