69

I have code to calculate the percentage difference between 2 numbers - (oldNum - newNum) / oldNum * 100; - where both of the numbers are doubles. I expected to have to add some sort of checking / exception handling in case oldNum is 0. However, when I did a test run with values of 0.0 for both oldNum and newNum, execution continued as if nothing had happened and no error was thrown. Running this code with ints would definitely cause an arithmetic division-by-zero exception. Why does Java ignore it when it comes to doubles?

Aziz Shaikh
  • 16,245
  • 11
  • 62
  • 79
froadie
  • 79,995
  • 75
  • 166
  • 235
  • 4
    Good question - the inconsistency between integer and double behavior adds confusion and hassle. – Steve B. Mar 04 '10 at 18:09
  • 2
    possible duplicate of [Why does division by zero with floating point (or double precision) numbers not throw java.lang.ArithmeticException: / by zero in Java](http://stackoverflow.com/questions/12954193/why-does-division-by-zero-with-floating-point-or-double-precision-numbers-not) – Raedwald Jan 21 '14 at 21:35
  • 2
    @Raedwald - considering that this question was posted 2 1/2 years before the one you linked, I would say that question is a (possible) duplicate of this one :) – froadie Jan 22 '14 at 10:24

5 Answers5

115

Java's float and double types, like pretty much any other language out there (and pretty much any hardware FP unit), implement the IEEE 754 standard for floating point math, which mandates division by zero to return a special "infinity" value. Throwing an exception would actually violate that standard.

Integer arithmetic (implemented as two's complement representation by Java and most other languages and hardware) is different and has no special infinity or NaN values, thus throwing exceptions is a useful behaviour there.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 4
    The proper answer to this question should be this. I needed to know why `float` and `double` does not cause Exceptions. Thanks. – Cengiz Can Oct 21 '11 at 09:25
  • 1
    This should be the marked correct answer. The answer currently marked as correct does not answer the question, nor does it make it clear that Java is following IEEE 754. – Necrototem Oct 06 '16 at 16:14
48

The result of division by zero is, mathematically speaking, undefined, which can be expressed with a float/double (as NaN - not a number), it isn't, however, wrong in any fundamental sense.

As an integer must hold a specific numerical value, an error must be thrown on division by zero when dealing with them.

RubioRic
  • 2,442
  • 4
  • 28
  • 35
Kris
  • 14,426
  • 7
  • 55
  • 65
  • 7
    Exactly. This is defined in the Java Language Spec here: http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 – Michael Myers Mar 04 '10 at 18:02
  • 1
    I tried outputting the result, and it's an NaN value. Does that mean ints can't hold NaN? – froadie Mar 04 '10 at 18:03
  • 1
    but this is the case of `0.0/0.0` which is NaN. – amit kumar Mar 04 '10 at 18:05
  • 1
    Infinity is "not a number" in the traditional sense, so it is implemented this way. – Kris Mar 04 '10 at 18:06
  • 6
    I think there should be an Integer.NaN – OscarRyz Mar 04 '10 at 18:16
  • 1
    There are mathematical reasons why there isn't Interger.NaN. For example, Elephant and Apple are both not numbers, so each are NaN. But if they were the same NaN they would be equal. But an Elephant != Apple. Now this example doesn't exactly apply when aquiring NaN through numeric means, but the principle is that they end up as separate entities that may or may not be the same. – Thomas Eding Mar 04 '10 at 18:20
  • Really? I always thought the result of division by zero is a nonsensical singularity... or "Not A Number". – Lawrence Dol Mar 04 '10 at 19:19
  • 14
    @trinithis: your explanation has nothing to do with the reason why there is no Integer.NaN. The reason is simply that the IEEE standard for floating point arithmetic mandates such special values whereas the quasi-standard for integer arithmetic (two's complement) has no such special values, thus leaving it to implementors how to deal with division by zero (raising an interrupt or exception, designating a special value, or just leaving the result undefined). – Michael Borgwardt Mar 04 '10 at 19:29
  • 2
    @Kris: Strictly speaking it can't be infinity because lim(1/x) is different as x -> 0+ and x -> 0-. But that's a minor quibble. – eaolson Mar 06 '10 at 17:33
  • @Oscar Reyes: that would mean there would be an Integer value that did not correspond to a legal int value, which would wreck all sort of havoc, I bet. – eaolson Mar 06 '10 at 17:34
  • 8
    x/0 is NOT infinity, the result is undefined. x/y tends to infinity when y tends to 0. Sorry for the thread necro. – SoManyGoblins Nov 07 '13 at 18:48
  • 1
    Downvote for wrong answer (and repeating the correct one). x/0 is not infinity. It is undefined. – starflyer Jul 29 '14 at 18:48
  • @starflyer You are incorrect: x/0 will result in infinity or negative infinity if x is not equal to 0. Only if x is equal to zero, the result is the defined value NaN (which most people would call undefined) . – fishinear Nov 24 '18 at 14:43
6

When divided by zero ( 0 or 0.00 )

  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

  3. But if we divide int by 0.0, then JVM will show Infinity:

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

    Console: Infinity

This is because JVM will automatically type cast int to double, so we get infinity instead of ArithmeticException.

Raman Gupta
  • 1,580
  • 15
  • 12
5

The way a double is stored is quite different to an int. See http://firstclassthoughts.co.uk/java/traps/java_double_traps.html for a more detailed explanation on how Java handles double calculations. You should also read up on Floating Point numbers, in particular the concept of Not a Number (NaN).

If you're interested in learning more about floating point representation, I'd advise reading this document (Word format, sorry). It delves into the binary representation of numbers, which may be helpful to your understanding.

Mike
  • 2,417
  • 1
  • 24
  • 33
4

Though Java developers know about the double primitive type and Double class, while doing floating point arithmetic they don't pay enough attention to Double.INFINITY, NaN, -0.0 and other rules that govern the arithmetic calculations involving them.

The simple answer to this question is that it will not throw ArithmeticException and return Double.INFINITY. Also, note that the comparison x == Double.NaN always evaluates to false, even if x itself is a NaN.

To test if x is a NaN, one should use the method call Double.isNaN(x) to check if given number is NaN or not. This is very close to NULL in SQL.

It may helpful for you.

ziyapathan
  • 41
  • 10