Questions tagged [division]

In mathematics, division (÷) is an arithmetic elementary operation.

In mathematics, especially in elementary arithmetic, division (÷) is an arithmetic operation. Specifically, if b times c equals a (b × c = a) where b is not zero, then a divided by b equals c (a ÷ b = c).

In the expression a ÷ b = c, a is called the dividend, b the divisor and c the quotient.

In programming, division is usually represented by the / symbol. There are two import forms of division in most programming languages:

  • In floating point division which operates on floating point or other rational numeric datatypes, the fractional part of the result is included (to the accuracy provided by the datatype):

    float a = 5.0;
    float b = 2.0;
    float c = a / b;   // c = 2.5
    
  • In integer division which operates on integral numeric datatypes, the fractional part of the result is discarded, as in following example:

    int a = 5;
    int b = 2;
    int c = a / b;     //  c = 2
    
2262 questions
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
24
votes
10 answers

Fast division/mod by 10ˣ

In my program I use a lot of integer division by 10x and integer mod function of power 10. For example: unsigned __int64 a = 12345; a = a / 100; .... or: unsigned __int64 a = 12345; a = a % 1000; .... If I am going to use the right bit shift >>,…
kirbo
  • 1,707
  • 5
  • 26
  • 32
24
votes
8 answers

python: getting around division by zero

I have a big data set of floating point numbers. I iterate through them and evaluate np.log(x) for each of them. I get RuntimeWarning: divide by zero encountered in log I would like to get around this and return 0 if this error occurs. I am…
Julia
  • 1,369
  • 4
  • 18
  • 38
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

Perform integer division using multiplication

Looking at x86 assembly produced by a compiler, I noticed that (unsigned) integer divisions are sometimes implemented as integer multiplications. These optimizations seem to follow the form value / n => (value * ((0xFFFFFFFF / n) + 1)) /…
22
votes
4 answers

64/32-bit division on a processor with 32/16-bit division

My processor, a small 16-bit microcontroller with no FPU and integer math only has 16/16 division and 32/16 division which both take 18 cycles. At the moment I'm using a very slow software routine (~7,500 cycles) to do 64/32 division. Is there any…
Thomas O
  • 6,026
  • 12
  • 42
  • 60
22
votes
4 answers

Check if an integer is divisible by another integer (Swift)

I need to check if an integer is divisible by another integer exactly. If not I would like to round it up to the closest multiple of the number. Example: var numberOne = 3 var numberTwo = 5 numberTwo is not a multiple of numberOne therefore I would…
Tom Coomer
  • 6,227
  • 12
  • 45
  • 82
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
21
votes
3 answers

How to find the remainder of a division in C?

Which is the best way to find out whether the division of two numbers will return a remainder? Let us take for example, I have an array with values {3,5,7,8,9,17,19}. Now I need to find the perfect divisor of 51 from the above array. Is there any…
Vivek
  • 4,526
  • 17
  • 56
  • 69
21
votes
1 answer

What is the function to get the quotient and remainder (DIVMOD) for rust?

x86 and likely other architectures provide a method to get the quotient and remainder in a single operation (DIV). Because of this many languages have a DIVMOD combined operation, (like DIVREM in C#, DIVMOD in Python, or with div and div_t in C. How…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
21
votes
2 answers

how to do two complement multiplication and division of integers?

I have read this post on binary multiplication using two complement. but it is not very clear to me. Even I have difficulty understanding the wiki article on this. I want to know how to go about calculating multiplications of negative numbers: eg:…
eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
21
votes
1 answer

How to divide the value of pandas columns by the other column

I have a dataframe: >>> dt COL000 COL001 QT STK_ID RPT_Date STK000 20120331 2.6151 2.1467 1 20120630 4.0589 2.3442 2 20120930 4.4547 3.9204 3 20121231 …
bigbug
  • 55,954
  • 42
  • 77
  • 96
21
votes
7 answers

Unexpected result in long/int division

I have values like this: long millis = 11400000; int consta = 86400000; double res = millis/consta; The question is: why res equals 0.0 (instead of ca. 0.131944)? It's stored in double so there should be no rounding right?
alex
  • 10,900
  • 15
  • 70
  • 100
20
votes
18 answers

Why is 0 divided by 0 an error?

I have come across this problem in a calculation I do in my code, where the divisor is 0 if the divident is 0 too. In my code I return 0 for that case. I am wondering, while division by zero is generally undefined, why not make an exception for this…
Björn Pollex
  • 75,346
  • 28
  • 201
  • 283