Search code examples
javafloating-pointdoubledivide-by-zero

Why is number divided by zero infinity in Java?


Why should the following code in Java

System.out.println(new Integer(1)/ new Double(0));

print 'Infinity' and not undefined. Isn't that mathematically wrong?


Solution

  • In mathematics, there are many different structures that support arithmetic. The most prominent ones, such as the natural numbers, the integers, and the real numbers, do not include infinity. In those systems, division by zero is not supported.

    Other systems do include at least one infinity. See, for example, the real projective line. It does permit division by zero.

    There is only one way to know what is mathematically defined or undefined in a particular system - study that system.

    Similarly, whether operations are commutative (a op b == b op a) and/or associative (a op (b op c) == (a op b) op c) depends on the system and the operation.

    IEEE 754 binary floating point is a system with a finite set of elements, including two infinities. Addition and multiplication are both commutative but not associative. Java real and double arithmetic are based on it. The Java Language Specification is the only way to know for sure what is or is not defined in Java floating point arithmetic.

    Many of the worst errors in using floating point have their basis in assuming that floating point numbers are real numbers, rather than a valid but different system.