Multiplying infinity by zero yields NaN. Multiplying infinity by a non-zero finite number produces an infinity
of the appropriate sign.
Floating-point division and remainder can produce infinities or NaN but never throw an exception. This table
shows the results of the various combinations:
x y x/y x%y
Finite ±0.0 ± NaN
Finite ± ±0.0 x
±0.0 ±0.0 NaN NaN
± Finite ± NaN
± ± NaN NaN
Otherwise, floating-point remainder (%) acts analogously to integer remainder as described earlier. See the
IEEEremainder method in "Math and StrictMath" on page 657 for a different remainder calculation.
Exercise 9.1: Write a program that uses the operators +, , *, and /, on two infinite operands and show the
result. Ensure that you try both same signed and opposite-signed infinity values.
9.1.3. Strict and Non-Strict Floating-Point Arithmetic
Floating-point arithmetic can be executed in one of two modes: FP-strict or not FP-strict. For simplicity, we
refer to these as strict and non-strict, respectively. Strict floating-point evaluation follows constrained rules
about exact floating-point operations: When you execute strict floating-point code you will always get exactly
equivalent results on all Java virtual machine implementations. Floating-point arithmetic that is non-strict can
be executed with somewhat relaxed rules. These rules allow the use of floating-point representations that may
avoid some overflows or underflows that would occur if the arithmetic were executed strictly. This means that
some applications may run differently on different virtual machines when under non-strict floating point.
Non-strict floating point might also execute faster than strict floating point, because the relaxed rules may
allow the use of representations that are supported directly by the underlying hardware.
The strictness of floating-point arithmetic is determined by the presence of the modifier strictfp, which
can be applied to a class, interface, or method. When you declare a method strictfp, all the code in the
method will be executed according to strict constraints. When you use strictfp on a class or interface, all
code in the class, including initializers and code in nested types, will be evaluated strictly. To determine if an
expression is strict, all methods, classes, and interfaces within which the expression is contained are
examined; if any of them is declared strictfp then the expression is strict. For example, an inner class
declared within a strict outer class, or within a strict method, is also strict. However, strictness is not
inheritedthe presence of strictfp on a class or interface declaration does not cause extended classes or
interfaces to be strict.
Constant expressions that require floating point are always evaluated strictly. Otherwise, any code that is not
marked as strictfp can be executed under rules that do not require repeatable results. If you want to
guarantee bit-for-bit exact results across all Java virtual-machine implementations, you should use
strictfp on relevant methods, classes, and interfaces. You should also note that a virtual machine can
satisfy the rules for non-strict floating point by always acting strictly: Non-strict floating point does not
require the virtual machine to act differently, it offers the virtual machine a degree of freedom to optimize
code where repeatable results are not required.