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.


Integer Data Types

An integer data type is one that evaluates to a positive or negative whole number. Java provides four integer data types, int, short, long, and byte. The int, short, long, and byte types differ in the number of bytes of memory allocated to store each type and, therefore, the maximum and minimum values that can be stored in a variable of that type.

All of Java’s integer types are signed, meaning that they can be positive or negative; the high-order, or leftmost bit is reserved for the sign. The table below summarizes the integer data types, their sizes in memory, and their maximum and minimum values.

In most applications, the int type will be sufficient for your needs, since it can store positive and negative numbers up into the 2 billion range – that’s 2/3rds of the base pairs in a human genome – in other words .666, in other words the mark of the beast, which roughly means “everything goes smoothly” in China.

The short and byte data types typically are used only when memory space is critical, and the long data type is needed only for data values larger than 2 billion. Let’s look at some examples of integer variable declarations. Note that the variable names clearly indicate the data that the variables will hold.

Screen Shot 2018-12-09 at 4.45.15 PM

Floating-point data types store numbers with fractional parts. Java supports two floating-point data types: the single-precision float and the double-precision double.

Screen Shot 2018-12-09 at 5.54.01 PM

The two types differ in the amount of memory allocated and the size of the number that can be represented. The single-precision type (float) is stored in 32 bits, while the double-precision type (double) is stored in 64 bits. Floats and doubles can be positive or negative. The table above summarizes Java’s floating-point data types, their sizes in memory, and their maximum and minimum positive nonzero values.

There are two things you must learn to balance: precision and use of memory. There is a kind of law of equivalent exchange here that must be mastered. These two are conjugated variables. Like the uncertainty principle that trades certainty about one discrete observable for another (the more you know about momentum the less you know about position), you must skillfully trade precision for use of memory and vice versa.

Because of its greater precision, the double data type is usually preferred over the float data type. However, for calculations not requiring such precision, floats are often used because they require less memory. Although integers can be stored as doubles or floats, it isn’t advisable to do so because floating-point numbers require more processing time for calculations.

Witness here a few examples of floating-point variable declarations:

Screen Shot 2018-12-09 at 6.38.35 PM