division : Java Glossary

go to home page D words local find full screen, hide local find menu Google search web for more information on this topic jump to foot of page translate this page with Babelfish punctuation 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z (all) ©1996-2009 2008-04-15 Roedy Green, Canadian Mind Products
division
There are two kinds of division in Java, integer and floating point. They both use the / operator to ensure you will get them confused. It depends on whether the operands surrounding it are int/ long or float/double which form is used. Integer division always gives an integer result, no fraction, truncated toward 0. Floating point division gives a fractional answer as accurate as IEEE format allows.
Integer Division Covered Quotient
Floating Point Division Honing Your Intuition
Remainder Learning More
Floored Division Links

Integer Division

Floating Point Division

Floating point division always seems to be a wee bit off from what you would expect. That is because many common decimal fractions are repeaters when expressed in binary that Java uses internally.

Remainder

In Java you take the remainder with the % operator. In Java, the sign of the remainder follows the dividend, not the divisor. Java division has the Euclidean property. When you multiply the quotient by the divisor and add the remainder you get back to the dividend. On the other hand, when you ask for % 3 in Java, you may be astounded to sometimes get an answer outside the range 0..2. See remainder/modulus sign rules. Be especially careful when corralling random numbers into a smaller range with the % operator. Java division does have the Euclidean property. When you multiply the quotient by the divisor and add the remainder you get back to the dividend. Java division is truncated division.

Floored Division

Floored division is what you normally want when trying to figure out which bin an item belongs in. You can compute floored division as:
rounding down to next lower multiple.

Covered Quotient

For computing how many fixed-size bins you need to contain N items, you want ceiled division, also known as the covered quotient. You can compute the covered quotient as:
rounding up to next higher multiple.

Honing Your Intuition

By examining the patterns in the following tables of / integer division, you will have a better intuition of how it work.
Integer Division: col / row
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
-10 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -10
-9 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1 -9
-8 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1 -1 -8
-7 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 -1 -1 -1 -7
-6 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -6
-5 2 1 1 1 1 1 0 0 0 0 0 0 0 0 0 -1 -1 -1 -1 -1 -2 -5
-4 2 2 2 1 1 1 1 0 0 0 0 0 0 0 -1 -1 -1 -1 -2 -2 -2 -4
-3 3 3 2 2 2 1 1 1 0 0 0 0 0 -1 -1 -1 -2 -2 -2 -3 -3 -3
-2 5 4 4 3 3 2 2 1 1 0 0 0 -1 -1 -2 -2 -3 -3 -4 -4 -5 -2
-1 10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -1
0 - - - - - - - - - - - - - - - - - - - - - 0
1 -10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10 1
2 -5 -4 -4 -3 -3 -2 -2 -1 -1 0 0 0 1 1 2 2 3 3 4 4 5 2
3 -3 -3 -2 -2 -2 -1 -1 -1 0 0 0 0 0 1 1 1 2 2 2 3 3 3
4 -2 -2 -2 -1 -1 -1 -1 0 0 0 0 0 0 0 1 1 1 1 2 2 2 4
5 -2 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 1 1 1 1 1 2 5
6 -1 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 6
7 -1 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 7
8 -1 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 8
9 -1 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 9
10 -1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 10
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10

Learning More


CMP homejump to top You can get the freshest copy of this page from: or possibly from your local J: drive (Java virtual drive/mindprod.com website mirror)
http://mindprod.com/jgloss/division.html J:\mindprod\jgloss\division.html
CMP logofeedback Please email your feedback for publication, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email
mindprod.com IP:[65.110.21.43]
view BlogYour face IP:[38.107.191.105]
You are visitor number 101,797.