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.

Screen Shot 2018-12-13 at 6.35.13 PM.png

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.

Screen Shot 2018-12-13 at 6.44.48 PM

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.