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
783
votes
11 answers

How can I force division to be floating point? Division keeps rounding down to 0?

I have two integer values a and b, but I need their ratio in floating point. I know that a < b and I want to calculate a / b, so if I use integer division I'll always get 0 with a remainder of a. How can I force c to be a floating point number in…
Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
701
votes
48 answers

Divide a number by 3 without using *, /, +, -, % operators

How would you divide a number by 3 without using *, /, +, -, %, operators? The number may be signed or unsigned.
Green goblin
  • 9,898
  • 13
  • 71
  • 100
491
votes
5 answers

What is the reason for having '//' in Python?

I saw this in someone's code: y = img_index // num_images where img_index is a running index and num_images is 3. When I mess around with // in IPython, it seems to act just like a division sign (i.e. one forward slash). I was just wondering if…
Pete
  • 5,791
  • 3
  • 19
  • 10
416
votes
22 answers

Which is better option to use for dividing an integer number by 2?

Which of the following techniques is the best option for dividing an integer by 2 and why? Technique 1: x = x >> 1; Technique 2: x = x / 2; Here x is an integer.
Abhineet
  • 6,459
  • 10
  • 35
  • 53
329
votes
19 answers

Is multiplication and division using shift operators in C actually faster?

Multiplication and division can be achieved using bit operators, for example i*2 = i<<1 i*3 = (i<<1) + i; i*10 = (i<<3) + (i<<1) and so on. Is it actually faster to use say (i<<3)+(i<<1) to multiply with 10 than using i*10 directly? Is there any…
eku
  • 3,357
  • 3
  • 19
  • 20
307
votes
7 answers

Why is division in Ruby returning an integer instead of decimal value?

For example: 9 / 5 #=> 1 but I expected 1.8. How can I get the correct decimal (non-integer) result? Why is it returning 1 at all?
ErwinM
  • 5,051
  • 2
  • 23
  • 35
274
votes
4 answers

Why does integer division yield a float instead of another integer?

Consider this division in Python 3: >>> 2/2 1.0 Is this intended? I strongly remember earlier versions returning int/int = int. What should I do? Is there a new division operator or must I always cast? In 2.x, the behaviour was indeed reversed;…
Jonas Byström
  • 25,316
  • 23
  • 100
  • 147
252
votes
10 answers

How to get a float result by dividing two integer values using T-SQL?

Using T-SQL and Microsoft SQL Server I would like to specify the number of decimal digits when I do a division between 2 integer numbers like: select 1/3 That currently returns 0. I would like it to return 0,33. Something like: select round(1/3,…
UnDiUdin
  • 14,924
  • 39
  • 151
  • 249
180
votes
2 answers

How to do integer division in javascript (Getting division answer in int not float)?

Is there any function in Javascript that lets you do integer division, I mean getting division answer in int, not in floating point number. var x = 455/10; // Now x is 45.5 // Expected x to be 45 But I want x to be 45. I am trying to eliminate last…
Nakib
  • 4,593
  • 7
  • 23
  • 45
175
votes
6 answers

How can I do division with variables in a Linux shell?

When I run commands in my shell as below, it returns an expr: non-integer argument error. Can someone please explain this to me? $ x=20 $ y=5 $ expr x / y expr: non-integer argument
Judking
  • 6,111
  • 11
  • 55
  • 84
174
votes
1 answer

Python 3 integer division

In Python 3 vs Python 2.6, I've noticed that I can divide two integers and get a float. How do you get the Python 2.6 behaviour back? Is there a different method to get int/int = int?
Megatron
  • 15,909
  • 12
  • 89
  • 97
172
votes
8 answers

Why does integer division in C# return an integer and not a float?

Does anyone know why integer division in C# returns an integer and not a float? What is the idea behind it? (Is it only a legacy of C/C++?) In C#: float x = 13 / 4; //== operator is overridden here to use epsilon compare if (x == 3.0) print…
BanditoBunny
  • 3,658
  • 5
  • 32
  • 40
154
votes
4 answers

Division ( / ) not giving my answer in postgresql

I have a table software and columns in it as dev_cost, sell_cost. If dev_cost is 16000 and sell_cost is 7500, how do I find the quantity of software to be sold in order to recover the dev_cost? I have queried as below: select dev_cost / sell_cost…
zeewagon
  • 1,835
  • 4
  • 18
  • 22
128
votes
9 answers

C++ Best way to get integer division and remainder

I am just wondering, if I want to divide a by b, and am interested both in the result c and the remainder (e.g. say I have number of seconds and want to split that into minutes and seconds), what is the best way to go about it? Would it be int c =…
Cookie
  • 12,004
  • 13
  • 54
  • 83
119
votes
19 answers

How Does Modulus Divison Work

I don't really understand how modulus division works. I was calculating 27 % 16 and wound up with 11 and I don't understand why. I can't seem to find an explanation in layman's terms online. Can someone elaborate on a very high level as to what's…
NSWOA
  • 1,201
  • 2
  • 9
  • 5
1
2 3
99 100