Separation 1.0 / 0.0: output infinite

double d=1.0/0.0;

      

output Infinity

double d=1/0;

      

conclusion ArithmeticException

.

What is the difference between the two? What does Infinity mean here?

+3


source to share


3 answers


The first case is treated as division by double, and later as division by int, and therefore an ArthimeticException.

That's what infinity means



http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#POSITIVE_INFINITY

The separation of doubles and floats follows the IEEE 754 standard for floating point matching, which should not throw an exception.

+5


source


Mathematically, division by zero is undefined, although it can be considered unconscious. (With a little more stringency, this is a number greater than x for any value of x.)

IEEE754 double floating point (used by Java) has infinity representation. This is a 1.0 / 0.0 result. In this sense, 1.0 / 0.0 is computed as it is in floating point arithmetic.



The integral type has no infinity representation, so an exception is thrown. 1 / 0

calculated in integer arithmetic.

0


source


a floating point number has a way to encode "infinity". So infinity is a valid double variable. Integer variables do not have this option. So an exception is thrown instead.

0


source







All Articles