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
27
votes
7 answers

Round with integer division

Is there is a simple, pythonic way of rounding to the nearest whole number without using floating point? I'd like to do the following but with integer arithmetic: skip = int(round(1.0 * total / surplus)) ============== @John: Floating point is not…
new name
  • 15,861
  • 19
  • 68
  • 114
25
votes
2 answers

How to get fractions in an integer division?

How do you divide two integers and get a double or float answer in C?
Chad Carisch
  • 2,422
  • 3
  • 22
  • 30
24
votes
6 answers

Efficiently implementing floored / euclidean integer division

Floored division is when the result is always floored down (towards −∞), not towards 0: Is it possible to efficiently implement floored or euclidean integer division in C/C++? (the obvious solution is to check the dividend's sign)
Vladimir Panteleev
  • 24,651
  • 6
  • 70
  • 114
23
votes
3 answers

How to implement (fast) bigint division?

I'm currently making my own BigInt class, by splitting numbers by 7 digits. (i.e. in base 10,000,000) I implemented addition, subtraction, and multiplication, and now I'm implementing division and mod. I wrote a code that performs division by long…
JiminP
  • 2,136
  • 19
  • 26
23
votes
3 answers

Integer division algorithm

I was thinking about an algorithm in division of large numbers: dividing with remainder bigint C by bigint D, where we know the representation of C in base b, and D is of form b^k-1. It's probably the easiest to show it on an example. Let's try…
mornik
  • 231
  • 2
  • 5
23
votes
4 answers

Why is math.floor(x/y) != x // y for two evenly divisible floats in Python?

I have been reading about division and integer division in Python and the differences between division in Python2 vs Python3. For the most part it all makes sense. Python 2 uses integer division only when both values are integers. Python 3 always…
None
  • 5,491
  • 1
  • 40
  • 51
23
votes
1 answer

numpy array, difference between a /= x vs. a = a / x

I'm using python 2.7.3, when I execute the following piece of code: import numpy as np a = np.array([[1,2,3],[4,5,6]]) a = a / float(2**16 - 1) print a This will result in he following output: >> array([[1.52590219e-05, 3.05180438e-05,…
Gio
  • 3,242
  • 1
  • 25
  • 53
23
votes
1 answer

Why is modulus operator slow?

Paraphrasing from in "Programming Pearls" book (about c language on older machines, since book is from the late 90's): Integer arithmetic operations (+, -, *) can take around 10 nano seconds whereas the % operator takes up to 100 nano seconds. Why…
AV94
  • 1,824
  • 3
  • 23
  • 36
23
votes
9 answers

Why I cannot the get percentage by using Int

Please forgive my programming knowledge. I know this is a simple thing, but I do not understand why result is always 0. Why decimal will be fine? int a = 100; int b = 200; decimal c = (a / b) * 100; Many thanks.
Daoming Yang
  • 1,325
  • 3
  • 20
  • 41
23
votes
6 answers

How to check if given number is divisible of 15 in fastest way?

Division in processor takes much time, so I want to ask how to check in fastest way if number is divisible of some other number, in my case I need to check if number is divisible by 15. Also I've been looking through web and found fun ways to check…
user2532605
22
votes
4 answers

Division result is always zero

I got this C code. #include int main(void) { int n, d, i; double t=0, k; scanf("%d %d", &n, &d); t = (1/100) * d; k = n / 3; printf("%.2lf\t%.2lf\n", t, k); return 0; } I want to…
VaioIsBorn
  • 7,683
  • 9
  • 31
  • 28
22
votes
4 answers

C integer division and floor

In C, is there a difference between integer division a/b and floor(a/b) where both a and b are integers? More specifically what happens during both processes?
CodeKingPlusPlus
  • 15,383
  • 51
  • 135
  • 216
20
votes
5 answers

How does one do integer (signed or unsigned) division on ARM?

I'm working on Cortex-A8 and Cortex-A9 in particular. I know that some architectures don't come with integer division, but what is the best way to do it other than convert to float, divide, convert to integer? Or is that indeed the best…
Phonon
  • 12,549
  • 13
  • 64
  • 114
20
votes
2 answers

How can I get the quotient and the remainder in a single step?

Possible Duplicate: Divide and Get Remainder at the same time? Is it possible to get both the quotient and the remainder of integer division in a single step, i.e., without performing integer division twice?
dtech
  • 47,916
  • 17
  • 112
  • 190
19
votes
6 answers

Is div function useful (stdlib.h)?

There is a function called div in C,C++ (stdlib.h) div_t div(int numer, int denom); typedef struct _div_t { int quot; int rem; } div_t; But C,C++ have / and % operators. My question is: "When there are / and % operators, Is div function…
Amir Saniyan
  • 13,014
  • 20
  • 92
  • 137
1 2
3
41 42