The following statement throws java.lang.ArithmeticException: / by zero
as obvious.
System.out.println(0/0);
because the literal 0
is considered to be an int
literal and divide by zero is not allowed in integer arithmetic.
The following case however doesn't throw any exception like java.lang.ArithmeticException: / by zero
.
int a = 0;
double b = 6.199;
System.out.println((b/a));
It displays Infinity
.
The following statement produces NaN
(Not a Number) with no exception.
System.out.println(0D/0); //or 0.0/0, or 0.0/0.0 or 0/0.0 - floating point arithmetic.
In this case, both of the operands are considered to be double.
Similarly, the following statements don't throw any exception.
double div1 = 0D/0; //or 0D/0D
double div2 = 0/0D; //or 0D/0D
System.out.printf("div1 = %s : div2 = %s%n", div1, div2);
System.out.printf("div1 == div2 : %b%n", div1 == div2);
System.out.printf("div1 == div1 : %b%n", div1 == div1);
System.out.printf("div2 == div2 : %b%n", div2 == div2);
System.out.printf("Double.NaN == Double.NaN : %b%n", Double.NaN == Double.NaN);
System.out.printf("Float.NaN == Float.NaN : %b%n", Float.NaN == Float.NaN);
They produce the following output.
div1 = NaN : div2 = NaN
div1 == div2 : false
div1 == div1 : false
div2 == div2 : false
Double.NaN == Double.NaN : false
Float.NaN == Float.NaN : false
They all return false.
Why is this operation (division by zero) allowed with floating point or double precision numbers?
By the way, I can understand that floating point numbers (double precision numbers) have their values that represent positive infinity, negative infinity, not a number (NaN
)...
It's that way because that's how IEEE 754 defined it.
Division by a floating point 0.0 yields NaN or +/-Inf, depending on whether the numerator is 0 or not.
Division by an integer 0 is not covered by IEEE 754, and generates an exception - there's no other way of indicating the error because an
int
can't representNaN
orInf
.Generating an exception is similar to the (software)
INT
generated by a division by zero on x86 microprocessors.To Infinity and Beyond
The above code, and the snippets you mentioned gives
infinity
.Why Java uses
Double
s to represent decimals. Binary cannot fully represent a number, it can only represent an approximation and therefore, neither can Java’s double.Imagine a number incredibly close to zero. If you know calculus, imagine a limit at zero. The variable would be approaching zero to some imaginably tiny distance but never equal exactly. You can imagine that, right? Well, suppose that number is needs so much precision for Java to represent, it gives up and calls it
0.0
because it does not have a good alternative. That’s what is happening here. Any regular number divided by a super close number to zero is basically, infinity. Try it:5 / (10^-100)
.Also refer to section : Special Floating-Point Values at Math Errors for more info :)
Related question : why does 1/0 give error but 1.0/0/0 give inf
UPDATE:
INT
does not have an infinity andNaN
value in set whereas float does have a infinity andNaN
value. (According to IEEE 754 standards that java follows)In short, that's the way it's specified in the IEEE-754 standard, which is what Java's Floating-Point Operations are based on.
When there is a divide by zero, the computer can not create a representation of the result as a number. The computer needs to signal that the result is not a number.
For floating point values it can produce a special not-a-number sentinel value, because there are some 32-bit (for
float
) and 64-bit (fordouble
) bit patterns that do not represent a number, and thus can be interpreted as not-a-number (NaN
).For integer values using the common twos-complement scheme (as Java requires), all 32-bit (for
int
) and 64-bit (forlong
) bit patterns represent a number. So the computer can not report the problem using a sentinel. It must report the problem "out of band" in some manner. Throwing an exception is the out of band method chosen for Java.Java follows the IEEE 754 standard which defines values to return by default in the case of a division by zero. http://en.wikipedia.org/wiki/IEEE_754#Exception_handling