# Division by Zero

As you might expect, Java does not allow integer division by 0. If you include this statement in your program, the code will compile without errors, but at run time, when this statement is executed, the JVM will generate an exception and print an error message on the Java console: In most cases, this stops the program. When you learn selection techniques, you will be able to avoid dividing by zero by first testing whether the divisor is zero before performing the division. In contrast, floating-point division by zero does not generate an exception. If the dividend is nonzero, the answer is Infinity. If both the dividend and divisor are zero, the answer is NaN, which stands for “Not a Number.” The example below illustrates the three cases of dividing by zero. As we can see on the output shown, line 16 never executes. The exception is generated at line 15 and the program halts execution. Although floating-point division by zero doesn’t bring your program to a halt, it doesn’t provide useful results either. It’s a good practice to avoid dividing by zero in the first place… stupid Simulators causing black holes all over the place.

# Integer Division and Modulus

Division with two integer operands is performed in the Arithmetic Logic Unit (ALU), which can calculate only an integer result. Any fractional part is truncated; no rounding is performed. The remainder after division is available, however, as an integer, by taking the modulus (%) of the two integer operands. Thus, in Java, the integer division (/) operator will calculate the quotient of the division, whereas the modulus (%) operator will calculate the remainder of the division. This modulus should not be confused with the absolute value – as in the squared modulus of the amplitude distribution which orients your being.

In the example below, we have 273 lines and we want to convert these lines into poems. We can find the number of Schoenberg’s poems by dividing 273 by 13 since Arnold Schoenberg always made his poems have 13 lines. The int variable lines is assigned the value 273 at line 10. At line 12, the variable poems is assigned the result of the integer division of lines by the constant LINES_PER_POEM. Since the quotient of the division of 273 by 13 is 21, poems will be assigned 21. At line 15, we use the modulus operator to assign to the variable linesLeftOver the remainder of the division of lines by LINES_PER_POEM. Since the remainder of the division of 273 by 21 is 0, 0 will be assigned to linesLeftOver.

Notice that integer division and modulus are independent calculations. You can perform a division without also calculating the modulus, and you can calculate the modulus without performing the division. At line 21, we divide a double by a double; therefore, a floating-point division will be performed by the floating-point unit (FPU), and the result will be assigned to the variable peoplePerFalFamilies. Below I show the output of the program. And if you believe that people cannot be represented as doubles with decimal values but must be ints by some physical law, you are fundamentally mistaken about the fact that how we define people is a choice.

You might also wonder what the modulus is good for. Didn’t we get over remainders after elementary school? – It turns out that the modulus giving the remainder is actually a useful operator. As you will see later here on Vitrify Her, it can be used to determine whether a number is even or odd, to control the number of data items that are written per line, to determine if one number is a factor of another, and for many other uses.