division : Java Glossary
home D words local find no local find frame, full screen Google search web for topic jump to footer translate with Babelfish 2008-04-15 by Roedy Green ©1996-2008 Canadian Mind Products
Go to : 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)
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. 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
CMP logo
feedback Please email your feedback for publication, errors, omissions, broken/redirected link reports
and suggestions to improve this page to Roedy Green : feedback email
made with CSS
HTML Checked!
ICRA ratings logo
mindprod.com IP:[65.110.21.43]
Your face IP:[38.103.63.16] Spread the Net
You are visitor number 62,619.
You can get a fresh copy of this page from: or possibly from your local J: drive (Java virtual drive/Mindprod website mirror)
http://mindprod.com/jgloss/division.html J:\mindprod\jgloss\division.html