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
1336
votes
19 answers

How to perform an integer division, and separately get the remainder, in JavaScript

In JavaScript, how do I get: The whole number of times a given integer goes into another? The remainder?
Yarin
  • 173,523
  • 149
  • 402
  • 512
354
votes
11 answers

Fast ceiling of an integer division in C / C++

Given integer values x and y, C and C++ both return as the quotient q = x/y the floor of the floating point equivalent. I'm interested in a method of returning the ceiling instead. For example, ceil(10/5)=2 and ceil(11/5)=3. The obvious approach…
andand
  • 17,134
  • 11
  • 53
  • 79
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
306
votes
11 answers

Integer division: How do you produce a double?

For this code block: int num = 5; int denom = 7; double d = num / denom; the value of d is 0.0. It can be forced to work by casting: double d = ((double) num) / denom; But is there another way to get the correct double result? I don't like casting…
walnutmon
  • 5,873
  • 7
  • 42
  • 57
271
votes
6 answers

What is the behavior of integer division?

For example, int result; result = 125/100; or result = 43/100; Will result always be the floor of the division? What is the defined behavior?
T.T.T.
  • 33,367
  • 47
  • 130
  • 168
268
votes
5 answers

Why does GCC use multiplication by a strange number in implementing integer division?

I've been reading about div and mul assembly operations, and I decided to see them in action by writing a simple program in C: File division.c #include #include int main() { size_t i = 9; size_t j = i / 5; …
qiubit
  • 4,708
  • 6
  • 23
  • 37
221
votes
13 answers

Find the division remainder of a number

How could I go about finding the division remainder of a number in Python? For example: If the number is 26 and divided number is 7, then the division remainder is 5. (since 7+7+7=21 and 26-21=5.) For simple divisibility testing, see How do you…
Bob
  • 10,427
  • 24
  • 63
  • 71
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
151
votes
19 answers

Int division: Why is the result of 1/3 == 0?

I was writing this code: public static void main(String[] args) { double g = 1 / 3; System.out.printf("%.2f", g); } The result is 0. Why is this, and how do I solve this problem?
Tofiq
  • 2,901
  • 6
  • 26
  • 34
135
votes
10 answers

Why does dividing two int not yield the right value when assigned to double?

How come that in the following snippet int a = 7; int b = 3; double c = 0; c = a / b; c ends up having the value 2, rather than 2.3333, as one would expect. If a and b are doubles, the answer does turn to 2.333. But surely because c already is a…
Jahoe
  • 1,666
  • 2
  • 12
  • 27
118
votes
1 answer

How to perform division in Go

I am trying to perform a simple division in Go. fmt.Println(3/10) This prints 0 instead of 0.3. This is kind of weird. Could someone please share what is the reason behind this? i want to perform different arithmetic operations in Go. Thanks
Vrushank Doshi
  • 2,428
  • 5
  • 20
  • 34
106
votes
26 answers

Rounding integer division (instead of truncating)

I was curious to know how I can round a number to the nearest whole number. For instance, if I had: int a = 59 / 4; which would be 14.75 if calculated in floating point; how can I store the result as 15 in "a"?
Dave
  • 1,129
  • 2
  • 9
  • 5
93
votes
1 answer

Dividing two integers to produce a float result

Possible Duplicate: Why can't I return a double from two ints being divided My C++ program is truncating the output of my integer devision even when I try and place the output into a float. How can I prevent this whilst keeping those to variables…
jwbensley
  • 10,534
  • 19
  • 75
  • 93
88
votes
8 answers

What's the mathematical reason behind Python choosing to round integer division toward negative infinity?

I know Python // rounds towards negative infinity and in C++ / is truncating, rounding towards 0. And here's what I know so far: |remainder| -12 / 10 = -1, - 2 // C++ -12 // 10 = -2, + 8 # Python 12 / -10 = -1, 2 …
Rick
  • 7,007
  • 2
  • 49
  • 79
69
votes
6 answers

Why does the division of two integers return 0.0 in Java?

int totalOptCount = 500; int totalRespCount=1500; float percentage =(float)(totalOptCount/totalRespCount); Why does this always return value 0.0? Also I want to format this into 00.00 format and convert into string?
kiran
  • 749
  • 2
  • 6
  • 5
1
2 3
41 42