3

Why such different answers on dividing a number by zero:

My code:

class Test {
  public static void main(String[] args){

    int a = (int)(3/0.0F);
    System.out.println(a);

    System.out.println(3/0.0F);

    System.out.println(3/0);
  }
}

Output:

2147483647
Infinity
Exception in thread "main" java.lang.ArithmeticException: / by zero

Every time I divide a number by an integer (byte, short, int, long) it throws ArithmeticException, which is not the case when done with real numbers (float, double). Why?

Rudy Velthuis
  • 28,387
  • 5
  • 46
  • 94
Himanshu Aggarwal
  • 1,803
  • 2
  • 24
  • 36
  • 2
    Because floating-point is designed to return NaN in that circumstance. This is an out-of-band value. There are no out-band values for integers so an exception is required. – user207421 Jul 22 '14 at 04:22
  • 1
    In case you didn't know OP - NaN stands for "Not a Number". – takendarkk Jul 22 '14 at 04:23
  • @EJP - I wonder why they have different rules for floats and integers in case of division by zero. – Erran Morad Jul 22 '14 at 04:24
  • @BoratSagdiyev I just answered that. – user207421 Jul 22 '14 at 04:27
  • @EJP in that case, why the first statement didn't throw an exception on type casting? – Himanshu Aggarwal Jul 22 '14 at 04:27
  • 1
    Just so you know, the `2147483647` output is because you're casting infinity (as shown in next output) to an integer, so it is going to keeping rolling over the integer range and end up at the largest possible value (2^31-1 or 2147483647) – Michael Yaworski Jul 22 '14 at 04:39
  • 1
    @HimanshuAggarwal [JLS #5.1.3](http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.1.3):"The value must be too large (a positive value of large magnitude or positive infinity), and the result of the first step is the largest representable value of type int or long." – user207421 Jul 22 '14 at 04:43
  • i got that. thanks @EJP and mike – Himanshu Aggarwal Jul 22 '14 at 05:26
  • possible duplicate of [Why does the use of integer variables throw an exception?](http://stackoverflow.com/questions/3380749/why-does-the-use-of-integer-variables-throw-an-exception) – Raedwald Jul 22 '14 at 06:56
  • 2
    @EJP A finite number divided by zero does not produce NaN but an infinity in floating-point. Only 0.0/0.0 produces NaN. – Pascal Cuoq Jul 22 '14 at 07:21

1 Answers1

5

From JLS §15.17.2:

  • Division of a nonzero finite value by a zero results in a signed infinity. The sign is determined by the rule stated above.

with the exception that:

if the value of the divisor in an integer division is 0, then an ArithmeticException is thrown.

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • The obvious question is "Why did they write the specification in a way that does not treat floating point and integer arithmetic congruently?" – Mike Zboray Jul 22 '14 at 04:33
  • 4
    @mikez The obvious answer is "IEEE 754", and the lack of a way to implement that for integers because there is no integral `NaN.` – user207421 Jul 22 '14 at 04:38