3.4 The Floating-Point Operations
 Java Number Cruncher. The Java Programmer's Guide to Numerical Computing Authors: Mak R. Published year: 2001 Pages: 28/141

 Java Number Cruncher: The Java Programmer's Guide to Numerical Computing By Ronald  Mak Table of Contents Chapter  3.   The Floating-Point Standard

3.4 The Floating-Point Operations

Java supports the same additive, multiplicative, postfix, and unary arithmetic operations that it supports for the integer types, which we saw in Chapter 2.

Java allows additive and multiplicative operations between an integer value and a floating-point value. If at least one operand is floating-point, then Java converts the integer operand to floating-point, and then it performs a floating-point operation.

Table 3-2. The type of the result depends on the types of the operands when performing the floating-point addition, subtraction, multiplication, division, and remainder arithmetic operations.

Operand 1

Operand 2

Result

float

float

float

float

double

double

double

float

double

double

double

double

The result type of a postfix or a unary operation is the same as the type of its single operand. Table 3-2 shows how the result type of an additive or a multiplicative operation depends on the types of its two operands. The result type is float only if both operands are type float. Adding 1 to Float.MAX_VALUE results in Float.POSITIVE_INFINITY, not a double value.

The Java floating-point remainder operation is worth special mention. The floating-point operation is analogous to the integer operation. For any real values x and y, the remainder r is defined by

where q is an integer value whose sign is the same as the sign of , and it is the integer value with the largest magnitude such that . Program 3-2 demonstrates the operation with some examples. See Listing 3-2.

Listing 3-2 The float remainder operation.
```package numbercruncher.program3_2;

/**
* PROGRAM 3-2: Float Remainder
*
* Demonstrate the float remainder operation.
*/
public class FloatRemainder
{
public static void main(String args[])
{
float values[] = {
5f,   3f,
5.5f, 1.1f,
-5.5f, 2.1f,
5.5f, -3.1f,
-5.5f, -4.1f,
};
for (int i = 0; i < values.length/2; ++i) {
float x = values[2*i];
float y = values[2*i+1];

System.out.println(x + " % " + y + " = " + x%y);
}
}
}
```

Output:

```5.0 % 3.0 = 2.0
5.5 % 1.1 = 1.0999999
-5.5 % 2.1 = -1.3000002
5.5 % -3.1 = 2.4
-5.5 % -4.1 = -1.4000001
```

The output of Program 3-2 shows that 5.0 % 3.0 is 2.0, as in the integer version. -5.5 % 2.1 should be -1.3 exactly, because q = -2, and 5.5 - 2(-2.1) = -1.3. However, whereas -5.5 is exactly representable as a floating-point number, the float representation of 2.1 is slightly less than 2.1, and so we have a roundoff error. Similarly, -5.5 % (-4.1) has a roundoff error. The roundoff error is most evident in 5.5 % 1.1, which should be exactly 0. The floating-point representation of 1.1 is slightly greater than 1.1, and so q = 4 instead of 5. (We can confirm these number representations and subsequent roundoff errors using Program 3-1.)

Java's definition of the floating-point remainder operation differs from the IEEE 754 standard. Instead of using q as defined in the previous paragraph, the standard uses the integer value closest to . Java has the library routine Math.IEEEremainder() for computing the standard's remainder operation.

 Top

 Java Number Cruncher. The Java Programmer's Guide to Numerical Computing Authors: Mak R. Published year: 2001 Pages: 28/141