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
33
votes
9 answers

ArithmeticException thrown during BigDecimal.divide

I thought java.math.BigDecimal is supposed to be The Answer™ to the need of performing infinite precision arithmetic with decimal numbers. Consider the following snippet: import java.math.BigDecimal; //... final BigDecimal one =…
polygenelubricants
  • 376,812
  • 128
  • 561
  • 623
32
votes
4 answers

Why does integer division code give the wrong answer?

I have a very simple division in Java (it's a product quantity / production per hour), however whenever I make this division I get strange errors: float res = quantity / standard; I have tried the above division with several values and I always get…
Julian C.
  • 339
  • 1
  • 3
  • 4
30
votes
9 answers

Fast multiplication/division by 2 for floats and doubles (C/C++)

In the software I'm writing, I'm doing millions of multiplication or division by 2 (or powers of 2) of my values. I would really like these values to be int so that I could access the bitshift operators int a = 1; int b = a<<24 However, I cannot,…
B. Decoster
  • 7,723
  • 1
  • 32
  • 52
29
votes
6 answers

Python-style integer division & modulus in C

In Python and Ruby, signed integer division truncates towards negative infinity, and signed integer modulus has the same sign the second operand: >>> (-41) / 3 -14 >>> (-41) % 3 1 However, in C and Java, signed integer division truncates towards 0,…
user102008
  • 30,736
  • 10
  • 83
  • 104
29
votes
2 answers

Integer division in Java

This feels like a stupid question, but I can't find the answer anywhere in the Java documentation. If I declare two ints and then divide them, what exactly is happening? Are they converted to floats/doubles first, divided, then cast back to an…
MadMonty
  • 807
  • 2
  • 7
  • 15
29
votes
2 answers

Division of integers returns 0

I feel like I'm missing something obvious. I am trying to test out the distribution of random(). Here is the table: create table test ( id int, random_float float, random_int int ); Here is what I want to do: truncate table test; insert into…
Phrancis
  • 2,222
  • 2
  • 27
  • 40
28
votes
10 answers

How do I divide so I get a decimal value?

I want to know how to get remainder and quotient in single value in Java. Example: 3/2 I should get value as 1.5. If I use the / operator I get only the quotient. If I user the % operator I get only the remainder. How do I get both at a time in…
Raja
27
votes
3 answers

Difference between "Math.DivRem" and % operator?

What is the difference between System.Math.DivRem() and the % operator?
brandeded
  • 2,080
  • 6
  • 25
  • 52
27
votes
5 answers

Simple division in Java - is this a bug or a feature?

I'm trying this simple calculation in a Java application: System.out.println("b=" + (1 - 7 / 10)); Obviously I expect the output to be b=0.3, but I actually get b=1 instead. What?! Why does this happen? If I write: System.out.println("b=" + (1 -…
rasgo
  • 1,381
  • 4
  • 15
  • 28
26
votes
3 answers

How can I perform divison on a datetime.timedelta in python?

I'd like to be able to do the following: num_intervals = (cur_date - previous_date) / interval_length or print (datetime.now() - (datetime.now() - timedelta(days=5))) / timedelta(hours=12) # won't run, would like it to print '10' but the…
Luke
  • 18,585
  • 24
  • 87
  • 110
26
votes
3 answers

Dividing a number by instances of my class in Python

I have a class called Time, and I need to implement a Frequency class. How can I implement dividing ints or floats by an instance of Time to get an instance of Frequency ? I already know about __div__, __truediv__, __floordiv__ and other Python…
lucidiot
  • 484
  • 1
  • 6
  • 15
26
votes
9 answers

Efficiently dividing unsigned value by a power of two, rounding up

I want to implement unsigneda integer division by an arbitrary power of two, rounding up, efficiently. So what I want, mathematically, is ceiling(p/q)0. In C, the strawman implementation, which doesn't take advantage of the restricted domain of q…
BeeOnRope
  • 60,350
  • 16
  • 207
  • 386
26
votes
4 answers

How to manage division of huge numbers in Python?

I have a 100 digit number and I am trying to put all the digits of the number into a list, so that I can perform operations on them. To do this, I am using the following code: for x in range (0, 1000): list[x] = number % 10 number = number /…
Ambidextrous
  • 882
  • 2
  • 12
  • 26
24
votes
4 answers

Division by zero in Haskell

I found a quite strange inconsistency between the behaviour of div and /. *ghci> :t 1 `div` 0 1 `div` 0 :: Integral a => a *ghci> :t 1 / 0 1 / 0 :: Fractional a => a *ghci> 1 / 0 Infinity *ghci> 1 `div` 0 *** Exception: divide by zero I was quite…
Riccardo T.
  • 8,907
  • 5
  • 38
  • 78
24
votes
5 answers

Does INT_MIN % -1 produce undefined behavior?

gcc generates floating code that raises SIGFPE for the following code: #include int x = -1; int main() { return INT_MIN % x; } However I can find no statement in the standard that this code invokes undefined or implementation-defined…
R.. GitHub STOP HELPING ICE
  • 208,859
  • 35
  • 376
  • 711