How are positive and negative infinity represented in Java floating-point, and how do operations behave with them?
answer
- Non-zero / 0.0 = signed Infinity, no exception (integer /0 throws)
- Overflow past MAX_VALUE rounds to Infinity
- Absorbing: Inf+1=Inf, 1/Inf=0
- Indeterminate forms (Inf-Inf, Inf/Inf, 0*Inf) = NaN
- isInfinite vs isFinite (isFinite excludes NaN too)
basics
~10 sDividing a non-zero number by zero gives infinity (Double.POSITIVE_INFINITY or NEGATIVE_INFINITY) instead of throwing. Infinity arithmetic mostly stays infinite, but undefined combos like Infinity - Infinity give NaN.
solid answer
~40 sJava's float/double follow IEEE 754, which includes signed infinities: Double.POSITIVE_INFINITY and Double.NEGATIVE_INFINITY (same for Float). They arise from dividing a non-zero value by 0.0 (sign depends on operands), or from overflow when a result exceeds the largest representable value. Crucially, floating-point division by zero does NOT throw — only integer division by zero throws ArithmeticException. Arithmetic on infinity is well-defined and 'absorbing': Infinity + 1 = Infinity, Infinity * 2 = Infinity, 1.0 / Infinity = 0.0. But indeterminate forms produce NaN: Infinity - Infinity, Infinity / Infinity, and 0.0 * Infinity. Comparisons work normally: POSITIVE_INFINITY is greater than every finite value. You detect them with Double.isInfinite(x), and Double.isFinite(x) excludes both infinity and NaN.
code
java · 10 linesSystem.out.println(1.0 / 0.0); // Infinity (no exception)
System.out.println(-1.0 / 0.0); // -Infinity
// System.out.println(1 / 0); // would THROW ArithmeticException (integer)
System.out.println(Double.MAX_VALUE * 2); // Infinity (overflow)
System.out.println(Double.POSITIVE_INFINITY - Double.POSITIVE_INFINITY); // NaN
System.out.println(1.0 / Double.POSITIVE_INFINITY); // 0.0
System.out.println(Double.isInfinite(1.0 / 0.0)); // true
System.out.println(Double.isFinite(Double.NaN)); // falsego deeper
Knows that float division by zero gives infinity rather than throwing, and the constants exist.
Explains how infinities arise (zero-division, overflow), the absorbing arithmetic rules, and that indeterminate forms produce NaN.
Contrasts ordered infinity vs unordered NaN, uses isFinite for validation, and knows the serialization/persistence pitfalls.
Designs numeric APIs and data pipelines that handle overflow/infinity sentinels deliberately and prevents non-finite values from leaking into storage or wire formats.
## Background Java `float`/`double` use **IEEE 754**, which reserves special bit patterns not just for NaN but for **two infinities**: positive and negative. Unlike integers, floating-point cannot throw on overflow or zero-division in the same way; instead it produces these special values so a computation can continue. ## The constants - `Double.POSITIVE_INFINITY`, `Double.NEGATIVE_INFINITY` - `Float.POSITIVE_INFINITY`, `Float.NEGATIVE_INFINITY` ## How infinities are produced 1. **Division of a non-zero number by zero** (note: a *floating-point* zero, `0.0`): - `1.0 / 0.0` → `+Infinity` - `-1.0 / 0.0` → `-Infinity` - The sign follows normal sign rules of the operands. - This does **not** throw. Only **integer** `1 / 0` throws `ArithmeticException`. 2. **Overflow**: when a result is larger than the biggest finite value `Double.MAX_VALUE`, it rounds *up* to infinity. Example: `Double.MAX_VALUE * 2` → `+Infinity`. ## How arithmetic behaves (absorbing rules) Infinity 'absorbs' finite operands: ``` +Infinity + 1.0 = +Infinity +Infinity * 2.0 = +Infinity +Infinity * -3.0 = -Infinity 1.0 / +Infinity = 0.0 -1.0 / +Infinity = -0.0 (negative zero) ``` ## Indeterminate forms → NaN Some combinations have no defined value and yield **NaN**: ``` +Infinity - +Infinity = NaN +Infinity / +Infinity = NaN 0.0 * +Infinity = NaN ``` These match the mathematical 'indeterminate forms'. ## Comparisons Infinities are **ordered** (unlike NaN). `Double.POSITIVE_INFINITY` is greater than every finite number and every `-Infinity`; `Double.NEGATIVE_INFINITY` is less than everything finite. So `5.0 < Double.POSITIVE_INFINITY` is `true`. This makes infinities useful as sentinel/initial values (e.g. initialize a running minimum to `+Infinity`). ## Detecting them ```java Double.isInfinite(x); // true for either signed infinity Double.isFinite(x); // true only for ordinary numbers (false for both Infinity and NaN) Double.isNaN(x); // separate check ``` ## Practical notes - Because float division by zero is silent, **validate** divisors or post-check with `isFinite` in numeric code, especially before persisting or serializing (JSON cannot represent Infinity/NaN, and many DB columns will reject or mangle them). - Infinity as a sentinel: `double min = Double.POSITIVE_INFINITY;` then `min = Math.min(min, value);` is a clean idiom because every real value is smaller.
- Why does 1.0/0.0 yield Infinity but 1/0 throws an exception?Integer arithmetic has no representation for infinity, so the JVM throws ArithmeticException. IEEE 754 floating-point reserves bit patterns for infinity, so the operation returns a value instead of throwing.
- What is a good use of Double.POSITIVE_INFINITY in everyday code?As an initial value when computing a minimum (or NEGATIVE_INFINITY for a maximum), since every finite value compares smaller (or larger), avoiding special first-element handling.
saying these in an interview costs you the question
- Claiming 1.0/0.0 throws ArithmeticException (only integer division does)
- Thinking Infinity - Infinity is 0 (it's NaN)
- Believing infinity is unordered like NaN (it is ordered)
- Serializing Infinity/NaN to JSON without handling it