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
    
2011 questions
745
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
  • 108,984
  • 95
  • 246
  • 308
691
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,536
  • 13
  • 63
  • 97
455
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,231
  • 3
  • 17
  • 9
411
votes
23 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,149
  • 10
  • 32
  • 51
304
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,107
  • 3
  • 16
  • 20
272
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
  • 4,671
  • 2
  • 20
  • 32
242
votes
5 answers

Python integer division yields float

Python 3.1 (r31:73574, Jun 26 2009, 20:21:35) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> 2/2 1.0 Is this intended? I strongly remember earlier versions returning int/int=int? What…
Jonas Byström
  • 22,233
  • 21
  • 93
  • 137
202
votes
8 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,…
LaBracca
  • 13,941
  • 34
  • 129
  • 224
160
votes
1 answer

Python3 integer division

In Python3 vs Python2.6, I've noticed that I can divide two integers and get a float. How do you get the Python2.6 behaviour back? Is there a different method to get int/int = int?
Megatron
  • 12,223
  • 9
  • 75
  • 86
152
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
  • 5,231
  • 10
  • 46
  • 76
146
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,086
  • 5
  • 26
  • 39
145
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
  • 3,923
  • 7
  • 20
  • 45
112
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,131
  • 2
  • 8
  • 5
109
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
  • 10,754
  • 13
  • 47
  • 69
98
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,259
  • 2
  • 12
  • 21
1
2 3
99 100