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
47
votes
3 answers

When is the difference between quotRem and divMod useful?

From the haskell report: The quot, rem, div, and mod class methods satisfy these laws if y is non-zero: (x `quot` y)*y + (x `rem` y) == x (x `div` y)*y + (x `mod` y) == x quot is integer division truncated toward zero, while the result of…
grom
  • 15,842
  • 19
  • 64
  • 67
47
votes
8 answers

In Python, what is a good way to round towards zero in integer division?

1/2 gives 0 as it should. However, -1/2 gives -1 , but I want it to round towards 0 (i.e. I want -1/2 to be 0), regardless of whether it's positive or negative. What is the best way to do that?
blacktrance
  • 905
  • 2
  • 11
  • 25
44
votes
12 answers

What's the fastest way to divide an integer by 3?

int x = n / 3; // <-- make this faster // for instance int a = n * 3; // <-- normal integer multiplication int b = (n << 1) + n; // <-- potentially faster multiplication
Greg Dean
  • 29,221
  • 14
  • 67
  • 78
41
votes
2 answers

Division in Haskell

I'm making a function in Haskell that halves only the evens in a list and I am experiencing a problem. When I run the complier it complains that you can't perform division of an int and that I need a fractional int type declaration. I have tried…
D347th
  • 697
  • 1
  • 10
  • 16
41
votes
3 answers

How to divide two Int a get a BigDecimal in Kotlin?

I want to divide two Integers and get a BigDecimal back in Kotlin. E.g. 3/6 = 0.500000. I've tried some solutions, like: val num = BigDecimal(3.div(6)) println("%.6f".format(num)) // The result is: 0.000000 but none of them solve my problem.
Alif Al-Gibran
  • 1,166
  • 1
  • 13
  • 24
41
votes
8 answers

Python: Remove division decimal

I have made a program that divides numbers and then returns the number, But the thing is that when it returns the number it has a decimal like this: 2.0 But I want it to give me: 2 so is there anyway I can do this?
Dan Alexander
  • 2,004
  • 6
  • 24
  • 34
40
votes
6 answers

Why does dividing a float by an integer return 0.0?

So if I have a range of numbers '0 - 1024' and I want to bring them into '0 - 255', the maths would dictate to divide the input by the maximum the input will be (1024 in this case) which will give me a number between 0.0 - 1.0. then multiply that by…
Arif Driessen
  • 587
  • 2
  • 6
  • 8
38
votes
6 answers

Division in C++ not working as expected

I was working on something else, but everything came out as zero, so I made this minimalistic example, and the output is still 0. #include int main(int argc, char** argv) { double f=3/5; std::cout << f; return 0; } What am I…
Psirus
  • 425
  • 2
  • 5
  • 5
38
votes
16 answers

Check if a number is divisible by 3

I need to find whether a number is divisible by 3 without using %, / or *. The hint given was to use atoi() function. Any idea how to do it?
josh
  • 13,793
  • 12
  • 49
  • 58
38
votes
3 answers

Integer division in awk

I want to divide two numbers in awk, using integer division, i.e truncating the result. For example k = 3 / 2 print k should print 1 According to the manual, Division; because all numbers in awk are floating-point numbers, the result is not…
user000001
  • 32,226
  • 12
  • 81
  • 108
37
votes
10 answers

C# is rounding down divisions by itself

When I make a division in C#, it automaticaly rounds down. See this example: double i; i = 200 / 3; Messagebox.Show(i.ToString()); This shows me a messagebox containing "66". 200 / 3 is actually 66.66666~ however. Is there a way I can avoid this…
ONOZ
  • 1,390
  • 2
  • 11
  • 19
36
votes
3 answers

Should I bit-shift to divide by 2 in Java?

Possible Duplicates: Is shifting bits faster than multiplying and dividing in Java? .NET? Quick Java Optimization Question Many years ago in college, I learned that bit-shifting right by one accomplishes the same thing as dividing by two, but is…
Matt Huggins
  • 81,398
  • 36
  • 149
  • 218
35
votes
3 answers

Is divmod() faster than using the % and // operators?

I remember from assembly that integer division instructions yield both the quotient and remainder. So, in python will the built-in divmod() function be better performance-wise than using the % and // operators (suppose of course one needs both the…
smichak
  • 4,716
  • 3
  • 35
  • 47
33
votes
4 answers

Is integer division always equal to the floor of regular division?

For large quotients, integer division (//) doesn't seem to be necessarily equal to the floor of regular division (math.floor(a/b)). According to Python docs (https://docs.python.org/3/reference/expressions.html - 6.7), floor division of integers…
33
votes
4 answers

Find multiples of a number in PHP

I want to find all muliples of a number in PHP. I'm using something like this if($count != 20 ) to work out if $count is not equal to 20. But I also need this script to check if $count is not equal to 20, 40, 60, 80, 100, 120, 140, 160 etc. Any…
dotty
  • 40,405
  • 66
  • 150
  • 195