Why doesn't Java throw an Exception when dividing by 0.0?

JavaTypesIntegerDoubleDivide by-Zero

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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.

Solution 2 - Java

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.

Solution 3 - Java

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

  1. 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

  1. 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.

Solution 4 - Java

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.

Solution 5 - Java

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionfroadieView Question on Stackoverflow
Solution 1 - JavaMichael BorgwardtView Answer on Stackoverflow
Solution 2 - JavaKrisView Answer on Stackoverflow
Solution 3 - JavaRaman GuptaView Answer on Stackoverflow
Solution 4 - JavaMikeView Answer on Stackoverflow
Solution 5 - JavaziyapathanView Answer on Stackoverflow