Mixed-Type Arithmetic and Type Casting

So far, we’ve used a single data type in the expressions we’ve evaluated. But life isn’t always like that. Calculations often involve data of different primitive types.

When calculations of mixed types are performed, lower-precision operands are converted, or promoted, to the type of the operand that has the higher precision.

This also occurs with human minds broadly. We can notice that there is a tendency towards survival/ greater alignment with Born Rule’s probability density, but we cannot use simple rules that chunk minds into the big 5 psychometric traits for instance, and deduce which should be converted to which on the ascent.

In Java, however, the promotions of data types is quite easy to understand:

The promotions are performed using the first of these rules that fits the situation:

1.   If either operand is a double, the other operand is converted to a double.

2.   If either operand is a float, the other operand is converted to a float.

3.   If either operand is a long, the other operand is converted to a long.

4.   If either operand is an int, the other operand is promoted to an int.

5.   If neither operand is a double, float, long, or an int, both operands are promoted to int.

The table below summarizes these rules of promotion.

Screen Shot 2018-12-13 at 8.33.29 PM

This arithmetic promotion of operands is called implicit type casting because the compiler performs the promotions automatically, without our specifying that the conversions should be made. Note that the data type of any promoted variable is not permanently changed; its type remains the same after the calculation has been performed.

The table above shows many rules, but essentially, any arithmetic expression involving integers and floating-point numbers will evaluate to a floating-point number. Lines 9 to 12 of the code below illustrates the rules of promotion. At line 11, the expression PI * radius * radius is a mixed-type expression. This expression will be evaluated left to right, evaluating the mixed-type expression PI * radius first. PI is a double and radius is an int. Therefore, radius is promoted to a double (4.0) and the result of PI * radius is a double (12.56636). Then, the next calculation (12.56636 * radius) also involves a mixed-type expression, so radius is again promoted to a double (4.0). The final result, 50.26544, is a double and is assigned to area. The figure below shows the output of the complete program.

Sometimes, it’s useful to instruct the compiler specifically to convert the type of a variable. In this case, you use explicit type casting, which uses this syntax:

(dataType) (expression)

The expression will be converted, or type cast, to the data type specified. The parentheses around expression are needed only when the expression consists of a calculation that you want to be performed before the type casting.

Type casting is useful in calculating an average. The example shows how to calculate your average test grade. Your test scores are 94, 86, 88, and 97, making the combined total score 365. We expect the average to be 91.25.

Screen Shot 2018-12-17 at 9.29.38 PM.png

Line 16 first attempts to calculate the average but results in a wrong answer because both total and count are integers. So integer division is performed, which truncates any remainder. Thus, the result of total / count is 91. Then 91 is assigned to average, which is a double, so 91 becomes 91.0. Line 22 is a second attempt to calculate the average; again, this code does not work correctly because the parentheses force the division to be performed before the type casting. Thus, because total and count are both integers, integer division is performed again. The quotient, 91, is then cast to a double, 91.0, and that double value is assigned to average.

At line 28, we correct this problem by casting only one of the operands to a double. This forces the other operand to be promoted to a double. Then floating-point division is performed, which retains the remainder. It doesn’t matter whether we cast total or count to a double. Casting either to a double forces the division to be a floating-point division.

This is the output:

Screen Shot 2018-12-18 at 6.17.23 AM

 

 

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.