| Java Number Cruncher: The Java Programmer's Guide to Numerical Computing By Ronald Mak | Table of Contents | | Chapter 3. The Floating-Point Standard | 3.6 No Exceptions! What happens when "bad" things occur during floating-point operations? The IEEE 754 standard specifies standard responses to five kinds of exceptions, as shown in Table 3-3. Java provides the standard responses. The result of an overflow is either - Infinity or + Infinity, and the result of an underflow is either a denormalized number, -0.0, or +0.0. The IEEE 754 standard also specifies that, when an exception occurs, the computer must signal it by setting an associated status flag, and this flag must stay set until the program clears it. The standard also specifies that a program be able to trap the exception or ignore it by masking it. Table 3-3. Standard responses to floating-point exceptions, as specified by the IEEE 754 standard. Exception | Standard Response | Invalid operation | Set the result to Not-a-Number. | Division by zero | Set the result to ± . | Overflow | Set the result to the largest possible normalized number or to ± . | Underflow | Set the result to ±0, the smallest possible normalized number, or a denormalized number. | Inexact value | Set the result to the correctly rounded value. | Java deviates from this standard. Java's floating-point operations never throw exceptions. They are nonstop. Programs must check for the exceptions indirectly, such as by testing for NaN using the Float.isNaN() and Double.isNaN() methods , and we must be especially wary when results become - Infinity or + Infinity. |