division : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (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, truncated toward 0. Floating point division gives a fractional answer as accurate as IEEE (Institute of Electrical & Electronics Engineers) 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 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



This page is posted
on the web at:

http://mindprod.com/jgloss/division.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\division.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[18.117.162.193]
You are visitor number