double d=1.0/0.0;
output is Infinity
double d=1/0;
output is ArithmeticException
.
What is difference between between these two? What is meaning of Infinity here?
In case of double/float division, the output is Infinity, the basic reason behind that it implements the floating point arithmetic algorithm which specifies a special values like “Not a number” OR “infinity” for “divided by zero cases” as per IEEE 754 standards.
Dividing by zero is an operation that has no meaning in ordinary arithmetic and is, therefore, undefined.
Division by zero returns Infinity , which is similar to NaN (not a number).
Dividing a floating-point value by zero doesn't throw an exception; it results in positive infinity, negative infinity, or not a number (NaN), according to the rules of IEEE 754 arithmetic.
The first case is treated as a division on double and the later as a division on int and hence the ArthimeticException.
Here is what infinity means
http://docs.oracle.com/javase/7/docs/api/java/lang/Double.html#POSITIVE_INFINITY
The division of doubles and floats is as per the IEEE 754 standards for floating point match which shouldnt throw an exception.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With