60

Why doesn't this code throw an ArithmeticException? Take a look:

public class NewClass {

    public static void main(String[] args) {
        // TODO code application logic here
        double tab[] = {1.2, 3.4, 0.0, 5.6};

        try {
            for (int i = 0; i < tab.length; i++) {
                tab[i] = 1.0 / tab[i];
            }
        } catch (ArithmeticException ae) {
            System.out.println("ArithmeticException occured!");
        }
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Katie
  • 3,517
  • 11
  • 36
  • 49
  • so how can I change my code to get an ArithmeticException? (I dont want to change the type of an array to int)? – Katie Jan 03 '13 at 11:30
  • 3
    This is duplicated http://stackoverflow.com/questions/5291606/why-does-1-0-give-error-but-1-0-0-returns-inf – Seba Jan 03 '13 at 11:30
  • 2
    `if (tab[i] == 0) throw new ArithmeticException();`. – assylias Jan 03 '13 at 11:31
  • @assylias: Im not so sure about that, I read somewhere that we shouldnt throw an ArithmerticException – Katie Jan 03 '13 at 11:32
  • 1
    Katie, you are overthinking. :) assylias solution is just fine. If, for some reason, you don't want to throw ArithmeticException, just throw the exception you want. – gd1 Jan 03 '13 at 11:34
  • @gd1: ok, if you say so ;) – Katie Jan 03 '13 at 11:42
  • 1
    Checking floating value using `==` is potentially error prone. You should always check against sufficiently small number, to rule out rounding errors. – jnovacho Nov 15 '13 at 11:07
  • I think jnovacho is referring to assylias's comment. When comparing int values, you can use the `==` operator for comparison perfectly fine. But with floats, `0 == 0.0` will return false. You should use the Double Object: `if (Double.valueOf(0.0).compareTo(tab[i]) == 0)` – lugte098 Jan 24 '14 at 14:40
  • Possible duplicate of [Why does integer division by zero 1/0 give error but floating point 1/0.0 returns "Inf"?](https://stackoverflow.com/questions/5291606/why-does-integer-division-by-zero-1-0-give-error-but-floating-point-1-0-0-return) – Ciro Santilli OurBigBook.com Apr 14 '19 at 08:19

8 Answers8

99

IEEE 754 defines 1.0 / 0.0 as Infinity and -1.0 / 0.0 as -Infinity and 0.0 / 0.0 as NaN.

By the way, floating point values also have -0.0 and so 1.0/ -0.0 is -Infinity.

Integer arithmetic doesn't have any of these values and throws an Exception instead.

To check for all possible values (e.g. NaN, 0.0, -0.0) which could produce a non finite number you can do the following.

if (Math.abs(tab[i] = 1 / tab[i]) < Double.POSITIVE_INFINITY)
   throw new ArithmeticException("Not finite");
Michael
  • 41,989
  • 11
  • 82
  • 128
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
31

Why can't you just check it yourself and throw an exception if that is what you want.

    try {
        for (int i = 0; i < tab.length; i++) {
            tab[i] = 1.0 / tab[i];

            if (tab[i] == Double.POSITIVE_INFINITY ||
                    tab[i] == Double.NEGATIVE_INFINITY)
                throw new ArithmeticException();
        }
    } catch (ArithmeticException ae) {
        System.out.println("ArithmeticException occured!");
    }
Waqas Ilyas
  • 3,116
  • 1
  • 17
  • 27
26

That's because you are dealing with floating point numbers. Division by zero returns Infinity, which is similar to NaN (not a number).

If you want to prevent this, you have to test tab[i] before using it. Then you can throw your own exception, if you really need it.

gd1
  • 11,300
  • 7
  • 49
  • 88
12

0.0 is a double literal and this is not considered as absolute zero! No exception because it is considered that the double variable large enough to hold the values representing near infinity!

codeMan
  • 5,730
  • 3
  • 27
  • 51
  • 2
    No, 0.0 really is 0 and the IEEE floating point spec defines the result as a special value "positive infinity", not merely a very large value. – Sean Owen May 18 '13 at 15:12
12

Java will not throw an exception if you divide by float zero. It will detect a run-time error only if you divide by integer zero not double zero.

If you divide by 0.0, the result will be INFINITY.

Khalil
  • 121
  • 1
  • 2
8

When divided by zero

  1. If you divide double by 0, JVM will show Infinity.

    public static void main(String [] args){ double a=10.00; System.out.println(a/0); }
    

    Console: Infinity

  2. If you divide int by 0, then JVM will throw Arithmetic Exception.

    public static void main(String [] args){
        int a=10;
        System.out.println(a/0);
    }
    

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

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Raman Gupta
  • 1,580
  • 15
  • 12
3

There is a trick, Arithmetic exceptions only happen when you are playing around with integers and only during / or % operation.

If there is any floating point number in an arithmetic operation, internally all integers will get converted into floating point. This may help you to remember things easily.

Adam Horvath
  • 1,249
  • 1
  • 10
  • 25
0

This is behaviour of floating point arithmetic is by specification. Excerpt from the specification, § 15.17.2. Division Operator /:

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

wilx
  • 17,697
  • 6
  • 59
  • 114