Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dividing 1.0/0.0: output is infinity [duplicate]

Tags:

java

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?

like image 759
user3593753 Avatar asked Jul 08 '15 11:07

user3593753


People also ask

Why is double divided by INFINITY zero?

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.

What happens when you divide an int variable by 0?

Dividing by zero is an operation that has no meaning in ordinary arithmetic and is, therefore, undefined.

What happens if u divide by 0 in Java?

Division by zero returns Infinity , which is similar to NaN (not a number).

Is Divide by zero a runtime exception?

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.


1 Answers

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.

like image 110
Kalyan Chavali Avatar answered Oct 11 '22 13:10

Kalyan Chavali