Dipanshu Yashaswi
Dipanshu Yashaswi

Reputation: 31

Java Exception Handling :/zeo

i am using java jdk1.8 version. when i devide any number by zero(0) it shows message "Infinity".While it should shows at runtime ArithmeticException :/zero.What can be the reason.

class division
{
      public static void main(String args[])
      {
          float a=10,b=0;
          System.out.println(a/b);
      }
}

Upvotes: 2

Views: 60

Answers (2)

Lajos Arpad
Lajos Arpad

Reputation: 77063

Mathematically,

x / 0 can be:

  • unkown if x = 0, since 0 / 0 = p can be easily solved to 0 = 0 * p, which is true for any p
  • invalid, since x / 0 = p can be easily solved to p * 0 = x, which, since x != 0 is untrue and paradoxical
  • infinite, since if we have a number y infinitely close to 0, rounded to 0, then x / 0 ~ x / y = +- infinity, depending on the signs of x and y

As @Bathsheba pointed out in his answer:

Java insists on the IEEE754 standard for floating point.

So, if we take a look at Wikipedia, for example, then we can see that:

Division by zero (an operation on finite operands gives an exact infinite result, e.g., 1/0 or log(0)) (returns ±infinity by default).

Upvotes: 0

Bathsheba
Bathsheba

Reputation: 234875

No it shouldn't issue an exception.

Java insists on the IEEE754 standard for floating point. This states that a division by a floating point zero yields NaN if the numerator is also zero, +Infinity if the numerator is positive, and -Infinity if the numerator is negative.

Upvotes: 6

Related Questions