Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java

In short, that’s the way it’s specified in the IEEE-754 standard, which is what Java’s Floating-Point Operations are based on.

Why doesn’t division by zero (or overflow, or underflow) stop the program or trigger an error? Why does a standard on numbers include “not-a-number” (NaN)?

The 754 model encourages robust programs. It is intended not only for numerical analysts but also for spreadsheet users, database systems, or even coffee pots. The propagation rules for NaNs and infinities allow inconsequential exceptions to vanish. Similarly, gradual underflow maintains error properties over a precision’s range.

When exceptional situations need attention, they can be examined immediately via traps or at a convenient time via status flags. Traps can be used to stop a program, but unrecoverable situations are extremely rare. Simply stopping a program is not an option for embedded systems or network agents. More often, traps log diagnostic information or substitute valid results.

Flags offer both predictable control flow and speed. Their use requires the programmer be aware of exceptional conditions, but flag stickiness allows programmers to delay handling exceptional conditions until necessary.

Leave a Comment